Beispiel #1
0
 public function valid()
 {
     if (!parent::valid()) {
         return false;
     }
     return $this->range->contains($this->current()->getKey());
 }
Beispiel #2
0
 /**
  * @param IP|Network $exclude
  * @return array
  * @throws \Exception
  */
 public function exclude($exclude)
 {
     $exclude = self::parse($exclude);
     if ($exclude->getFirstIP()->inAddr() > $this->getLastIP()->inAddr() || $exclude->getLastIP()->inAddr() < $this->getFirstIP()->inAddr()) {
         throw new \Exception('Exclude subnet not within target network');
     }
     $networks = array();
     $newPrefixLength = $this->getPrefixLength() + 1;
     $lower = clone $this;
     $lower->setPrefixLength($newPrefixLength);
     $upper = clone $lower;
     $upper->setIP($lower->getLastIP()->next());
     while ($newPrefixLength <= $exclude->getPrefixLength()) {
         $range = new Range($lower->getFirstIP(), $lower->getLastIP());
         if ($range->contains($exclude)) {
             $matched = $lower;
             $unmatched = $upper;
         } else {
             $matched = $upper;
             $unmatched = $lower;
         }
         $networks[] = clone $unmatched;
         if (++$newPrefixLength > $this->getNetwork()->getMaxPrefixLength()) {
             break;
         }
         $matched->setPrefixLength($newPrefixLength);
         $unmatched->setPrefixLength($newPrefixLength);
         $unmatched->setIP($matched->getLastIP()->next());
     }
     sort($networks);
     return $networks;
 }
 /**
  * @dataProvider getExpectedResultsAndValues
  *
  * @param Boolean $contains
  * @param integer $value
  */
 public function testShouldDetermineIfContainsValue($contains, $value)
 {
     $range = new Range($this->start, $this->end);
     $this->assertEquals($contains, $range->contains($value));
 }
Beispiel #4
0
 /**
  * @param integer $value
  */
 public function add($value)
 {
     if ($this->range->contains($value)) {
         $this->count++;
     }
 }
Beispiel #5
0
 /**
  * Searches a range
  * 
  * @param Range $range 
  * 
  * @return RangeIterator
  */
 public function searchRange(Range $range)
 {
     $iterator = $this->getIterator();
     // find start
     $start = null;
     $binarySearch = new BinarySearch($this);
     $startHint = $binarySearch->search($range->getMin());
     if ($startHint == null) {
         return new RangeIterator($iterator, Range::getEmptyRange());
     }
     $iterator->setOffset($startHint->getOffset(), Parser::HINT_RESULT_BOUNDARY);
     if (!$range->contains($startHint->getKey()) && $startHint->getKey() <= $range->getMin()) {
         // shift $startHint higher
         foreach ($iterator as $result) {
             if ($range->contains($result->getKey())) {
                 $start = $result;
                 break;
             }
         }
     } else {
         // shift $startHint lower
         if ($range->contains($startHint->getKey())) {
             $start = $startHint;
         }
         $iterator->setDirection(KeyReader::DIRECTION_BACKWARD);
         foreach ($iterator as $result) {
             // Skip everything which is too big
             if (!$range->contains($result->getKey() && $result->getKey() >= $range->getMax())) {
                 continue;
             }
             // shift the start left until no more key is included
             if ($range->contains($result->getKey())) {
                 $start = $result;
             } else {
                 break;
             }
         }
     }
     if (is_null($start)) {
         return new RangeIterator($iterator, Range::getEmptyRange());
     }
     $iterator = $this->getIterator();
     $iterator->setOffset($start->getOffset(), Parser::HINT_RESULT_BOUNDARY);
     return new RangeIterator($iterator, $range);
 }