You are here

fields_rsi_prevention.inc in Media Gallery 7.2

Same filename and directory in other branches
  1. 7 fields_rsi_prevention.inc

This file provides easier access on entity properties and methods.

File

fields_rsi_prevention.inc
View source
<?php

/**
 * @file
 * This file provides easier access on entity properties and methods.
 */

/**
 * Decorates an entity to provide getters/setters.
 *
 * @example
 *
 * $node = new FieldRSIPreventor($node);
 *
 * // This still works,
 * $node->created
 *
 * // Gets the first value of body for LANGUAGE_NONE.
 * $node->getValue('body');
 *
 * // Gets the 2nd value of body in spanish
 * $node->getValue('body', 2, 'esp');
 */
class FieldsRSIPreventor {
  private $entity;
  function __construct($entity) {

    // Prevent this thing from chaining if people accidentally use it twice.
    if ($entity instanceof FieldRSIPreventor) {
      $entity = $entity->entity;
    }
    $this->entity = $entity;
  }
  function getValue($field_name, $delta = 0, $language = LANGUAGE_NONE) {
    if ($item = $this
      ->getItem($field_name, $delta, $language)) {
      return $item['value'];
    }
  }
  function getItem($field_name, $delta = 0, $language = LANGUAGE_NONE) {
    if (!isset($this->entity->{$field_name}[$language]) || !isset($this->entity->{$field_name}[$language][$delta])) {
      return FALSE;
    }
    return $this->entity->{$field_name}[$language][$delta];
  }
  function __get($key) {
    return $this->entity->{$key};
  }
  function __set($key, $value) {
    $this->entity->{$key} = $value;
  }

}

Classes

Namesort descending Description
FieldsRSIPreventor Decorates an entity to provide getters/setters.