[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/finder/Iterator/ -> SortableIterator.php (source)

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


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1