public function select($selector, $selectKey = false)
 {
     if ($this->isConverted || $this->data instanceof Collection === false) {
         return parent::select($selector, $selectKey);
     }
     if ($selectKey !== false && $selectKey !== null) {
         if (\Sledgehammer\is_closure($selectKey) || empty($this->options['mapping'][$selectKey])) {
             // Key not in the mapping array?
             return parent::select($selector, $selectKey);
         }
         if ($this->hasReadFilter($selectKey)) {
             // Key requires a readFilter?
             return parent::select($selector, $selectKey);
         }
         $selectKeyMapped = $this->options['mapping'][$selectKey];
     } else {
         $selectKeyMapped = $selectKey;
     }
     if (is_string($selector) && isset($this->options['mapping'][$selector])) {
         if ($this->hasReadFilter($selector)) {
             return parent::select($selector, $selectKey);
         }
         // Bypass Repository and return the resultset directly from the backend-collection.
         return $this->data->select($this->options['mapping'][$selector], $selectKeyMapped);
     } elseif (is_array($selector)) {
         $selectorMapped = [];
         foreach ($selector as $to => $from) {
             if (empty($this->options['mapping'][$from])) {
                 // Field not in the mapping array?
                 return parent::select($selector, $selectKey);
             }
             if ($this->hasReadFilter($from)) {
                 return parent::select($selector, $selectKey);
             }
             $selectorMapped[$to] = $this->options['mapping'][$from];
         }
         // Bypass Repository and return the resultset directly from the backend-collection.
         return $this->data->select($selectorMapped, $selectKeyMapped);
     }
     // Closure selector
     return parent::select($selector, $selectKey);
 }
Exemple #2
0
 /**
  * Register closures that lazily configure the default repository.
  * 
  * @param Closure $closure A closure that received the repository as the first argument
  */
 public static function configureDefault($closure)
 {
     if (\Sledgehammer\is_closure($closure) === false) {
         throw new Exception('Closure expected as first argument');
     }
     if (static::$lazyConfigurations === false) {
         // default repository already initialized
         $closure(Repository::instance());
     } else {
         static::$lazyConfigurations[] = $closure;
     }
 }
 public function test_compile()
 {
     $closure = PropertyPath::compile('id');
     $item = array('id' => 8);
     $this->assertTrue(\Sledgehammer\is_closure($closure), 'compile() should return a closure');
     $this->assertSame(8, $closure($item));
 }
Exemple #4
0
 /**
  * Return a new collection sorted by the given field in ascending order.
  *
  * @param string|Closure $selector
  * @param int            $method   The sorting method, options are: SORT_REGULAR, SORT_NUMERIC, SORT_STRING or SORT_NATURAL
  *
  * @return Collection
  */
 public function orderBy($selector, $method = SORT_REGULAR)
 {
     $sortOrder = [];
     $items = [];
     $indexed = true;
     $counter = 0;
     if (\Sledgehammer\is_closure($selector)) {
         $closure = $selector;
     } else {
         $closure = PropertyPath::compile($selector);
     }
     // Collect values
     foreach ($this as $key => $item) {
         $items[$key] = $item;
         $sortOrder[$key] = $closure($item, $key);
         if ($key !== $counter) {
             $indexed = false;
         }
         ++$counter;
     }
     // Sort the values
     if ($method === SORT_NATURAL) {
         natsort($sortOrder);
     } elseif ($method === \Sledgehammer\SORT_NATURAL_CI) {
         natcasesort($sortOrder);
     } else {
         asort($sortOrder, $method);
     }
     $sorted = [];
     foreach (array_keys($sortOrder) as $key) {
         if ($indexed) {
             $sorted[] = $items[$key];
         } else {
             // Keep keys intact
             $sorted[$key] = $items[$key];
         }
     }
     return new self($sorted);
 }