/** * Hydrate one entity. * * ### Options: * * * fieldList: A whitelist of fields to be assigned to the entity. If not present, * the accessible fields list in the entity will be used. * * accessibleFields: A list of fields to allow or deny in entity accessible fields. * * @param array $data The data to hydrate. * @param array $options List of options * @return \Muffin\Webservice\Model\Resource * @see \Muffin\Webservice\Model\Endpoint::newEntity() */ public function one(array $data, array $options = []) { list($data, $options) = $this->_prepareDataAndOptions($data, $options); $primaryKey = (array) $this->_endpoint->primaryKey(); $resourceClass = $this->_endpoint->resourceClass(); /* @var \Muffin\Webservice\Model\Resource $entity */ $entity = new $resourceClass(); $entity->source($this->_endpoint->registryAlias()); if (isset($options['accessibleFields'])) { foreach ((array) $options['accessibleFields'] as $key => $value) { $entity->accessible($key, $value); } } $errors = $this->_validate($data, $options, true); $properties = []; foreach ($data as $key => $value) { if (!empty($errors[$key])) { $entity->invalid($key, $value); continue; } if ($value === '' && in_array($key, $primaryKey, true)) { // Skip marshalling '' for pk fields. continue; } $properties[$key] = $value; } if (!isset($options['fieldList'])) { $entity->set($properties); $entity->errors($errors); return $entity; } foreach ((array) $options['fieldList'] as $field) { if (array_key_exists($field, $properties)) { $entity->set($field, $properties[$field]); } } $entity->errors($errors); return $entity; }
protected function _transformResource(array $result, Endpoint $endpoint) { $properties = []; foreach ($result as $field => $value) { if ($field === 'custom_fields') { // Loop over custom fields foreach ($value as $customField) { // Get the alias for the custom field $customFieldField = Schema::nameToField($customField['name']); // Lookup the field in the schema $column = $endpoint->schema()->column($customFieldField); // If no value has been given set it to null if (!isset($customField['value'])) { $properties[$customFieldField] = null; continue; } // Cast value to correct type and set it as property $properties[$customFieldField] = $this->castValue($customField['value'], $column['type']); } continue; } $column = $endpoint->schema()->column($field); if (!$column) { $properties[$field] = $value; continue; } $properties[$field] = $this->castValue($value, $column['type']); } return $this->_createResource($endpoint->resourceClass(), $properties); }
/** * Turns a single result into a resource * * @param \Muffin\Webservice\Model\Endpoint $endpoint The endpoint class to use * @param array $result The API result * * @return \Muffin\Webservice\Model\Resource */ protected function _transformResource(Endpoint $endpoint, array $result) { $properties = []; foreach ($result as $property => $value) { $properties[$property] = $value; } return $this->_createResource($endpoint->resourceClass(), $properties); }