Scalr\Api\DataType\ApiEntityAdapter::convertOutputValue PHP Method

convertOutputValue() public static method

Converts output value
public static convertOutputValue ( string $fieldType, string $value ) : mixed
$fieldType string Field type
$value string A value taken from input
return mixed Returns value which can be used in the response
    public static function convertOutputValue($fieldType, $value)
    {
        switch ($fieldType) {
            case 'boolean':
                $result = (bool) $value;
                break;
            case 'UTCDatetime':
            case 'UTCDate':
                $result = is_null($value) ? null : $value->format('Y-m-d\\TH:i:s\\Z');
                break;
            case 'date':
            case 'datetime':
                if (is_null($value)) {
                    $result = null;
                } else {
                    if (date_default_timezone_get() !== 'UTC') {
                        $value->setTimezone(new \DateTimeZone(date_default_timezone_get()));
                    }
                    $result = $value->format('Y-m-d\\TH:i:s\\Z');
                }
                break;
            default:
                $result = $value;
        }
        return $result;
    }

Usage Example

示例#1
0
 /**
  * {@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;
 }