/**
  * Test findOne.
  */
 public function testFindOne()
 {
     $this->given($emptyDataSource = $this->emptyDataSource())->when($findResult = $emptyDataSource->findOne())->then->variable($findResult)->isNull();
     $this->given($randomDataSource = $this->randomDataSource())->when($findResult = $randomDataSource->findOne())->then->variable($findResult)->isNotNull();
     $this->given($datasource = $this->randomDataSource(Criteria::method('value')->gt(2), null, 1, 3))->when($findResult = $datasource->findOne())->then->integer($findResult->value())->isGreaterThan(2);
     $this->given($comparator = new Comparator(), $sortCriteria = new CallbackComparator(function ($a, $b) use($comparator) {
         return -1 * $comparator->compare($a, $b);
     }), $datasource = $this->randomDataSource(Criteria::method('value')->lt(10), $sortCriteria))->when($findResult = $datasource->findOne())->then->integer($findResult->value())->isEqualTo(9);
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function sort(ComparatorInterface $criteria = null)
 {
     $elements = $this->collection->toArray();
     if ($criteria === null) {
         $criteria = new Comparator();
     }
     usort($elements, function ($a, $b) use($criteria) {
         return $criteria->compare($a, $b);
     });
     $this->clear();
     foreach ($elements as $element) {
         $this->add($element);
     }
 }
Esempio n. 3
0
 /**
  * InMemoryMigrationStore constructor.
  */
 public function __construct()
 {
     $this->store = new SortedArrayHashMap([], Comparator::from(function ($a, $b) {
         // order desc
         return -1 * Version::fromString($a)->compareTo(Version::fromString($b));
     }));
 }
Esempio n. 4
0
 /**
  * @param callable|string $selector
  * @param Direction       $direction
  *
  * @return \Cubiche\Core\Comparable\ComparatorInterface
  */
 public static function by($selector, Direction $direction = null)
 {
     if (\is_string($selector)) {
         $selector = new Property($selector);
     }
     return Comparator::by($selector, $direction);
 }
Esempio n. 5
0
 /**
  * MigrationManager constructor.
  *
  * @param MigrationStoreInterface $migrationStore
  * @param string                  $migrationsDirectory
  */
 public function __construct(MigrationStoreInterface $migrationStore, $migrationsDirectory)
 {
     $this->migrationStore = $migrationStore;
     $this->migrationsDirectory = $migrationsDirectory;
     $this->migrationsInFile = new SortedArrayHashMap([], Comparator::from(function ($v1, $v2) {
         return Version::fromString($v1)->compareTo(Version::fromString($v2));
     }));
 }
 /**
  * @param \Iterator $iterator
  * @param callable  $comparator
  *
  * @return \ArrayIterator
  */
 protected function sortIterator(\Iterator $iterator, callable $comparator = null)
 {
     $array = \iterator_to_array($iterator, true);
     \uasort($array, $comparator === null ? Comparator::defaultComparator() : $comparator);
     return new \ArrayIterator($array);
 }
Esempio n. 7
0
 /**
  * {@inheritdoc}
  */
 public function sort(ComparatorInterface $criteria = null)
 {
     if ($criteria === null) {
         $criteria = new Comparator();
     }
     uksort($this->elements, function ($a, $b) use($criteria) {
         return $criteria->compare($a, $b);
     });
 }
Esempio n. 8
0
 /**
  * {@inheritdoc}
  */
 protected function comparator()
 {
     return Comparator::by(Criteria::property('age'));
 }
Esempio n. 9
0
 public function testCreate()
 {
     $this->given($comparator = new Comparator())->and($collection = new SortedArrayList($this->randomValues(10)))->and($reverseComparator = $comparator->reverse())->then()->list($collection)->isSortedUsing($comparator)->isNotSortedUsing($reverseComparator);
 }
Esempio n. 10
0
 /**
  * @param array|\Traversable $enumerable
  * @param callable           $comparator
  */
 public function __construct($enumerable, callable $comparator = null)
 {
     parent::__construct($enumerable);
     $this->comparator = Comparator::ensure($comparator);
     $this->sortedIterator = null;
 }
Esempio n. 11
0
 /**
  * {@inheritdoc}
  */
 public function compare($a, $b)
 {
     return $this->direction()->value() * parent::compare($this->selector->__invoke($a), $this->selector->__invoke($b));
 }
Esempio n. 12
0
 /**
  * Test by method.
  */
 public function testBy()
 {
     $this->given($selector = function ($value) {
         return $value['foo'];
     })->when($comparator = Comparator::by($selector))->then()->object($comparator)->isInstanceOf(SelectorComparator::class)->object($comparator->selector())->isIdenticalTo($selector)->object($comparator->direction())->isEqualTo(Direction::ASC())->when($comparator = Comparator::by($selector, Direction::DESC()))->then()->object($comparator->direction())->isEqualTo(Direction::DESC());
 }
Esempio n. 13
0
 /**
  * @param callable|mixed $firstSelector
  * @param callable|mixed $secondSelector
  * @param callable       $comparator
  */
 public function __construct($firstSelector, $secondSelector, callable $comparator = null)
 {
     parent::__construct($firstSelector, $secondSelector);
     $this->comparator = Comparator::ensure($comparator);
 }