radowoj /
crawla
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Radowoj\Crawla\Link; |
||
| 6 | |||
| 7 | use Countable; |
||
| 8 | use InvalidArgumentException; |
||
| 9 | use Radowoj\Crawla\Exception\InvalidUrlException; |
||
| 10 | |||
| 11 | class Collection implements Countable, CollectionInterface |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Items stored in this collection |
||
| 15 | * array keys - urls |
||
| 16 | * array values - depths. |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $items = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Collection constructor. |
||
| 24 | * |
||
| 25 | * @param array $sourceArray [url => depth] |
||
| 26 | */ |
||
| 27 | 26 | public function __construct(array $sourceArray = []) |
|
| 28 | { |
||
| 29 | 26 | $this->items = []; |
|
| 30 | 26 | $this->appendArray($sourceArray); |
|
| 31 | |||
| 32 | 22 | return $this; |
|
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param Link $link |
||
| 37 | * |
||
| 38 | * @return CollectionInterface |
||
| 39 | */ |
||
| 40 | 9 | public function push(Link $link): CollectionInterface |
|
| 41 | { |
||
| 42 | 9 | $this->items[$link->getUrl()] = $link->getDepth(); |
|
| 43 | |||
| 44 | 9 | return $this; |
|
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param array $urls |
||
| 49 | * @param int $depth |
||
| 50 | * |
||
| 51 | * @return CollectionInterface |
||
| 52 | */ |
||
| 53 | 7 | public function appendUrlsAtDepth(array $urls, int $depth = 0): CollectionInterface |
|
| 54 | { |
||
| 55 | 7 | $sourceArray = array_fill_keys($urls, $depth); |
|
| 56 | 7 | $this->appendArray($sourceArray); |
|
| 57 | |||
| 58 | 7 | return $this; |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param int|null $depth |
||
| 63 | * |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | 7 | public function all(int $depth = null): array |
|
| 67 | { |
||
| 68 | 7 | return null === $depth |
|
| 69 | 7 | ? array_keys($this->items) |
|
| 70 | 7 | : array_keys($this->items, $depth, true); |
|
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @return null|Link |
||
| 75 | */ |
||
| 76 | 7 | public function shift(): ?Link |
|
| 77 | { |
||
| 78 | 7 | if (!$this->items) { |
|
|
0 ignored issues
–
show
|
|||
| 79 | 7 | return null; |
|
| 80 | } |
||
| 81 | |||
| 82 | 7 | $depth = reset($this->items); |
|
| 83 | 7 | $url = key($this->items); |
|
| 84 | 7 | unset($this->items[$url]); |
|
| 85 | |||
| 86 | 7 | return new Link($url, $depth); |
|
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return int |
||
| 91 | */ |
||
| 92 | 1 | public function count(): int |
|
| 93 | { |
||
| 94 | 1 | return \count($this->items); |
|
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | 4 | public function toArray(): array |
|
| 101 | { |
||
| 102 | 4 | return $this->items; |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param array $sourceArray |
||
| 107 | */ |
||
| 108 | 26 | private function appendArray(array $sourceArray): void |
|
| 109 | { |
||
| 110 | 26 | foreach ($sourceArray as $url => $depth) { |
|
| 111 | 13 | if (!\is_string($url)) { |
|
| 112 | 1 | throw new InvalidArgumentException('Source array key must be a string (url)'); |
|
| 113 | } |
||
| 114 | 12 | if (!\is_int($depth) || $depth < 0) { |
|
| 115 | 2 | throw new InvalidArgumentException('Source array value must be a non-negative int (depth)'); |
|
| 116 | } |
||
| 117 | |||
| 118 | 10 | if (!\filter_var($url, FILTER_VALIDATE_URL)) { |
|
| 119 | 1 | throw new InvalidUrlException("Provided URL is invalid: {$url}"); |
|
| 120 | } |
||
| 121 | } |
||
| 122 | 22 | $this->items = array_merge($sourceArray, $this->items); |
|
| 123 | } |
||
| 124 | } |
||
| 125 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.