lambdaToSortFlagsAndOrder() public static method

Convert string lambda to SORT_ flags. Convert sortOrder from bool to SORT_ order.
public static lambdaToSortFlagsAndOrder ( callable | string | integer | null $closure, integer | boolean &$sortOrder ) : callable | string | integer | null
$closure callable | string | integer | null
$sortOrder integer | boolean
return callable | string | integer | null
Ejemplo n.º 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);
 }
Ejemplo n.º 2
0
 /** @covers YaLinqo\Utils::lambdaToSortFlagsAndOrder
  */
 function testLambdaToSortFlagsAndOrder_sortOrder()
 {
     $order = false;
     U::lambdaToSortFlagsAndOrder(null, $order);
     $this->assertSame(SORT_ASC, $order);
     $order = true;
     U::lambdaToSortFlagsAndOrder(null, $order);
     $this->assertSame(SORT_DESC, $order);
     $order = SORT_ASC;
     U::lambdaToSortFlagsAndOrder(null, $order);
     $this->assertSame(SORT_ASC, $order);
     $order = SORT_DESC;
     U::lambdaToSortFlagsAndOrder(null, $order);
     $this->assertSame(SORT_DESC, $order);
 }
Ejemplo n.º 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);
 }