Range.php 1017 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Security\Ip;
  8. use InvalidArgumentException;
  9. use IPLib\Factory;
  10. use IPLib\ParseStringFlag;
  11. use IPLib\Range\RangeInterface;
  12. use OCP\Security\Ip\IAddress;
  13. use OCP\Security\Ip\IRange;
  14. class Range implements IRange {
  15. private readonly RangeInterface $range;
  16. public function __construct(string $range) {
  17. $range = Factory::parseRangeString($range);
  18. if ($range === null) {
  19. throw new InvalidArgumentException('Given range can’t be parsed');
  20. }
  21. $this->range = $range;
  22. }
  23. public static function isValid(string $range): bool {
  24. return Factory::parseRangeString($range) !== null;
  25. }
  26. public function contains(IAddress $address): bool {
  27. return $this->range->contains(Factory::parseAddressString((string)$address, ParseStringFlag::MAY_INCLUDE_ZONEID));
  28. }
  29. public function __toString(): string {
  30. return $this->range->toString();
  31. }
  32. }