AssignedRange.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace IPLib\Address;
  3. use IPLib\Range\RangeInterface;
  4. /**
  5. * Represents an IP address range with an assigned range type.
  6. *
  7. * @since 1.5.0
  8. */
  9. class AssignedRange
  10. {
  11. /**
  12. * The range definition.
  13. *
  14. * @var \IPLib\Range\RangeInterface
  15. */
  16. protected $range;
  17. /**
  18. * The range type.
  19. *
  20. * @var int one of the \IPLib\Range\Type::T_ constants
  21. */
  22. protected $type;
  23. /**
  24. * The list of exceptions for this range type.
  25. *
  26. * @var \IPLib\Address\AssignedRange[]
  27. */
  28. protected $exceptions;
  29. /**
  30. * Initialize the instance.
  31. *
  32. * @param \IPLib\Range\RangeInterface $range the range definition
  33. * @param int $type The range type (one of the \IPLib\Range\Type::T_ constants)
  34. * @param \IPLib\Address\AssignedRange[] $exceptions the list of exceptions for this range type
  35. */
  36. public function __construct(RangeInterface $range, $type, array $exceptions = array())
  37. {
  38. $this->range = $range;
  39. $this->type = $type;
  40. $this->exceptions = $exceptions;
  41. }
  42. /**
  43. * Get the range definition.
  44. *
  45. * @return \IPLib\Range\RangeInterface
  46. */
  47. public function getRange()
  48. {
  49. return $this->range;
  50. }
  51. /**
  52. * Get the range type.
  53. *
  54. * @return int one of the \IPLib\Range\Type::T_ constants
  55. */
  56. public function getType()
  57. {
  58. return $this->type;
  59. }
  60. /**
  61. * Get the list of exceptions for this range type.
  62. *
  63. * @return \IPLib\Address\AssignedRange[]
  64. */
  65. public function getExceptions()
  66. {
  67. return $this->exceptions;
  68. }
  69. /**
  70. * Get the assigned type for a specific address.
  71. *
  72. * @param \IPLib\Address\AddressInterface $address
  73. *
  74. * @return int|null return NULL of the address is outside this address; a \IPLib\Range\Type::T_ constant otherwise
  75. */
  76. public function getAddressType(AddressInterface $address)
  77. {
  78. $result = null;
  79. if ($this->range->contains($address)) {
  80. foreach ($this->exceptions as $exception) {
  81. $result = $exception->getAddressType($address);
  82. if ($result !== null) {
  83. break;
  84. }
  85. }
  86. if ($result === null) {
  87. $result = $this->type;
  88. }
  89. }
  90. return $result;
  91. }
  92. /**
  93. * Get the assigned type for a specific address range.
  94. *
  95. * @param \IPLib\Range\RangeInterface $range
  96. *
  97. * @return int|false|null return NULL of the range is fully outside this range; false if it's partly crosses this range (or it contains mixed types); a \IPLib\Range\Type::T_ constant otherwise
  98. */
  99. public function getRangeType(RangeInterface $range)
  100. {
  101. $myStart = $this->range->getComparableStartString();
  102. $rangeEnd = $range->getComparableEndString();
  103. if ($myStart > $rangeEnd) {
  104. $result = null;
  105. } else {
  106. $myEnd = $this->range->getComparableEndString();
  107. $rangeStart = $range->getComparableStartString();
  108. if ($myEnd < $rangeStart) {
  109. $result = null;
  110. } elseif ($rangeStart < $myStart || $rangeEnd > $myEnd) {
  111. $result = false;
  112. } else {
  113. $result = null;
  114. foreach ($this->exceptions as $exception) {
  115. $result = $exception->getRangeType($range);
  116. if ($result !== null) {
  117. break;
  118. }
  119. }
  120. if ($result === null) {
  121. $result = $this->getType();
  122. }
  123. }
  124. }
  125. return $result;
  126. }
  127. }