/** * {@inheritdoc} * @see \Scalr\Api\DataType\ApiEntityAdapter::validateObject() */ public function validateObject($object, $method = null) { parent::validateObject($object, $method); if (isset($object->scope) && $object->scope !== $this->controller->getScope()) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid scope"); } }
/** * {@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; }
/** * {@inheritdoc} * @see \Scalr\Api\DataType\ApiEntityAdapter::validateObject() */ public function validateObject($object, $method = null) { parent::validateObject($object, $method); if (isset($object->scope) && $object->scope !== ScopeInterface::SCOPE_ENVIRONMENT) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid scope"); } }
/** * @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); }
/** * {@inheritdoc} * @see ApiEntityAdapter::validateObject() */ public function validateObject($object, $method = null) { parent::validateObject($object, $method); if ($method === Request::METHOD_POST) { if (empty($object->name)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Missed property name'); } if ($object->ruleType === static::BASIC_SCALING_RULE xor !isset(ScalingMetricAdapter::$nameMap[$object->name])) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, 'Unexpected ruleType value'); } } }
/** * {@inheritdoc} * @see \Scalr\Model\Objects\AdapterInterface::toData() */ public function toData($entity) { $dataClass = $this->getDataClass(); $result = new $dataClass(); if (!$entity instanceof AbstractEntity) { throw new \InvalidArgumentException(sprintf("%s expects first argument to be instance of AbstractEntity class.", __METHOD__)); } $it = $entity->getIterator(); $bconvert = $this instanceof ApiEntityAdapter; $converterRules = $this->getRules(); if (!empty($converterRules[static::RULE_TYPE_TO_DATA])) { foreach ($converterRules[static::RULE_TYPE_TO_DATA] as $key => $property) { //This is necessary when result data key does not match the property name of the entity $key = is_int($key) ? $property : $key; if ($key[0] === '_' && method_exists($this, $key)) { //It is callable $this->{$key}($entity, $result, self::ACT_CONVERT_TO_OBJECT); } else { $result->{$property} = isset($entity->{$key}) ? $bconvert ? ApiEntityAdapter::convertOutputValue($it->getField($key)->column->type, $entity->{$key}) : $entity->{$key} : null; } } } else { foreach ($it->fields() as $field) { $result->{$field->name} = $bconvert ? ApiEntityAdapter::convertOutputValue($field->column->type, $entity->{$field->name}) : $entity->{$field->name}; } } return $result; }