You are here

class ECKEntity in Entity Construction Kit (ECK) 7.3

Same name and namespace in other branches
  1. 7 eck.module \EckEntity
  2. 7.2 eck.module \ECKEntity

Hierarchy

Expanded class hierarchy of ECKEntity

2 string references to 'ECKEntity'
eck_permissions_eck_access in modules/eck_permissions/eck_permissions.module
eck__entity_type__info in ./eck.entity_type.inc
Generate the entity info for a specific entity.

File

./eck.classes.inc, line 765
Classes for all the different objects used in ECK.

View source
class ECKEntity extends Entity {

  // This flag only gets set so we can make our properties public again
  // before we save the object.
  protected $ignoreValidation;
  protected $PropertyValues = array();

  /**
   * Constructor.
   */
  public function __construct(array $values = array(), $entity_type = NULL) {
    $this->ignoreValidation = FALSE;
    parent::__construct($values, $entity_type);

    // I have this stupid crap.. fixing drupals mess ups.
    $entity_type_name = $this
      ->entityType();
    $entity_type = EntityType::loadByName($entity_type_name);
    $properties = $entity_type->properties;
    $property_names = array_keys($properties);
    foreach ($property_names as $pn) {
      $value = $this->{$pn};
      unset($this->{$pn});
      $this->{$pn} = $value;
    }
  }

  /**
   * Magic set.
   */
  public function __set($name, $value) {

    // Lets implement non restrictive validation.
    // If it is a property validate, if it isn't just set it.
    // Try to load the entityType and property information.
    if ($entity_type_name = $this
      ->entityType()) {
      if (($entity_type = EntityType::loadByName($entity_type_name)) && is_object($entity_type)) {
        if ($properties = $entity_type->properties) {
          $property_names = array_keys($properties);
        }
      }
    }

    // @todo should look into loading the entity info through entity_get_info()
    // and determining validation that way.
    // Currently we don't validate when the ECKEntityType has not been
    // instantiated yet. Not sure if it is needed though.
    if (isset($property_names) && in_array($name, $property_names) && !$this->ignoreValidation) {
      $property_type = $properties[$name]['type'];
      $property_type_class = eck_get_property_type_class($property_type);
      if (!isset($value)) {
        $schema = $property_type_class::schema();
        if (isset($schema['default'])) {
          $this->PropertyValues[$name] = $schema['default'];
        }
      }
      elseif ($property_type_class::validate($value)) {
        $this->PropertyValues[$name] = $value;
      }
      else {
        throw new Exception("Invalid value {$value} for property {$name} of type {$property_type} in\n        Entity type: {$entity_type_name}");
      }
    }
    else {
      $this->{$name} = $value;
    }
  }

  /**
   * Magic get.
   */
  public function __get($name) {
    if (array_key_exists($name, $this->PropertyValues)) {
      return $this->PropertyValues[$name];
    }
    elseif (isset($this->{$name})) {
      return $this->{$name};
    }
    elseif ($field = field_info_field($name)) {
      if ($this->entityType) {
        if ($bundle = $this
          ->bundle()) {
          if (in_array($bundle, $field['bundles'][$this->entityType])) {
            $this->{$name} = array();
            return $this->{$name};
          }
        }
      }
    }
    return NULL;
  }

  /**
   * Magic set.
   */
  public function __isset($name) {
    if (array_key_exists($name, $this->PropertyValues)) {
      return TRUE;
    }
    elseif (isset($this->{$name})) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Save.
   */
  public function save() {

    // Going back to public properties.
    $this->ignoreValidation = TRUE;
    foreach ($this->PropertyValues as $property => $value) {
      $this->{$property} = $value;
    }
    return parent::save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ECKEntity::$ignoreValidation protected property
ECKEntity::$PropertyValues protected property
ECKEntity::save public function Save. Overrides Entity::save
ECKEntity::__construct public function Constructor. Overrides Entity::__construct
ECKEntity::__get public function Magic get.
ECKEntity::__isset public function Magic set.
ECKEntity::__set public function Magic set.
Entity::$defaultLabel protected property 1
Entity::$entityInfo protected property
Entity::$entityType protected property
Entity::$idKey protected property
Entity::$wrapper protected property
Entity::buildContent public function Builds a structured array representing the entity's content. Overrides EntityInterface::buildContent 1
Entity::bundle public function Returns the bundle of the entity. Overrides EntityInterface::bundle
Entity::defaultLabel protected function Defines the entity label if the 'entity_class_label' callback is used. 1
Entity::defaultUri protected function Override this in order to implement a custom default URI and specify 'entity_class_uri' as 'uri callback' hook_entity_info().
Entity::delete public function Permanently deletes the entity. Overrides EntityInterface::delete
Entity::entityInfo public function Returns the info of the type of the entity. Overrides EntityInterface::entityInfo
Entity::entityType public function Returns the type of the entity. Overrides EntityInterface::entityType
Entity::export public function Exports the entity. Overrides EntityInterface::export
Entity::getTranslation public function Gets the raw, translated value of a property or field. Overrides EntityInterface::getTranslation
Entity::hasStatus public function Checks if the entity has a certain exportable status. Overrides EntityInterface::hasStatus
Entity::identifier public function Returns the entity identifier, i.e. the entities name or numeric id. Overrides EntityInterface::identifier
Entity::internalIdentifier public function Returns the internal, numeric identifier. Overrides EntityInterface::internalIdentifier
Entity::isDefaultRevision public function Checks whether the entity is the default revision. Overrides EntityInterface::isDefaultRevision
Entity::label public function Returns the label of the entity. Overrides EntityInterface::label
Entity::setUp protected function Set up the object instance on construction or unserializiation.
Entity::uri public function Returns the uri of the entity just as entity_uri(). Overrides EntityInterface::uri
Entity::view public function Generate an array for rendering the entity. Overrides EntityInterface::view
Entity::wrapper public function Returns the EntityMetadataWrapper of the entity. Overrides EntityInterface::wrapper
Entity::__sleep public function Magic method to only serialize what's necessary.
Entity::__wakeup public function Magic method to invoke setUp() on unserialization.