Exemplo n.º 1
0
 protected function checkForIncludes($source, JsonApiRootNode $jsonApiRootNode, $relationshipNamesToInclude)
 {
     if (!$source->isEmpty) {
         $modelClassName = $this->modelClassName;
         $fetchedCollections = array();
         // we will cache collections here, so we don't get duplicate data to include when multiple relationships point to the same object
         foreach ($relationshipNamesToInclude as $relationshipNameToInclude) {
             if ($modelClassName::hasRelationship($relationshipNameToInclude)) {
                 // first of all, we fetch the data
                 $source->fetch($relationshipNameToInclude);
                 $fetchedObject = $source->get($relationshipNameToInclude);
                 // We don't know yet if this is a Collection or a Model we just fetched,
                 // as the source we fetched it from can be both as well
                 if ($fetchedObject instanceof Collection) {
                     $fetchedCollection = $fetchedObject;
                     if (!$fetchedCollection->isEmpty) {
                         // next we get the type of the data we fetched
                         $relationship = $modelClassName::getRelationship($relationshipNameToInclude);
                         $relationshipType = $relationship->modelType;
                         // Here we check if we already fetched data of the same type
                         if (array_key_exists($relationshipType, $fetchedCollections)) {
                             // we already fetched data of the same type before, lets merge it with the data we have, so we don't create duplicates in the response
                             // luckally for us, collection->put() handles duplicates by checking for id
                             $previouslyFetchedCollection = $fetchedCollections[$relationshipType];
                             foreach ($fetchedCollection as $model) {
                                 $previouslyFetchedCollection->put($model);
                             }
                         } else {
                             // we haven't fetched this type yet, lets cache it in case we do later
                             $fetchedCollections[$relationshipType] = $fetchedCollection;
                         }
                     }
                 } else {
                     if ($fetchedObject instanceof Model) {
                         $fetchedModel = $fetchedObject;
                         if (!empty($fetchedModel)) {
                             // next we get the type of the data we fetched
                             $relationship = $modelClassName::getRelationship($relationshipNameToInclude);
                             $relationshipType = $relationship->modelType;
                             // now we check if we already included objects of the same type
                             if (!array_key_exists($relationshipType, $fetchedCollections)) {
                                 // we haven't fetched this type yet, lets cache it in case we do later
                                 $fetchedCollections[$relationshipType] = Collection::forge($relationshipType);
                             }
                             $previouslyFetchedCollection = $fetchedCollections[$relationshipType];
                             $previouslyFetchedCollection->put($fetchedModel);
                         }
                     }
                 }
             }
         }
         // now that we cached the collections, it's just a matter of looping them, and including the data in our response
         foreach ($fetchedCollections as $fetchedCollection) {
             if (!$fetchedCollection->isEmpty) {
                 $serializedCollection = $fetchedCollection->getJsonApiNode();
                 $jsonApiRootNode->addInclude($serializedCollection);
             }
         }
     }
 }
Exemplo n.º 2
0
 public function fetchCollection()
 {
     $entities = $this->fetch();
     return Collection::forge($this->modelType, null, $entities);
 }