Example #1
0
 /**
  * Push `PhotoResult` item to an array/iterator
  *
  * @param array|\Iterator $haystack List of items
  * @param array $keys Key names containing photo ID
  * @param callable $callback (Optional) Callback to use for building
  * items to push into the array
  * @param array $options Photo options
  * @see build()
  * @throws \InvalidArgumentException
  */
 public function push(&$haystack, array $keys = array(), \Closure $callback = null, array $options = array())
 {
     if (!is_array($haystack)) {
         throw new \InvalidArgumentException(sprintf('Argument 1 passed to %s must be an array', __METHOD__));
     }
     // Generate an array of index that will be pushed to the original array.
     // If no key is set, by convention, we look for `photo_id`
     $keys = empty($keys) ? array('photo_id' => 'photo') : $keys;
     foreach ($keys as $index => $name) {
         if (is_int($index)) {
             unset($keys[$index]);
             $keys[$name] = substr($name, 0, -3);
             // Remove '_id'
         }
     }
     if ($callback == null) {
         $callback = function (&$item, $photo, $name) use($keys) {
             $item[$name] = $photo;
             return $item;
         };
     }
     if (array_values($haystack) === $haystack) {
         // This array is a list
         foreach ($keys as $index => $name) {
             // Get list of photo ids
             $ids = ArrayUtils::arrayColumn($haystack, $index);
             $photos = $this->collection($ids, $options);
             foreach ($haystack as $key => $item) {
                 $callback($haystack[$key], $photos->get($key), $name, $index);
             }
         }
     } else {
         foreach ($keys as $index => $name) {
             $photo = $this->get($haystack[$index], $options);
             $callback($haystack, $photo, $name, $index);
         }
     }
 }