Bound.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*
  3. * This file is part of composer/semver.
  4. *
  5. * (c) Composer <https://github.com/composer>
  6. *
  7. * For the full copyright and license information, please view
  8. * the LICENSE file that was distributed with this source code.
  9. */
  10. namespace Composer\Semver\Constraint;
  11. class Bound
  12. {
  13. /**
  14. * @var string
  15. */
  16. private $version;
  17. /**
  18. * @var bool
  19. */
  20. private $isInclusive;
  21. /**
  22. * @param string $version
  23. * @param bool $isInclusive
  24. */
  25. public function __construct($version, $isInclusive)
  26. {
  27. $this->version = $version;
  28. $this->isInclusive = $isInclusive;
  29. }
  30. /**
  31. * @return string
  32. */
  33. public function getVersion()
  34. {
  35. return $this->version;
  36. }
  37. /**
  38. * @return bool
  39. */
  40. public function isInclusive()
  41. {
  42. return $this->isInclusive;
  43. }
  44. /**
  45. * @return bool
  46. */
  47. public function isZero()
  48. {
  49. return $this->getVersion() === '0.0.0.0-dev' && $this->isInclusive();
  50. }
  51. /**
  52. * @return bool
  53. */
  54. public function isPositiveInfinity()
  55. {
  56. return $this->getVersion() === PHP_INT_MAX.'.0.0.0' && !$this->isInclusive();
  57. }
  58. /**
  59. * Compares a bound to another with a given operator.
  60. *
  61. * @param Bound $other
  62. * @param string $operator
  63. *
  64. * @return bool
  65. */
  66. public function compareTo(Bound $other, $operator)
  67. {
  68. if (!\in_array($operator, array('<', '>'), true)) {
  69. throw new \InvalidArgumentException('Does not support any other operator other than > or <.');
  70. }
  71. // If they are the same it doesn't matter
  72. if ($this == $other) {
  73. return false;
  74. }
  75. $compareResult = version_compare($this->getVersion(), $other->getVersion());
  76. // Not the same version means we don't need to check if the bounds are inclusive or not
  77. if (0 !== $compareResult) {
  78. return (('>' === $operator) ? 1 : -1) === $compareResult;
  79. }
  80. // Question we're answering here is "am I higher than $other?"
  81. return '>' === $operator ? $other->isInclusive() : !$other->isInclusive();
  82. }
  83. public function __toString()
  84. {
  85. return sprintf(
  86. '%s [%s]',
  87. $this->getVersion(),
  88. $this->isInclusive() ? 'inclusive' : 'exclusive'
  89. );
  90. }
  91. /**
  92. * @return self
  93. */
  94. public static function zero()
  95. {
  96. return new Bound('0.0.0.0-dev', true);
  97. }
  98. /**
  99. * @return self
  100. */
  101. public static function positiveInfinity()
  102. {
  103. return new Bound(PHP_INT_MAX.'.0.0.0', false);
  104. }
  105. }