Mercurial > hg > cmmr2012-drupal-site
diff vendor/sebastian/comparator/src/Factory.php @ 2:5311817fb629
Theme updates
author | Chris Cannam |
---|---|
date | Tue, 10 Jul 2018 13:19:18 +0000 |
parents | c75dbcec494b |
children |
line wrap: on
line diff
--- a/vendor/sebastian/comparator/src/Factory.php Thu Jul 05 15:32:06 2018 +0100 +++ b/vendor/sebastian/comparator/src/Factory.php Tue Jul 10 13:19:18 2018 +0000 @@ -1,6 +1,6 @@ <?php /* - * This file is part of the Comparator package. + * This file is part of sebastian/comparator. * * (c) Sebastian Bergmann <sebastian@phpunit.de> * @@ -18,7 +18,12 @@ /** * @var Comparator[] */ - private $comparators = array(); + private $customComparators = []; + + /** + * @var Comparator[] + */ + private $defaultComparators = []; /** * @var Factory @@ -26,25 +31,6 @@ private static $instance; /** - * Constructs a new factory. - */ - public function __construct() - { - $this->register(new TypeComparator); - $this->register(new ScalarComparator); - $this->register(new NumericComparator); - $this->register(new DoubleComparator); - $this->register(new ArrayComparator); - $this->register(new ResourceComparator); - $this->register(new ObjectComparator); - $this->register(new ExceptionComparator); - $this->register(new SplObjectStorageComparator); - $this->register(new DOMNodeComparator); - $this->register(new MockObjectComparator); - $this->register(new DateTimeComparator); - } - - /** * @return Factory */ public static function getInstance() @@ -57,15 +43,30 @@ } /** + * Constructs a new factory. + */ + public function __construct() + { + $this->registerDefaultComparators(); + } + + /** * Returns the correct comparator for comparing two values. * - * @param mixed $expected The first value to compare - * @param mixed $actual The second value to compare + * @param mixed $expected The first value to compare + * @param mixed $actual The second value to compare + * * @return Comparator */ public function getComparatorFor($expected, $actual) { - foreach ($this->comparators as $comparator) { + foreach ($this->customComparators as $comparator) { + if ($comparator->accepts($expected, $actual)) { + return $comparator; + } + } + + foreach ($this->defaultComparators as $comparator) { if ($comparator->accepts($expected, $actual)) { return $comparator; } @@ -75,16 +76,16 @@ /** * Registers a new comparator. * - * This comparator will be returned by getInstance() if its accept() method + * This comparator will be returned by getComparatorFor() if its accept() method * returns TRUE for the compared values. It has higher priority than the - * existing comparators, meaning that its accept() method will be tested + * existing comparators, meaning that its accept() method will be invoked * before those of the other comparators. * - * @param Comparator $comparator The registered comparator + * @param Comparator $comparator The comparator to be registered */ public function register(Comparator $comparator) { - array_unshift($this->comparators, $comparator); + \array_unshift($this->customComparators, $comparator); $comparator->setFactory($this); } @@ -92,16 +93,47 @@ /** * Unregisters a comparator. * - * This comparator will no longer be returned by getInstance(). + * This comparator will no longer be considered by getComparatorFor(). * - * @param Comparator $comparator The unregistered comparator + * @param Comparator $comparator The comparator to be unregistered */ public function unregister(Comparator $comparator) { - foreach ($this->comparators as $key => $_comparator) { + foreach ($this->customComparators as $key => $_comparator) { if ($comparator === $_comparator) { - unset($this->comparators[$key]); + unset($this->customComparators[$key]); } } } + + /** + * Unregisters all non-default comparators. + */ + public function reset() + { + $this->customComparators = []; + } + + private function registerDefaultComparators() + { + $this->registerDefaultComparator(new TypeComparator); + $this->registerDefaultComparator(new ScalarComparator); + $this->registerDefaultComparator(new NumericComparator); + $this->registerDefaultComparator(new DoubleComparator); + $this->registerDefaultComparator(new ArrayComparator); + $this->registerDefaultComparator(new ResourceComparator); + $this->registerDefaultComparator(new ObjectComparator); + $this->registerDefaultComparator(new ExceptionComparator); + $this->registerDefaultComparator(new SplObjectStorageComparator); + $this->registerDefaultComparator(new DOMNodeComparator); + $this->registerDefaultComparator(new MockObjectComparator); + $this->registerDefaultComparator(new DateTimeComparator); + } + + private function registerDefaultComparator(Comparator $comparator) + { + \array_unshift($this->defaultComparators, $comparator); + + $comparator->setFactory($this); + } }