Пример #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
 /**
  * <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 bool $desc A direction in which to order the elements: false for ascending (by increasing value), true for descending (by decreasing value).
  * @param callable $keySelector {(v, k) ==> key} A function to extract a key from an element. Default: value.
  * @param callable $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
  * @return \YaLinqo\OrderedEnumerable
  */
 public function thenByDir($desc, $keySelector = null, $comparer = null)
 {
     $keySelector = Utils::createLambda($keySelector, 'v,k', Functions::$value);
     $comparer = Utils::createLambda($comparer, 'a,b', Functions::$compareStrict);
     return new self($this->source, $desc, $keySelector, $comparer, $this);
 }
Пример #3
0
 /**
  *
  * @param type $closure
  * @param type $closureArgs
  * @param type $default
  * @return type
  */
 public function createLambda($closure, $closureArgs, $default = null)
 {
     return \YaLinqo\Utils::createLambda($closure, $closureArgs, $default);
 }
Пример #4
0
 /**
  * <p><b>Syntax</b>: writeLine ([selector])
  * <p>Output all the sequence values, with a new line after each element.
  * @param callable|null $selector {(v, k) ==> value} A transform function to apply to each element. Default: value.
  * @return string
  * @see echo, PHP_EOL
  */
 public function writeLine($selector = null)
 {
     $selector = Utils::createLambda($selector, 'v,k', Functions::$value);
     foreach ($this as $k => $v) {
         echo call_user_func($selector, $v, $k), PHP_EOL;
     }
 }
Пример #5
0
 /**
  * Generates a sequence by mimicking a for loop.
  * <p><b>Syntax</b>: generate (funcValue {(v, k) ==> value} [, seedValue [, funcKey {(v, k) ==> key} [, seedKey]]])
  * <p>If seedValue is null, the first value will be the result of calling funcValue on seedValue and seedKey. The same applies for seedKey.
  * @param callable $funcValue {(v, k) ==> value} State update function to run on value after every iteration of the generator loop. Default: value.
  * @param mixed $seedValue Initial state of the generator loop for values. Default: null.
  * @param callable|null $funcKey {(v, k) ==> key} State update function to run on key after every iteration of the generator loop. Default: increment.
  * @param mixed $seedKey Initial state of the generator loop ofr keys. Default: 0.
  * @return Enumerable
  * @package YaLinqo\Generation
  */
 public static function generate($funcValue, $seedValue = null, $funcKey = null, $seedKey = null)
 {
     $funcValue = Utils::createLambda($funcValue, 'v,k');
     $funcKey = Utils::createLambda($funcKey, 'v,k', false);
     return new self(function () use($funcValue, $funcKey, $seedValue, $seedKey) {
         $key = $seedKey === null ? $funcKey ? $funcKey($seedValue, $seedKey) : 0 : $seedKey;
         $value = $seedValue === null ? $funcValue($seedValue, $seedKey) : $seedValue;
         (yield $key => $value);
         while (true) {
             list($value, $key) = [$funcValue($value, $key), $funcKey ? $funcKey($value, $key) : $key + 1];
             (yield $key => $value);
         }
     });
 }
Пример #6
0
 /**
  * Returns elements from a sequence as long as a specified condition is true.
  * <p><b>Syntax</b>: takeWhile (predicate {(v, k) ==> result})
  * <p>The takeWhile method tests each element of source by using predicate and yields the element if the result is true. Enumeration stops when the predicate function returns false for an element or when source contains no more elements.
  * <p>The takeWhile and {@link skipWhile} methods are functional complements. Given a sequence coll and a pure function p, concatenating the results of coll->takeWhile(p) and coll->skipWhile(p) yields the same sequence as coll.
  * @param callable $predicate {(v, k) ==> result} A function to test each element for a condition.
  * @return Enumerable A sequence that contains the elements from the input sequence that occur before the element at which the test no longer passes.
  * @package YaLinqo\Pagination
  */
 public function takeWhile($predicate)
 {
     $predicate = Utils::createLambda($predicate, 'v,k');
     return new self(function () use($predicate) {
         foreach ($this as $k => $v) {
             if (!$predicate($v, $k)) {
                 break;
             }
             (yield $k => $v);
         }
     });
 }
Пример #7
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);
 }
Пример #8
0
 /** @covers YaLinqo\Utils::createLambda
  * @covers YaLinqo\Utils::createLambdaFromString
  */
 function testCreateLambda_lambdaString()
 {
     $f = U::createLambda('strcmp', 'a,b');
     $this->assertSame(0, $f('a', 'a'));
     $f = U::createLambda('$val+1', 'val');
     $this->assertSame(3, $f(2));
     $f = U::createLambda('$a+$b', 'a,b');
     $this->assertSame(5, $f(2, 3));
     $f = U::createLambda('$b+$c', 'a,b,c,d');
     $this->assertSame(5, $f(1, 2, 3, 4));
     $this->assertSame(5, $f(1, 2, 3, 4, 5));
     $f = U::createLambda('{ return $val+1; }', 'val');
     $this->assertSame(3, $f(2));
     $f = U::createLambda('{ return $a+$b; }', 'a,b');
     $this->assertSame(5, $f(2, 3));
     $f = U::createLambda('{ return $b+$c; }', 'a,b,c,d');
     $this->assertSame(5, $f(1, 2, 3, 4));
     $this->assertSame(5, $f(1, 2, 3, 4, 5));
     $f = U::createLambda('$val2 ==> $val2+1', 'val');
     $this->assertSame(3, $f(2));
     $f = U::createLambda('($v1, $v2) ==> $v1+$v2', 'a,b');
     $this->assertSame(5, $f(2, 3));
     $f = U::createLambda('($q, $w, $e, $r) ==> $w+$e', 'a,b,c,d');
     $this->assertSame(5, $f(1, 2, 3, 4));
     $this->assertSame(5, $f(1, 2, 3, 4, 5));
     $f = U::createLambda('$val2 ==> { return $val2+1; }', 'val');
     $this->assertSame(3, $f(2));
     $f = U::createLambda('($v1, $v2) ==> { return $v1+$v2; }', 'a,b');
     $this->assertSame(5, $f(2, 3));
     $f = U::createLambda('($q, $w, $e, $r) ==> { return $w+$e; }', 'a,b,c,d');
     $this->assertSame(5, $f(1, 2, 3, 4));
     $this->assertSame(5, $f(1, 2, 3, 4, 5));
 }
Пример #9
0
 /** @covers YaLinqo\Utils::createLambda
  * @covers YaLinqo\Utils::createLambdaFromString
  */
 function testCreateLambda_lambdaString()
 {
     $o = new \Tests\Stubs\Temp(2);
     /** @var $f callback */
     $f = U::createLambda('$val+1', 'val');
     $this->assertSame(3, $f(2));
     $f = U::createLambda('$a+$b', 'a,b');
     $this->assertSame(5, $f(2, 3));
     $f = U::createLambda('$b+$c', 'a,b,c,d');
     $this->assertSame(5, $f(1, 2, 3, 4));
     $this->assertSame(5, $f(1, 2, 3, 4, 5));
     $f = U::createLambda('{ return $val+1; }', 'val');
     $this->assertSame(3, $f(2));
     $f = U::createLambda('{ return $a+$b; }', 'a,b');
     $this->assertSame(5, $f(2, 3));
     $f = U::createLambda('{ return $b+$c; }', 'a,b,c,d');
     $this->assertSame(5, $f(1, 2, 3, 4));
     $this->assertSame(5, $f(1, 2, 3, 4, 5));
     $f = U::createLambda('$val2 ==> $val2+1', 'val');
     $this->assertSame(3, $f(2));
     $f = U::createLambda('($v1, $v2) ==> $v1+$v2', 'a,b');
     $this->assertSame(5, $f(2, 3));
     $f = U::createLambda('($q, $w, $e, $r) ==> $w+$e', 'a,b,c,d');
     $this->assertSame(5, $f(1, 2, 3, 4));
     $this->assertSame(5, $f(1, 2, 3, 4, 5));
     $f = U::createLambda('$val2 ==> { return $val2+1; }', 'val');
     $this->assertSame(3, $f(2));
     $f = U::createLambda('($v1, $v2) ==> { return $v1+$v2; }', 'a,b');
     $this->assertSame(5, $f(2, 3));
     $f = U::createLambda('($q, $w, $e, $r) ==> { return $w+$e; }', 'a,b,c,d');
     $this->assertSame(5, $f(1, 2, 3, 4));
     $this->assertSame(5, $f(1, 2, 3, 4, 5));
 }
Пример #10
0
<?php

/**
 * Global functions and initialization.
 * @author Alexander Prokhorov
 * @license Simplified BSD
 * @link https://github.com/Athari/YaLinqo YaLinqo on GitHub
 */
\YaLinqo\Functions::init();
\YaLinqo\Utils::init();
if (!function_exists('from')) {
    /**
     * Create Enumerable from an array or any other traversible source.
     * @param array|\Iterator|\IteratorAggregate|\YaLinqo\Enumerable $source
     * @throws \InvalidArgumentException If source is not array or Traversible or Enumerable.
     * @return \YaLinqo\Enumerable
     * @see \YaLinqo\Enumerable::from
     */
    function from($source)
    {
        return \YaLinqo\Enumerable::from($source);
    }
}