createComparer() public static method

Convert string lambda or SORT_ flags to callable function. Sets isReversed to false if descending is reversed.
public static createComparer ( callable | integer | null $closure, integer $sortOrder, boolean &$isReversed ) : callable | string | null
$closure callable | integer | null
$sortOrder integer
$isReversed boolean
return callable | string | null
示例#1
0
 /**
  * <p><b>Syntax</b>: thenByDir (false|true [, {{(v, k) ==> key} [, {{(a, b) ==> diff}]])
  * <p>Performs a subsequent ordering of elements in a sequence in a particular direction (ascending, descending) according to a key.
  * <p>Three methods are defined to extend the type OrderedEnumerable, which is the return type of this method. These three methods, namely {@link thenBy}, {@link thenByDescending} and {@link thenByDir}, enable you to specify additional sort criteria to sort a sequence. These methods also return an OrderedEnumerable, which means any number of consecutive calls to thenBy, thenByDescending or thenByDir can be made.
  * <p>Because OrderedEnumerable inherits from {@link Enumerable}, you can call {@link Enumerable::orderBy orderBy}, {@link Enumerable::orderByDescending orderByDescending} or {@link Enumerable::orderByDir orderByDir} on the results of a call to orderBy, orderByDescending, orderByDir, thenBy, thenByDescending or thenByDir. Doing this introduces a new primary ordering that ignores the previously established ordering.
  * <p>This method performs an unstable sort; that is, if the keys of two elements are equal, the order of the elements is not preserved. In contrast, a stable sort preserves the order of elements that have the same key. Internally, {@link usort} is used.
  * @param int|bool $sortOrder A direction in which to order the elements: false or SORT_DESC for ascending (by increasing value), true or SORT_ASC for descending (by decreasing value).
  * @param callable|null $keySelector {(v, k) ==> key} A function to extract a key from an element. Default: value.
  * @param callable|int|null $comparer {(a, b) ==> diff} Difference between a and b: &lt;0 if a&lt;b; 0 if a==b; &gt;0 if a&gt;b. Can also be a combination of SORT_ flags.
  * @return \YaLinqo\OrderedEnumerable
  */
 public function thenByDir($sortOrder, $keySelector = null, $comparer = null)
 {
     $sortFlags = Utils::lambdaToSortFlagsAndOrder($comparer, $sortOrder);
     $keySelector = Utils::createLambda($keySelector, 'v,k', Functions::$value);
     $isReversed = $sortOrder == SORT_DESC;
     $comparer = Utils::createComparer($comparer, $sortOrder, $isReversed);
     return new self($this->source, $sortOrder, $sortFlags, $isReversed, $keySelector, $comparer, $this);
 }
示例#2
0
 /** @covers YaLinqo\Utils::createComparer
  */
 function testCreateComparer_sortFlags_invalid()
 {
     $this->setExpectedException('\\InvalidArgumentException');
     $isReversed = null;
     U::createComparer(666, SORT_ASC, $isReversed);
 }
示例#3
0
 /**
  * Sorts the elements of a sequence in a particular direction (ascending, descending) according to a key.
  * <p><b>Syntax</b>: orderByDir (false|true [, {(v, k) ==> key} [, {(a, b) ==> diff}]])
  * <p>Three methods are defined to extend the type {@link OrderedEnumerable}, which is the return type of this method. These three methods, namely {@link OrderedEnumerable::thenBy thenBy}, {@link OrderedEnumerable::thenByDescending thenByDescending} and {@link OrderedEnumerable::thenByDir thenByDir}, enable you to specify additional sort criteria to sort a sequence. These methods also return an OrderedEnumerable, which means any number of consecutive calls to thenBy, thenByDescending or thenByDir can be made.
  * <p>Because OrderedEnumerable inherits from Enumerable, you can call {@link orderBy}, {@link orderByDescending} or {@link orderByDir} on the results of a call to orderBy, orderByDescending, orderByDir, thenBy, thenByDescending or thenByDir. Doing this introduces a new primary ordering that ignores the previously established ordering.
  * <p>This method performs an unstable sort; that is, if the keys of two elements are equal, the order of the elements is not preserved. In contrast, a stable sort preserves the order of elements that have the same key. Internally, {@link usort} is used.
  * @param int|bool $sortOrder A direction in which to order the elements: false or SORT_DESC for ascending (by increasing value), true or SORT_ASC for descending (by decreasing value).
  * @param callable|null $keySelector {(v, k) ==> key} A function to extract a key from an element. Default: value.
  * @param callable|int|null $comparer {(a, b) ==> diff} Difference between a and b: &lt;0 if a&lt;b; 0 if a==b; &gt;0 if a&gt;b. Can also be a combination of SORT_ flags.
  * @return OrderedEnumerable
  * @package YaLinqo\Ordering
  */
 public function orderByDir($sortOrder, $keySelector = null, $comparer = null)
 {
     $sortFlags = Utils::lambdaToSortFlagsAndOrder($comparer, $sortOrder);
     $keySelector = Utils::createLambda($keySelector, 'v,k', Functions::$value);
     $isReversed = $sortOrder == SORT_DESC;
     $comparer = Utils::createComparer($comparer, $sortOrder, $isReversed);
     return new OrderedEnumerable($this, $sortOrder, $sortFlags, $isReversed, $keySelector, $comparer);
 }