vendor/symfony/finder/Iterator/SortableIterator.php line 82

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Finder\Iterator;
  11. /**
  12.  * SortableIterator applies a sort on a given Iterator.
  13.  *
  14.  * @author Fabien Potencier <fabien@symfony.com>
  15.  */
  16. class SortableIterator implements \IteratorAggregate
  17. {
  18.     const SORT_BY_NONE 0;
  19.     const SORT_BY_NAME 1;
  20.     const SORT_BY_TYPE 2;
  21.     const SORT_BY_ACCESSED_TIME 3;
  22.     const SORT_BY_CHANGED_TIME 4;
  23.     const SORT_BY_MODIFIED_TIME 5;
  24.     const SORT_BY_NAME_NATURAL 6;
  25.     private $iterator;
  26.     private $sort;
  27.     /**
  28.      * @param \Traversable $iterator The Iterator to filter
  29.      * @param int|callable $sort     The sort type (SORT_BY_NAME, SORT_BY_TYPE, or a PHP callback)
  30.      *
  31.      * @throws \InvalidArgumentException
  32.      */
  33.     public function __construct(\Traversable $iterator$sortbool $reverseOrder false)
  34.     {
  35.         $this->iterator $iterator;
  36.         $order $reverseOrder ? -1;
  37.         if (self::SORT_BY_NAME === $sort) {
  38.             $this->sort = static function ($a$b) use ($order) {
  39.                 return $order strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
  40.             };
  41.         } elseif (self::SORT_BY_NAME_NATURAL === $sort) {
  42.             $this->sort = static function ($a$b) use ($order) {
  43.                 return $order strnatcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
  44.             };
  45.         } elseif (self::SORT_BY_TYPE === $sort) {
  46.             $this->sort = static function ($a$b) use ($order) {
  47.                 if ($a->isDir() && $b->isFile()) {
  48.                     return -$order;
  49.                 } elseif ($a->isFile() && $b->isDir()) {
  50.                     return $order;
  51.                 }
  52.                 return $order strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
  53.             };
  54.         } elseif (self::SORT_BY_ACCESSED_TIME === $sort) {
  55.             $this->sort = static function ($a$b) use ($order) {
  56.                 return $order * ($a->getATime() - $b->getATime());
  57.             };
  58.         } elseif (self::SORT_BY_CHANGED_TIME === $sort) {
  59.             $this->sort = static function ($a$b) use ($order) {
  60.                 return $order * ($a->getCTime() - $b->getCTime());
  61.             };
  62.         } elseif (self::SORT_BY_MODIFIED_TIME === $sort) {
  63.             $this->sort = static function ($a$b) use ($order) {
  64.                 return $order * ($a->getMTime() - $b->getMTime());
  65.             };
  66.         } elseif (self::SORT_BY_NONE === $sort) {
  67.             $this->sort $order;
  68.         } elseif (\is_callable($sort)) {
  69.             $this->sort $reverseOrder ? static function ($a$b) use ($sort) { return -$sort($a$b); } : $sort;
  70.         } else {
  71.             throw new \InvalidArgumentException('The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.');
  72.         }
  73.     }
  74.     public function getIterator()
  75.     {
  76.         if (=== $this->sort) {
  77.             return $this->iterator;
  78.         }
  79.         $array iterator_to_array($this->iteratortrue);
  80.         if (-=== $this->sort) {
  81.             $array array_reverse($array);
  82.         } else {
  83.             uasort($array$this->sort);
  84.         }
  85.         return new \ArrayIterator($array);
  86.     }
  87. }