/** * Used for object formatting. * * @param string $entity * @param array $data * @param array $options * @return mixed */ public function cast($entity, array $data, array $options = array()) { foreach ($data as $key => $val) { if (!is_array($val)) { continue; } $class = 'entity'; $model = $entity->model(); $data[$key] = $this->item($model, $val, compact('class')); } return parent::cast($entity, $data, $options); }
/** * Casts data into proper format when added to a collection or entity object. * * @param mixed $entity The entity or collection for which data is being cast, or the name of * the model class to which the entity/collection is bound. * @param array $data An array of data being assigned. * @param array $options Any associated options with, for example, instantiating new objects in * which to wrap the data. Options implemented by `cast()` itself: * - `first` _boolean_: Used when only one value is passed to `cast()`. Even though * that value must be wrapped in an array, setting the `'first'` option to `true` * causes only that one value to be returned. * @return mixed Returns the value of `$data`, cast to the proper format according to the schema * definition of the model class specified by `$model`. */ public function cast($entity, array $data, array $options = array()) { $defaults = array('pathKey' => null, 'model' => null); $options += $defaults; $model = $options['model'] ?: $entity->model(); foreach ($data as $key => $val) { if (!is_array($val)) { continue; } $pathKey = $options['pathKey'] ? "{$options['pathKey']}.{$key}" : $key; $class = (range(0, count($val) - 1) === array_keys($val)) ? 'array' : 'entity'; $data[$key] = $this->item($model, $val, compact('class', 'pathKey') + $options); } return parent::cast($entity, $data, $options); }
/** * The cast() method is used by the data source to recursively inspect and * transform data as it's placed into a collection. In this case, we'll use * cast() to transform arrays into Document objects. * * @param the query model * @param the request results * @param options ie(set, service, entity) */ public function cast($entity, array $data, array $options = array()) { $model = $entity->model(); foreach ($data as $key => $val) { if (!is_array($val)) { continue; } if (!empty($val['doclist']['docs'])) { //XXX The builder is hard-coded to limit groups to one element, // which is why we only use one doc here. //TODO This seems like something that belongs in model-config / queries. $data[$key] = $this->item($model, $val['doclist']['docs'][0], array('class' => 'entity')); } else { $data[$key] = $this->item($model, $val, array('class' => 'entity')); } } return parent::cast($entity, $data, $options); }