コード例 #1
0
ファイル: Utility.php プロジェクト: esbenp/architect
 /**
  * Set a property of an Eloquent model, normal object or array
  * @param mixed $objectOrArray model, object or array
  * @param string $property
  * @param void
  */
 public static function setProperty(&$objectOrArray, $property, $value)
 {
     // Eloquent models are also instances of ArrayAccess, and therefore
     // we check for that first
     if ($objectOrArray instanceof EloquentModel) {
         // Does relation exist?
         // If so, only set the relation if not primitive. Keeping a attribute
         // as a relation will allow for it to be converted to arrays during
         // serialization
         if ($property) {
             if ($objectOrArray->relationLoaded($property) && !Utility::isPrimitive($value)) {
                 $objectOrArray->setRelation($property, $value);
                 // If attribute is not a relation we just set it on
                 // the model directly. If it is a primitive relation (a relation
                 // converted to IDs) we unset the relation and set it as an attribute
             } else {
                 unset($objectOrArray[$property]);
                 $objectOrArray->setAttribute($property, $value);
             }
         }
     } elseif (is_array($objectOrArray)) {
         $objectOrArray[$property] = $value;
     } else {
         $objectOrArray->{$property} = $value;
     }
 }
コード例 #2
0
ファイル: IdsModeResolver.php プロジェクト: esbenp/architect
 /**
  * Map through the collection and convert it to a collection
  * of ids
  * @param  string $property
  * @param  object $object
  * @param  array $root
  * @param  string $fullPropertyPath
  * @return mixed
  */
 public function resolve($property, &$object, &$root, $fullPropertyPath)
 {
     if (is_array($object)) {
         // We need to determine if this is a singular relationship or
         // a collection of models
         $arrayCopy = $object;
         $firstElement = array_shift($arrayCopy);
         // The object was not a collection, and was rather a single
         // model, because the first item returned was a property
         // We therefore just return the single ID
         if (Utility::isPrimitive($firstElement)) {
             return (int) Utility::getProperty($object, 'id');
         }
         return array_map(function ($entry) {
             return (int) Utility::getProperty($entry, 'id');
         }, $object);
     } elseif ($object instanceof Collection) {
         return $object->map(function ($entry) {
             return (int) Utility::getProperty($entry, 'id');
         });
         // The relation is not a collection, but rather
         // a singular relation
     } elseif ($object instanceof Model) {
         return $object->id;
     }
 }
コード例 #3
0
 /**
  * Add the collection to the root array
  * @param array $root
  * @param object $object
  * @param string $fullPropertyPath
  * @return void
  */
 private function addCollectionToRoot(&$root, &$object, $fullPropertyPath)
 {
     // First determine if the $object is a resource or a
     // collection of resources
     $isResource = false;
     if (is_array($object)) {
         $copy = $object;
         $values = array_values($copy);
         $firstPropertyOrResource = array_shift($values);
         if (Utility::isPrimitive($firstPropertyOrResource)) {
             $isResource = true;
         }
     } elseif ($object instanceof EloquentModel) {
         $isResource = true;
     }
     $newCollection = $isResource ? [$object] : $object;
     // Does existing collections use arrays or Collections
     $copy = $root;
     $values = array_values($copy);
     $existingRootCollection = array_shift($values);
     $newCollection = $existingRootCollection instanceof Collection ? new Collection($newCollection) : $newCollection;
     if (!array_key_exists($fullPropertyPath, $root)) {
         $root[$fullPropertyPath] = $newCollection;
     } else {
         $this->mergeRootCollection($root[$fullPropertyPath], $newCollection);
     }
 }