/** * {@inheritdoc} * @see \Scalr\Model\Objects\AdapterInterface::toEntity() */ public function toEntity($data) { $entityClass = $this->getEntityClass(); $entity = new $entityClass(); $converterRules = $this->getRules(); $bconvert = $this instanceof ApiEntityAdapter; $it = $entity->getIterator(); if (!is_object($data)) { $data = (object) $data; } if (!empty($converterRules[static::RULE_TYPE_TO_DATA])) { foreach ($converterRules[static::RULE_TYPE_TO_DATA] as $key => $property) { $key = is_int($key) ? $property : $key; if (isset($data->{$property})) { if ($key[0] === '_' && method_exists($this, $key)) { //It is callable $this->{$key}($data, $entity, self::ACT_CONVERT_TO_ENTITY); } else { $entity->{$key} = $bconvert ? ApiEntityAdapter::convertInputValue($it->getField($key)->column->type, $data->{$property}) : $data->{$property}; } } } } else { foreach ($it->fields() as $field) { /* @var $field \Scalr\Model\Loader\Field */ $key = $field->name; if ($key[0] === '_' && method_exists($this, $key)) { //It is callable $this->{$key}($data, $entity, self::ACT_CONVERT_TO_ENTITY); } elseif (isset($data->{$key})) { $entity->{$key} = $bconvert ? ApiEntityAdapter::convertInputValue($field->column->type, $data->{$key}) : $data->{$key}; } } } return $entity; }
/** * @test * @param string $filedType * @param mixed $value Value what we have to convert * @dataProvider providerConvertInputValue() * @expectedException \Scalr\Api\Rest\Exception\ApiErrorException */ public function testConvertInputValue($filedType, $value) { ApiEntityAdapter::convertInputValue($filedType, $value); }