1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* /src/Collection/CollectionTrait.php |
5
|
|
|
* |
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace App\Collection; |
10
|
|
|
|
11
|
|
|
use CallbackFilterIterator; |
12
|
|
|
use Closure; |
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
use IteratorAggregate; |
15
|
|
|
use IteratorIterator; |
16
|
|
|
use Throwable; |
17
|
|
|
use function iterator_count; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Trait CollectionTrait |
21
|
|
|
* |
22
|
|
|
* @package App\Collection |
23
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
trait CollectionTrait |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Method to filter current collection. |
29
|
|
|
* |
30
|
|
|
* @psalm-var class-string $className |
31
|
|
|
*/ |
32
|
|
|
abstract public function filter(string $className): Closure; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Method to process error message for current collection. |
36
|
|
|
* |
37
|
|
|
* @psalm-var class-string $className |
38
|
|
|
* |
39
|
|
|
* @throws InvalidArgumentException |
40
|
|
|
*/ |
41
|
|
|
abstract public function getErrorMessage(string $className): string; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Getter method for given class for current collection. |
45
|
|
|
* |
46
|
|
|
* @psalm-var class-string $className |
47
|
|
|
* |
48
|
|
|
* @throws InvalidArgumentException |
49
|
|
|
*/ |
50
|
151 |
|
public function get(string $className): mixed |
51
|
|
|
{ |
52
|
151 |
|
return $this->getFilteredItem($className) |
53
|
|
|
?? throw new InvalidArgumentException($this->getErrorMessage($className)); |
|
|
|
|
54
|
151 |
|
} |
55
|
3 |
|
|
56
|
|
|
/** |
57
|
|
|
* Method to get all items from current collection. |
58
|
148 |
|
*/ |
59
|
|
|
public function getAll(): IteratorAggregate |
60
|
|
|
{ |
61
|
|
|
return $this->items; |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
/** |
65
|
|
|
* Method to check if specified class exists or not in current collection. |
66
|
2 |
|
* |
67
|
|
|
* @psalm-var class-string $className |
68
|
|
|
*/ |
69
|
|
|
public function has(?string $className = null): bool |
70
|
|
|
{ |
71
|
|
|
return $this->getFilteredItem($className ?? '') !== null; |
72
|
152 |
|
} |
73
|
|
|
|
74
|
152 |
|
/** |
75
|
|
|
* Count elements of an object. |
76
|
|
|
*/ |
77
|
|
|
public function count(): int |
78
|
|
|
{ |
79
|
|
|
return iterator_count($this->items); |
80
|
1 |
|
} |
81
|
|
|
|
82
|
1 |
|
/** |
83
|
|
|
* @psalm-var class-string $className |
84
|
|
|
*/ |
85
|
|
|
private function getFilteredItem(string $className): mixed |
86
|
|
|
{ |
87
|
|
|
try { |
88
|
176 |
|
$iterator = $this->items->getIterator(); |
89
|
|
|
} catch (Throwable $throwable) { |
90
|
|
|
$this->logger->error($throwable->getMessage()); |
91
|
176 |
|
|
92
|
1 |
|
return null; |
93
|
1 |
|
} |
94
|
|
|
|
95
|
1 |
|
$filteredIterator = new CallbackFilterIterator(new IteratorIterator($iterator), $this->filter($className)); |
96
|
|
|
$filteredIterator->rewind(); |
97
|
|
|
|
98
|
175 |
|
return $filteredIterator->current(); |
99
|
175 |
|
} |
100
|
|
|
} |
101
|
|
|
|