public getIterator ( ) : Scalr\Model\EntityPropertiesIterator | ||
return | Scalr\Model\EntityPropertiesIterator | Gets EntityPropertiesIterator |
/** * Constructor * * @param AbstractEntity $entity The target * @param string $field The field name */ public function __construct(AbstractEntity $entity, $field) { $this->entity = $entity; if (!$entity instanceof AbstractEntity) { throw new \InvalidArgumentException("The first argument should be instance of the AbstractEntity class."); } $this->field = $this->entity->getIterator()->getField($field); if (!$this->field) { throw new ModelException(sprintf("Invalid field %s for entity %s", $field, get_class($this->entity))); } }
/** * Copies all alterable properties from the request object to Entity * * It does not validate the values. It only checks whether the request * contains only alterable properties. If not it will raise ApiErrorExceptions * * @param object $object An object (source) * @param AbstractEntity $entity An Entity (destination) * @throws ApiErrorException * @throws Exception */ public function copyAlterableProperties($object, AbstractEntity $entity) { $rules = $this->getRules(); if (!isset($rules[static::RULE_TYPE_ALTERABLE])) { //Nothing to copy throw new \Exception(sprintf("ApiEntityAdapter::RULE_TYPE_ALTERABLE offset of rules has not been defined for the %s class.", get_class($this))); } $it = $entity->getIterator(); $notAlterable = array_diff(array_keys(get_object_vars($object)), $rules[static::RULE_TYPE_ALTERABLE]); if (!empty($notAlterable)) { if (count($notAlterable) > 1) { $message = "You are trying to set properties %s that either are not alterable or do not exist"; } else { $message = "You are trying to set the property %s which either is not alterable or does not exist"; } throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, sprintf($message, implode(', ', $notAlterable))); } foreach ($rules[static::RULE_TYPE_ALTERABLE] as $key) { if (!property_exists($object, $key)) { continue; } //As the name of the property that goes into response may be different from the //real property name in the Entity object it should be mapped at first if (!empty($rules[static::RULE_TYPE_TO_DATA])) { //if toData rule is null it means all properties are allowed if (($property = array_search($key, $rules[static::RULE_TYPE_TO_DATA])) !== false) { if (is_string($property)) { //In this case the real name of the property is the key of the array if ($property[0] === '_' && method_exists($this, $property)) { //It is callable $this->{$property}($object, $entity, self::ACT_CONVERT_TO_ENTITY); continue; } } else { $property = $key; } } } $property = isset($property) ? $property : $key; $entity->{$property} = $object->{$key} === null ? null : self::convertInputValue($it->getField($property)->column->type, $object->{$key}); } }
/** * Execute statement * * @param AbstractEntity $entity Next entity to saving in database * @param string $type optional The statement type (see EntityStatement::TYPE_* const) * * @return EntityStatement * @throws ModelException */ public function execute(AbstractEntity $entity, $type = null) { if (!$entity instanceof $this->entityClass) { throw new InvalidArgumentException("This statement processes only '{$this->entityClass}' entities!"); } /* @var $entity AbstractEntity */ $params = []; $auto = $entity->getIterator()->getAutogenerated(); if (empty($type)) { $type = isset($auto) && !empty($entity->{$auto->name}) ? static::TYPE_UPDATE : static::TYPE_INSERT; } switch ($type) { case static::TYPE_UPDATE: $stmt = $this->updateStmt; $fields = $this->updateParams; break; case static::TYPE_INSERT: $stmt = $this->insertStmt; $fields = $this->insertParams; break; case static::TYPE_DELETE: $stmt = $this->deleteStmt; $fields = $this->deleteParams; break; default: throw new DomainException("Unknown statement type {$type}"); } foreach ($this->prepare($type, $fields) as $field) { if (empty($entity->{$field->name})) { $entity->{$field->name} = $field->type->generateValue($entity); } } foreach ($fields as $field) { $params[] = $field->type->toDb($entity->{$field->name}); } $this->db->Execute($stmt, $params); if (isset($auto)) { $entity->{$auto->name} = $auto->type->toPhp($this->db->Insert_ID()); } return $this; }
/** * Creates and save entity to DB, keeps entity to delete after test * * @param AbstractEntity $entity Entity instance * @param array $data Properties to initialize * @param array $requiredData The list of names properties which should be save and initialize after delete * @return AbstractEntity * @throws \Scalr\Exception\ModelException */ public static function createEntity(AbstractEntity $entity, array $data, array $requiredData = null) { if (!empty($data)) { $it = $entity->getIterator(); foreach ($data as $prop => $value) { if ($it->getField($prop)) { $entity->{$prop} = $value; } else { throw new InvalidArgumentException(sprintf("Field %s does not exist in %s entity.", $prop, get_class($entity))); } } } $initProperties = []; if (!empty($requiredData)) { foreach ($requiredData as $prop) { if (isset($data[$prop])) { $initProperties[$prop] = $data[$prop]; } else { throw new InvalidArgumentException(sprintf("Field %s does not exist in data.", $prop)); } } } $entity->save(); $key = []; foreach ($entity->getIterator()->getPrimaryKey() as $position => $property) { $key[$position] = $entity->{$property}; } static::toDelete(get_class($entity), $key, $initProperties); return $entity; }
/** * Creates and save entity to DB^ keep entity to delete after test * * @param AbstractEntity $entity * @param array $data * * @return AbstractEntity */ public function createEntity(AbstractEntity $entity, array $data) { foreach ($entity->getIterator() as $property => $_) { if (isset($data[$property])) { $entity->{$property} = $data[$property]; } } $entity->save(); $key = []; foreach ($entity->getIterator()->getPrimaryKey() as $position => $property) { $key[$position] = $entity->{$property}; } static::$testData[get_class($entity)][] = $key; return $entity; }
/** * Creates and save entity to DB, keeps entity to delete after test * * @param AbstractEntity $entity * @param array $data * @param int $priority * * @return AbstractEntity * @throws \Scalr\Exception\ModelException */ public static function createEntity(AbstractEntity $entity, array $data, $priority = 0) { foreach ($entity->getIterator() as $property => $_) { if (isset($data[$property])) { $entity->{$property} = $data[$property]; } } $entity->save(); $key = []; foreach ($entity->getIterator()->getPrimaryKey() as $position => $property) { $key[$position] = $entity->{$property}; } static::toDelete(get_class($entity), $key, $priority); return $entity; }