|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of PHP CS Fixer. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * Dariusz Rumiński <[email protected]> |
| 8 | + * |
| 9 | + * This source file is subject to the MIT license that is bundled |
| 10 | + * with this source code in the file LICENSE. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace PhpCsFixer\FixerDefinition; |
| 14 | + |
| 15 | +/** |
| 16 | + * @author Andreas Möller <[email protected]> |
| 17 | + */ |
| 18 | +final class VersionSpecification implements VersionSpecificationInterface |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var int|null |
| 22 | + */ |
| 23 | + private $minimum; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var int|null |
| 27 | + */ |
| 28 | + private $maximum; |
| 29 | + |
| 30 | + /** |
| 31 | + * @param int|null $minimum |
| 32 | + * @param int|null $maximum |
| 33 | + * |
| 34 | + * @throws \InvalidArgumentException |
| 35 | + */ |
| 36 | + public function __construct($minimum = null, $maximum = null) |
| 37 | + { |
| 38 | + if (null === $minimum && null === $maximum) { |
| 39 | + throw new \InvalidArgumentException('Either minimum or maximum need to be specified'); |
| 40 | + } |
| 41 | + |
| 42 | + if (null !== $minimum && (!is_int($minimum) || 1 > $minimum)) { |
| 43 | + throw new \InvalidArgumentException('Minimum needs to be either null or an integer greater than 0'); |
| 44 | + } |
| 45 | + |
| 46 | + if (null !== $maximum && (!is_int($maximum) || 1 > $maximum)) { |
| 47 | + throw new \InvalidArgumentException('Minimum needs to be either null or an integer greater than 0'); |
| 48 | + } |
| 49 | + |
| 50 | + if (null !== $maximum && null !== $minimum && $maximum < $minimum) { |
| 51 | + throw new \InvalidArgumentException('Maximum should not be less than the minimum'); |
| 52 | + } |
| 53 | + |
| 54 | + $this->minimum = $minimum; |
| 55 | + $this->maximum = $maximum; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {@inheritdoc} |
| 60 | + */ |
| 61 | + public function isSatisfiedBy($version) |
| 62 | + { |
| 63 | + if (null !== $this->minimum && $version < $this->minimum) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + if (null !== $this->maximum && $version > $this->maximum) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + return true; |
| 71 | + } |
| 72 | +} |
0 commit comments