|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* /src/Resource/ResourceCollection.php |
|
5
|
|
|
* |
|
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Resource; |
|
10
|
|
|
|
|
11
|
|
|
use App\Collection\CollectionTrait; |
|
12
|
|
|
use App\Rest\Interfaces\RestResourceInterface; |
|
13
|
|
|
use CallbackFilterIterator; |
|
14
|
|
|
use Closure; |
|
15
|
|
|
use Countable; |
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
use IteratorAggregate; |
|
18
|
|
|
use IteratorIterator; |
|
19
|
|
|
use Psr\Log\LoggerInterface; |
|
20
|
|
|
use Throwable; |
|
21
|
|
|
use function sprintf; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class ResourceCollection |
|
25
|
|
|
* |
|
26
|
|
|
* @package App\Resource |
|
27
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
28
|
|
|
* |
|
29
|
|
|
* @method RestResourceInterface get(string $className) |
|
30
|
|
|
* @method IteratorAggregate<int, RestResourceInterface> getAll() |
|
31
|
|
|
*/ |
|
32
|
|
|
class ResourceCollection implements Countable |
|
33
|
|
|
{ |
|
34
|
|
|
use CollectionTrait; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Collection constructor. |
|
38
|
327 |
|
* |
|
39
|
|
|
* @param IteratorAggregate<int, RestResourceInterface> $items |
|
40
|
327 |
|
*/ |
|
41
|
327 |
|
public function __construct( |
|
42
|
327 |
|
private IteratorAggregate $items, |
|
43
|
|
|
private LoggerInterface $logger, |
|
44
|
|
|
) { |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
11 |
|
/** |
|
48
|
|
|
* Getter method for REST resource by entity class name. |
|
49
|
11 |
|
*/ |
|
50
|
|
|
public function getEntityResource(string $className): RestResourceInterface |
|
51
|
11 |
|
{ |
|
52
|
2 |
|
return $this->getFilteredItemByEntity($className) ?? throw new InvalidArgumentException( |
|
53
|
2 |
|
sprintf('Resource class does not exist for entity \'%s\'', $className), |
|
54
|
2 |
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
/** |
|
58
|
|
|
* Method to check if specified entity class REST resource exists or not |
|
59
|
|
|
* in current collection. |
|
60
|
9 |
|
*/ |
|
61
|
|
|
public function hasEntityResource(?string $className = null): bool |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->getFilteredItemByEntity($className ?? '') !== null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function filter(string $className): Closure |
|
67
|
12 |
|
{ |
|
68
|
|
|
return static fn (RestResourceInterface $restResource): bool => $restResource instanceof $className; |
|
69
|
12 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getErrorMessage(string $className): string |
|
72
|
118 |
|
{ |
|
73
|
|
|
return sprintf('Resource \'%s\' does not exist', $className); |
|
74
|
118 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
2 |
|
* Getter method to get filtered item by given entity class. |
|
78
|
|
|
*/ |
|
79
|
2 |
|
private function getFilteredItemByEntity(string $entityName): ?RestResourceInterface |
|
80
|
2 |
|
{ |
|
81
|
2 |
|
try { |
|
82
|
|
|
$iterator = $this->items->getIterator(); |
|
83
|
|
|
} catch (Throwable $throwable) { |
|
84
|
2 |
|
$this->logger->error($throwable->getMessage()); |
|
85
|
|
|
|
|
86
|
|
|
return null; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$callback = static fn (RestResourceInterface $resource): bool => $resource->getEntityName() === $entityName; |
|
90
|
23 |
|
|
|
91
|
|
|
$filteredIterator = new CallbackFilterIterator(new IteratorIterator($iterator), $callback); |
|
92
|
|
|
$filteredIterator->rewind(); |
|
93
|
23 |
|
|
|
94
|
1 |
|
return $filteredIterator->current(); |
|
95
|
1 |
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|