123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of the Geocoder package.
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- *
- * @license MIT License
- */
- namespace Geocoder\Model;
- use Geocoder\Exception\CollectionIsEmpty;
- use Geocoder\Exception\InvalidArgument;
- use Geocoder\Exception\OutOfBounds;
- use Traversable;
- /**
- * @author Giorgio Premi <giosh94mhz@gmail.com>
- */
- final class AdminLevelCollection implements \IteratorAggregate, \Countable
- {
- const MAX_LEVEL_DEPTH = 5;
- /**
- * @var AdminLevel[]
- */
- private $adminLevels;
- /**
- * @param AdminLevel[] $adminLevels
- */
- public function __construct(array $adminLevels = [])
- {
- $this->adminLevels = [];
- foreach ($adminLevels as $adminLevel) {
- $level = $adminLevel->getLevel();
- $this->checkLevel($level);
- if ($this->has($level)) {
- throw new InvalidArgument(sprintf('Administrative level %d is defined twice', $level));
- }
- $this->adminLevels[$level] = $adminLevel;
- }
- ksort($this->adminLevels, SORT_NUMERIC);
- }
- /**
- * {@inheritdoc}
- */
- public function getIterator(): Traversable
- {
- return new \ArrayIterator($this->all());
- }
- /**
- * {@inheritdoc}
- */
- public function count(): int
- {
- return count($this->adminLevels);
- }
- /**
- * @return AdminLevel
- *
- * @throws CollectionIsEmpty
- */
- public function first(): AdminLevel
- {
- if ([] === $this->adminLevels) {
- throw new CollectionIsEmpty();
- }
- return reset($this->adminLevels);
- }
- /**
- * @param int $offset
- * @param int|null $length
- *
- * @return AdminLevel[]
- */
- public function slice(int $offset, int $length = null): array
- {
- return array_slice($this->adminLevels, $offset, $length, true);
- }
- /**
- * @return bool
- */
- public function has(int $level): bool
- {
- return isset($this->adminLevels[$level]);
- }
- /**
- * @return AdminLevel
- *
- * @throws \OutOfBoundsException
- * @throws InvalidArgument
- */
- public function get(int $level): AdminLevel
- {
- $this->checkLevel($level);
- if (!isset($this->adminLevels[$level])) {
- throw new InvalidArgument(sprintf('Administrative level %d is not set for this address', $level));
- }
- return $this->adminLevels[$level];
- }
- /**
- * @return AdminLevel[]
- */
- public function all(): array
- {
- return $this->adminLevels;
- }
- /**
- * @param int $level
- *
- * @throws \OutOfBoundsException
- */
- private function checkLevel(int $level)
- {
- if ($level <= 0 || $level > self::MAX_LEVEL_DEPTH) {
- throw new OutOfBounds(sprintf('Administrative level should be an integer in [1,%d], %d given', self::MAX_LEVEL_DEPTH, $level));
- }
- }
- }
|