/**
  * Gets a resource instance for the provided parameter
  * @param mixed $parameter Parameter to adapt
  * @param \ride\library\http\jsonapi\JsonApiDocument $document Document
  * which is requested
  * @param string $relationshipPath dot-separated list of relationship names
  * @return JsonApiResource|null
  */
 public function getResource($translation, JsonApiDocument $document, $relationshipPath = null)
 {
     if ($translation === null) {
         return null;
     } elseif (!is_array($translation) || !isset($translation['id']) || !isset($translation['locale']) || !isset($translation['key']) || !isset($translation['value'])) {
         throw new JsonApiException('Could not get resource: provided data is not a translation');
     }
     $query = $document->getQuery();
     $api = $document->getApi();
     $resource = $api->createResource($this->type, $translation['id'], $relationshipPath);
     $resource->setLink('self', $this->web->getUrl('api.translations.detail', array('id' => $translation['id'])));
     if ($query->isFieldRequested($this->type, 'key')) {
         $resource->setAttribute('key', $translation['key']);
     }
     if ($query->isFieldRequested($this->type, 'value')) {
         $resource->setAttribute('value', $translation['value']);
     }
     if ($query->isFieldRequested($this->type, 'locale') && $query->isIncluded($relationshipPath)) {
         $adapter = $api->getResourceAdapter('locales');
         $fieldRelationshipPath = ($relationshipPath ? $relationshipPath . '.' : '') . 'locale';
         $relationshipResource = $adapter->getResource($translation['locale'], $document, $fieldRelationshipPath);
         $relationship = $api->createRelationship();
         $relationship->setResource($relationshipResource);
         $relationship->setLink('self', $this->web->getUrl('api.translations.relationship', array('id' => $translation['locale']->getCode() . '-' . $translation['key'], 'relationship' => 'locale')));
         $relationship->setLink('related', $this->web->getUrl('api.translations.related', array('id' => $translation['locale']->getCode() . '-' . $translation['key'], 'relationship' => 'locale')));
         $resource->setRelationship('locale', $relationship);
     }
     return $resource;
 }
 /**
  * Creates a new document
  * @param JsonApiQuery $query Query of the document
  * @return JsonApiDocument
  */
 public function createDocument(JsonApiQuery $query = null)
 {
     $document = new JsonApiDocument();
     $document->setApi($this);
     if ($query) {
         $document->setQuery($query);
     }
     return $document;
 }
 /**
  * Gets a resource instance for the provided model data
  * @param mixed $model Model to adapt
  * @param \ride\library\http\jsonapi\JsonApiDocument $document Document
  * which is requested
  * @param string $relationshipPath dot-separated list of relationship names
  * @return JsonApiResource|null
  */
 public function getResource($model, JsonApiDocument $document, $relationshipPath = null)
 {
     if ($model === null) {
         return null;
     } elseif (!$model instanceof Model) {
         throw new JsonApiException('Could not get resource: provided data is not an ORM model');
     }
     $query = $document->getQuery();
     $api = $document->getApi();
     $meta = $model->getMeta();
     $id = $meta->getName();
     $resource = $api->createResource($this->type, $meta->getName(), $relationshipPath);
     $resource->setLink('self', $this->web->getUrl('api.orm.model.detail', array('type' => $this->type, 'id' => $id)));
     if ($query->isFieldRequested($this->type, 'name')) {
         $resource->setAttribute('name', $meta->getName());
     }
     if ($query->isFieldRequested($this->type, 'isLocalized')) {
         $resource->setAttribute('isLocalized', $meta->isLocalized() ? true : false);
     }
     if ($query->isFieldRequested($this->type, 'localizedModelName')) {
         $resource->setAttribute('localizedModelName', $meta->getLocalizedModelName());
     }
     if ($query->isFieldRequested($this->type, 'willBlockDeleteWhenUser')) {
         $resource->setAttribute('willBlockDeleteWhenUsed', $meta->willBlockDeleteWhenUsed() ? true : false);
     }
     if ($query->isFieldRequested($this->type, 'entryClassName')) {
         $resource->setAttribute('entryClassName', $meta->getEntryClassName());
     }
     if ($query->isFieldRequested($this->type, 'proxyClassName')) {
         $resource->setAttribute('proxyClassName', $meta->getProxyClassName());
     }
     if ($query->isFieldRequested($this->type, 'modelClassName')) {
         $resource->setAttribute('modelClassName', get_class($model));
     }
     if ($query->isFieldRequested($this->type, 'options')) {
         $resource->setAttribute('options', $meta->getOptions());
     }
     if ($query->isFieldRequested($this->type, 'fields') && $query->isIncluded($relationshipPath)) {
         $adapter = $api->getResourceAdapter('model-fields');
         $value = array();
         $fieldRelationshipPath = ($relationshipPath ? $relationshipPath . '.' : '') . 'fields';
         $fields = $meta->getFields();
         foreach ($fields as $fieldName => $field) {
             $field->model = $model;
             $fieldId = $id . '-' . $fieldName;
             $value[] = $adapter->getResource($field, $document, $fieldRelationshipPath);
         }
         $relationship = $api->createRelationship();
         $relationship->setLink('self', $this->web->getUrl('api.orm.model.relationship', array('type' => $this->type, 'id' => $id, 'relationship' => 'fields')));
         // $relationship->setLink('related', $this->web->getUrl('api.orm.related', array('type' => $this->type, 'id' => $data->getId(), 'relationship' => $fieldName)));
         $relationship->setResourceCollection($value);
         $resource->setRelationship('fields', $relationship);
     }
     return $resource;
 }
 /**
  * Gets a resource instance for the provided model data
  * @param mixed $data Data to adapt
  * @param \ride\library\http\jsonapi\JsonApiDocument $document Document
  * which is requested
  * @param string $relationshipPath dot-separated list of relationship names
  * @return JsonApiResource|null
  */
 public function getResource($data, JsonApiDocument $document, $relationshipPath = null)
 {
     if ($data === null) {
         return null;
     } elseif (!$data instanceof Entry) {
         throw new JsonApiException('Could not get resource: provided data is not an ORM entry');
     }
     $query = $document->getQuery();
     $api = $document->getApi();
     $resource = $api->createResource($this->type, $data->getId(), $relationshipPath);
     $resource->setLink('self', $this->web->getUrl('api.orm.entry.detail', array('type' => $this->type, 'id' => $data->getId())));
     $fields = $this->model->getMeta()->getFields();
     foreach ($fields as $fieldName => $field) {
         if ($fieldName == 'id' || $fieldName == 'type' || !$query->isFieldRequested($this->type, $fieldName)) {
             continue;
         } elseif ($field instanceof PropertyField) {
             $value = $this->reflectionHelper->getProperty($data, $fieldName);
             if (($field->getType() == 'file' || $field->getType() == 'image') && $value) {
                 $dataUri = $this->web->getHttpFactory()->createDataUriFromFile($value);
                 if ($dataUri) {
                     $value = $dataUri->encode();
                 } else {
                     $value = null;
                 }
             }
             $resource->setAttribute($fieldName, $value);
             continue;
         }
         $fieldType = $api->getModelType($field->getRelationModelName());
         if ($fieldType === false || !$query->isIncluded($relationshipPath)) {
             continue;
         }
         $fieldRelationshipPath = ($relationshipPath ? $relationshipPath . '.' : '') . $fieldName;
         $relationship = $api->createRelationship();
         $relationship->setLink('self', $this->web->getUrl('api.orm.entry.relationship', array('type' => $this->type, 'id' => $data->getId(), 'relationship' => $fieldName)));
         $relationship->setLink('related', $this->web->getUrl('api.orm.entry.related', array('type' => $this->type, 'id' => $data->getId(), 'relationship' => $fieldName)));
         $adapter = $api->getResourceAdapter($fieldType);
         $value = $this->reflectionHelper->getProperty($data, $fieldName);
         if ($field instanceof HasManyField) {
             foreach ($value as $id => $entry) {
                 $value[$id] = $adapter->getResource($entry, $document, $fieldRelationshipPath);
             }
             $relationship->setResourceCollection($value);
         } else {
             if ($value) {
                 $relationshipResource = $adapter->getResource($value, $document, $fieldRelationshipPath);
             } else {
                 $relationshipResource = null;
             }
             $relationship->setResource($relationshipResource);
         }
         $resource->setRelationship($fieldName, $relationship);
     }
     return $resource;
 }
 /**
  * Gets a resource instance for the provided parameter
  * @param mixed $parameter Parameter to adapt
  * @param \ride\library\http\jsonapi\JsonApiDocument $document Document
  * which is requested
  * @param string $relationshipPath dot-separated list of relationship names
  * @return JsonApiResource|null
  */
 public function getResource($parameter, JsonApiDocument $document, $relationshipPath = null)
 {
     if ($parameter === null) {
         return null;
     } elseif (!is_array($parameter) || !isset($parameter['key']) || !isset($parameter['value'])) {
         throw new JsonApiException('Could not get resource: provided data is not a parameter');
     }
     $query = $document->getQuery();
     $api = $document->getApi();
     $id = $parameter['key'];
     $resource = $api->createResource($this->type, $id, $relationshipPath);
     $resource->setLink('self', $this->web->getUrl('api.parameters.detail', array('id' => $id)));
     if ($query->isFieldRequested($this->type, 'key')) {
         $resource->setAttribute('key', $parameter['key']);
     }
     if ($query->isFieldRequested($this->type, 'value')) {
         $resource->setAttribute('value', $parameter['value']);
     }
     return $resource;
 }
 /**
  * Gets a resource instance for the provided parameter
  * @param mixed $parameter Parameter to adapt
  * @param \ride\library\http\jsonapi\JsonApiDocument $document Document
  * which is requested
  * @param string $relationshipPath dot-separated list of relationship names
  * @return JsonApiResource|null
  */
 public function getResource($cacheControl, JsonApiDocument $document, $relationshipPath = null)
 {
     if ($cacheControl === null) {
         return null;
     } elseif (!is_array($cacheControl) || !isset($cacheControl['name']) || !isset($cacheControl['control'])) {
         throw new JsonApiException('Could not get resource: provided data is not a cache control');
     }
     $query = $document->getQuery();
     $api = $document->getApi();
     $id = $cacheControl['id'];
     $resource = $api->createResource($this->type, $id, $relationshipPath);
     $resource->setLink('self', $this->web->getUrl('api.caches.detail', array('id' => $id)));
     if ($query->isFieldRequested($this->type, 'name')) {
         $resource->setAttribute('name', $cacheControl['name']);
     }
     if ($query->isFieldRequested($this->type, 'isLocked')) {
         $resource->setAttribute('isLocked', $cacheControl['isLocked']);
     }
     if ($query->isFieldRequested($this->type, 'isEnabled')) {
         $resource->setAttribute('isEnabled', $cacheControl['isEnabled']);
     }
     return $resource;
 }
 /**
  * Gets a resource instance for the provided parameter
  * @param mixed $parameter Parameter to adapt
  * @param \ride\library\http\jsonapi\JsonApiDocument $document Document
  * which is requested
  * @param string $relationshipPath dot-separated list of relationship names
  * @return JsonApiResource|null
  */
 public function getResource($locale, JsonApiDocument $document, $relationshipPath = null)
 {
     if ($locale === null) {
         return null;
     } elseif (!$locale instanceof Locale) {
         throw new JsonApiException('Could not get resource: provided data is not a locale');
     }
     $query = $document->getQuery();
     $api = $document->getApi();
     $id = $locale->getCode();
     $resource = $api->createResource($this->type, $id, $relationshipPath);
     $resource->setLink('self', $this->web->getUrl('api.locales.detail', array('id' => $id)));
     if ($query->isFieldRequested($this->type, 'code')) {
         $resource->setAttribute('code', $locale->getCode());
     }
     if ($query->isFieldRequested($this->type, 'name')) {
         $resource->setAttribute('name', $locale->getName());
     }
     if ($query->isFieldRequested($this->type, 'properties')) {
         $resource->setAttribute('properties', $locale->getProperties());
     }
     return $resource;
 }
 /**
  * Gets a resource instance for the provided model data
  * @param mixed $model Model to adapt
  * @param \ride\library\http\jsonapi\JsonApiDocument $document Document
  * which is requested
  * @param string $relationshipPath dot-separated list of relationship names
  * @return JsonApiResource|null
  */
 public function getResource($field, JsonApiDocument $document, $relationshipPath = null)
 {
     if ($field === null) {
         return null;
     } elseif (!$field instanceof ModelField) {
         throw new JsonApiException('Could not get resource: provided data is not an ORM field');
     } elseif (!isset($field->model)) {
         throw new JsonApiException('Could not get resource: provided field data has no model property');
     }
     $modelName = $field->model->getName();
     $query = $document->getQuery();
     $api = $document->getApi();
     $id = $modelName . '-' . $field->getName();
     $resource = $api->createResource($this->type, $id, $relationshipPath);
     $resource->setLink('self', $this->web->getUrl('api.orm.field.detail', array('type' => $this->type, 'id' => $id)));
     if ($query->isFieldRequested($this->type, 'name')) {
         $resource->setAttribute('name', $field->getName());
     }
     if ($query->isFieldRequested($this->type, 'isLocalized')) {
         $resource->setAttribute('isLocalized', $field->isLocalized() ? true : false);
     }
     if ($query->isFieldRequested($this->type, 'type')) {
         if ($field instanceof HasManyField) {
             $type = 'hasMany';
         } elseif ($field instanceof HasOneField) {
             $type = 'hasOne';
         } elseif ($field instanceof BelongsToField) {
             $type = 'belongsTo';
         } else {
             $type = $field->getType();
         }
         $resource->setAttribute('type', $type);
     }
     if ($query->isFieldRequested($this->type, 'model') && $query->isIncluded($relationshipPath)) {
         if (!$field instanceof RelationField) {
             $relationModel = null;
         } else {
             $adapter = $api->getResourceAdapter('models');
             $fieldRelationshipPath = ($relationshipPath ? $relationshipPath . '.' : '') . 'model';
             $relationModel = $adapter->getResource($this->orm->getModel($field->getRelationModelName()), $document, $fieldRelationshipPath);
         }
         $relationship = $api->createRelationship();
         $relationship->setResource($relationModel);
         $resource->setRelationship('model', $relationship);
     }
     // $fields = $meta->getFields();
     // foreach ($fields as $fieldName => $field) {
     // if ($fieldName == 'id' || $fieldName == 'type' || !$query->isFieldRequested($this->type, $fieldName)) {
     // continue;
     // } elseif ($field instanceof PropertyField) {
     // $resource->setAttribute($fieldName, $this->reflectionHelper->getProperty($data, $fieldName));
     // continue;
     // }
     // $fieldType = $api->getModelType($field->getRelationModelName());
     // if ($fieldType === false || !$query->isIncluded($relationshipPath)) {
     // continue;
     // }
     // $fieldRelationshipPath = ($relationshipPath ? $relationshipPath . '.' : '') . $fieldName;
     // $relationship = $api->createRelationship();
     // $relationship->setLink('self', $this->web->getUrl('api.orm.relationship', array('type' => $this->type, 'id' => $data->getId(), 'relationship' => $fieldName)));
     // $relationship->setLink('related', $this->web->getUrl('api.orm.related', array('type' => $this->type, 'id' => $data->getId(), 'relationship' => $fieldName)));
     // $adapter = $api->getResourceAdapter($fieldType);
     // $value = $this->reflectionHelper->getProperty($data, $fieldName);
     // if ($field instanceof HasManyField) {
     // foreach ($value as $id => $entry) {
     // $value[$id] = $adapter->getResource($entry, $document, $fieldRelationshipPath);
     // }
     // $relationship->setResourceCollection($value);
     // } else {
     // if ($value) {
     // $relationshipResource = $adapter->getResource($value, $document, $fieldRelationshipPath);
     // } else {
     // $relationshipResource = null;
     // }
     // $relationship->setResource($relationshipResource);
     // }
     // $resource->setRelationship($fieldName, $relationship);
     // }
     return $resource;
 }
 public function providerGetJsonValue()
 {
     $document1 = new JsonApiDocument();
     $document1->setMeta('meta1', 'value1');
     $document1->setLink('self', 'http://url');
     $expected1 = array('jsonapi' => array('version' => '1.0'), 'meta' => array('meta1' => 'value1'), 'links' => array('self' => $document1->getLink('self')));
     $error = new JsonApiError();
     $error->setCode('my-error');
     $document2 = new JsonApiDocument();
     $document2->addError($error);
     $expected2 = array('jsonapi' => array('version' => '1.0'), 'errors' => array($error));
     $resource = new JsonApiResource('type', 'id');
     $resource->setAttribute('attribute1', 'value1');
     $resource->setAttribute('attribute2', 'value2');
     $document3 = new JsonApiDocument();
     $document3->setResourceData('type', $resource);
     $expected3 = array('jsonapi' => array('version' => '1.0'), 'data' => $resource->getJsonValue(true));
     $document4 = new JsonApiDocument();
     $document4->setResourceData('type', null);
     $expected4 = array('jsonapi' => array('version' => '1.0'), 'data' => null);
     return array(array($expected1, $document1), array($expected2, $document2), array($expected3, $document3), array($expected4, $document4));
 }