YaLinqo\Utils::createLambda PHP 메소드

createLambda() 공개 정적인 메소드

Convert string lambda to callable function. If callable is passed, return as is.
public static createLambda ( callable | null $closure, string $closureArgs, Closure | callable | null $default = null ) : callable | null
$closure callable | null
$closureArgs string
$default Closure | callable | null
리턴 callable | null
    public static function createLambda($closure, $closureArgs, $default = null)
    {
        if ($closure === null) {
            if ($default === null) {
                throw new \InvalidArgumentException(self::ERROR_CLOSURE_NULL);
            }
            return $default;
        }
        if ($closure instanceof \Closure) {
            return $closure;
        }
        if (is_string($closure) && ($function = self::createLambdaFromString($closure, $closureArgs))) {
            return $function;
        }
        if (is_callable($closure)) {
            return $closure;
        }
        throw new \InvalidArgumentException(self::ERROR_CLOSURE_NOT_CALLABLE);
    }

Usage Example

예제 #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);
 }
All Usage Examples Of YaLinqo\Utils::createLambda