/** * {@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; }