|
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
|
11 |
|
public function __construct(array $sourceArray = []) |
|
28
|
|
|
{ |
|
29
|
11 |
|
$this->items = []; |
|
30
|
11 |
|
$this->appendArray($sourceArray); |
|
31
|
|
|
|
|
32
|
7 |
|
return $this; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Link $link |
|
37
|
|
|
* |
|
38
|
|
|
* @return CollectionInterface |
|
39
|
|
|
*/ |
|
40
|
4 |
|
public function push(Link $link): CollectionInterface |
|
41
|
|
|
{ |
|
42
|
4 |
|
$this->items[$link->getUrl()] = $link->getDepth(); |
|
43
|
|
|
|
|
44
|
4 |
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param array $urls |
|
49
|
|
|
* @param int $depth |
|
50
|
|
|
* |
|
51
|
|
|
* @return CollectionInterface |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function appendUrlsAtDepth(array $urls, int $depth = 0): CollectionInterface |
|
54
|
|
|
{ |
|
55
|
1 |
|
$sourceArray = array_fill_keys($urls, $depth); |
|
56
|
1 |
|
$this->appendArray($sourceArray); |
|
57
|
|
|
|
|
58
|
1 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param int|null $depth |
|
63
|
|
|
* |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function all(int $depth = null): array |
|
67
|
|
|
{ |
|
68
|
1 |
|
return null === $depth |
|
69
|
1 |
|
? array_keys($this->items) |
|
70
|
1 |
|
: array_keys($this->items, $depth, true); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return null|Link |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function shift(): ?Link |
|
77
|
|
|
{ |
|
78
|
1 |
|
if (!$this->items) { |
|
|
|
|
|
|
79
|
1 |
|
return null; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
$depth = reset($this->items); |
|
83
|
1 |
|
$url = key($this->items); |
|
84
|
1 |
|
unset($this->items[$url]); |
|
85
|
|
|
|
|
86
|
1 |
|
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
|
11 |
|
private function appendArray(array $sourceArray): void |
|
109
|
|
|
{ |
|
110
|
11 |
|
foreach ($sourceArray as $url => $depth) { |
|
111
|
7 |
|
if (!\is_string($url)) { |
|
112
|
1 |
|
throw new InvalidArgumentException('Source array key must be a string (url)'); |
|
113
|
|
|
} |
|
114
|
6 |
|
if (!\is_int($depth) || $depth < 0) { |
|
115
|
2 |
|
throw new InvalidArgumentException('Source array value must be a non-negative int (depth)'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
4 |
|
if (!\filter_var($url, FILTER_VALIDATE_URL)) { |
|
119
|
4 |
|
throw new InvalidUrlException("Provided URL is invalid: {$url}"); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
7 |
|
$this->items = array_merge($sourceArray, $this->items); |
|
123
|
7 |
|
} |
|
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.