Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • CustomFilterIterator
  • DateRangeFilterIterator
  • DepthRangeFilterIterator
  • ExcludeDirectoryFilterIterator
  • FilecontentFilterIterator
  • FilenameFilterIterator
  • FilePathsIterator
  • FileTypeFilterIterator
  • FilterIterator
  • MultiplePcreFilterIterator
  • PathFilterIterator
  • RecursiveDirectoryIterator
  • SizeRangeFilterIterator
  • SortableIterator
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
 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:      * Constructor.
32:      *
33:      * @param \Traversable     $iterator The Iterator to filter
34:      * @param integer|callback $sort     The sort type (SORT_BY_NAME, SORT_BY_TYPE, or a PHP callback)
35:      *
36:      * @throws \InvalidArgumentException
37:      */
38:     public function __construct(\Traversable $iterator, $sort)
39:     {
40:         $this->iterator = $iterator;
41: 
42:         if (self::SORT_BY_NAME === $sort) {
43:             $this->sort = function ($a, $b) {
44:                 return strcmp($a->getRealpath(), $b->getRealpath());
45:             };
46:         } elseif (self::SORT_BY_TYPE === $sort) {
47:             $this->sort = function ($a, $b) {
48:                 if ($a->isDir() && $b->isFile()) {
49:                     return -1;
50:                 } elseif ($a->isFile() && $b->isDir()) {
51:                     return 1;
52:                 }
53: 
54:                 return strcmp($a->getRealpath(), $b->getRealpath());
55:             };
56:         } elseif (self::SORT_BY_ACCESSED_TIME === $sort) {
57:             $this->sort = function ($a, $b) {
58:                 return ($a->getATime() > $b->getATime());
59:             };
60:         } elseif (self::SORT_BY_CHANGED_TIME === $sort) {
61:             $this->sort = function ($a, $b) {
62:                 return ($a->getCTime() > $b->getCTime());
63:             };
64:         } elseif (self::SORT_BY_MODIFIED_TIME === $sort) {
65:             $this->sort = function ($a, $b) {
66:                 return ($a->getMTime() > $b->getMTime());
67:             };
68:         } elseif (is_callable($sort)) {
69:             $this->sort = $sort;
70:         } else {
71:             throw new \InvalidArgumentException('The SortableIterator takes a PHP callback or a valid built-in sort algorithm as an argument.');
72:         }
73:     }
74: 
75:     public function getIterator()
76:     {
77:         $array = iterator_to_array($this->iterator, true);
78:         uasort($array, $this->sort);
79: 
80:         return new \ArrayIterator($array);
81:     }
82: }
83: 
php-coveralls API documentation generated by ApiGen 2.8.0