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