1
0

Range.php 953 B

123456789101112131415161718192021222324252627282930313233343536373839
  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\Range\RangeInterface;
  11. use OCP\Security\Ip\IAddress;
  12. use OCP\Security\Ip\IRange;
  13. class Range implements IRange {
  14. private readonly RangeInterface $range;
  15. public function __construct(string $range) {
  16. $range = Factory::parseRangeString($range);
  17. if ($range === null) {
  18. throw new InvalidArgumentException('Given range can’t be parsed');
  19. }
  20. $this->range = $range;
  21. }
  22. public static function isValid(string $range): bool {
  23. return Factory::parseRangeString($range) !== null;
  24. }
  25. public function contains(IAddress $address): bool {
  26. return $this->range->contains(Factory::parseAddressString((string)$address));
  27. }
  28. public function __toString(): string {
  29. return $this->range->toString();
  30. }
  31. }