You are here

public static function ClosureExpressionVisitor::sortByField in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/doctrine/collections/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php \Doctrine\Common\Collections\Expr\ClosureExpressionVisitor::sortByField()

Helper for sorting arrays of objects based on multiple fields + orientations.

Parameters

string $name:

int $orientation:

\Closure $next:

Return value

\Closure

4 calls to ClosureExpressionVisitor::sortByField()
ArrayCollection::matching in vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php
Selects all elements from a selectable that match the expression and returns a new collection containing these elements.
ClosureExpressionVisitorTest::testSortByFieldAscending in vendor/doctrine/collections/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php
ClosureExpressionVisitorTest::testSortByFieldDescending in vendor/doctrine/collections/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php
ClosureExpressionVisitorTest::testSortDelegate in vendor/doctrine/collections/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php

File

vendor/doctrine/collections/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php, line 84

Class

ClosureExpressionVisitor
Walks an expression graph and turns it into a PHP closure.

Namespace

Doctrine\Common\Collections\Expr

Code

public static function sortByField($name, $orientation = 1, \Closure $next = null) {
  if (!$next) {
    $next = function () {
      return 0;
    };
  }
  return function ($a, $b) use ($name, $next, $orientation) {
    $aValue = ClosureExpressionVisitor::getObjectFieldValue($a, $name);
    $bValue = ClosureExpressionVisitor::getObjectFieldValue($b, $name);
    if ($aValue === $bValue) {
      return $next($a, $b);
    }
    return ($aValue > $bValue ? 1 : -1) * $orientation;
  };
}