|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Explicit Architecture POC, |
|
7
|
|
|
* which is created on top of the Symfony Demo application. |
|
8
|
|
|
* |
|
9
|
|
|
* (c) Herberto Graça <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Acme\App\Core\Port\Persistence; |
|
16
|
|
|
|
|
17
|
|
|
use Acme\App\Core\Port\Persistence\Exception\CanOnlyHydrateFromArrayException; |
|
18
|
|
|
use Acme\App\Core\Port\Persistence\Exception\EmptyQueryResultException; |
|
19
|
|
|
use Acme\App\Core\Port\Persistence\Exception\NotConstructableFromArrayException; |
|
20
|
|
|
use Acme\App\Core\Port\Persistence\Exception\NotUniqueQueryResultException; |
|
21
|
|
|
use Acme\PhpExtension\ConstructableFromArrayInterface; |
|
22
|
|
|
use ArrayIterator; |
|
23
|
|
|
use Iterator; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* This class can be used by an adapter to wrap a result array, or the adapter can have its own collection |
|
27
|
|
|
* which must implement the ResultCollectionInterface. |
|
28
|
|
|
* |
|
29
|
|
|
* @internal this class should not be used for type-hinting, use the ResultCollectionInterface instead |
|
30
|
|
|
*/ |
|
31
|
|
|
final class ResultCollection implements ResultCollectionInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private $itemList; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct(array $itemList = []) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->itemList = $itemList; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getSingleResult() |
|
44
|
|
|
{ |
|
45
|
|
|
$count = $this->count(); |
|
46
|
|
|
|
|
47
|
|
|
if ($count > 1) { |
|
48
|
|
|
throw new NotUniqueQueryResultException(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if ($count === 0) { |
|
52
|
|
|
throw new EmptyQueryResultException(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $this->getFirstElement(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getIterator(): Iterator |
|
59
|
|
|
{ |
|
60
|
|
|
return new ArrayIterator($this->itemList); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function count(): int |
|
64
|
|
|
{ |
|
65
|
|
|
return \count($this->itemList); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string|ConstructableFromArrayInterface $fqcn |
|
70
|
|
|
*/ |
|
71
|
|
|
public function hydrateResultItemsAs(string $fqcn): ResultCollectionInterface |
|
72
|
|
|
{ |
|
73
|
|
|
if (!is_subclass_of($fqcn, ConstructableFromArrayInterface::class)) { |
|
|
|
|
|
|
74
|
|
|
throw new NotConstructableFromArrayException($fqcn); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$item = reset($this->itemList); |
|
78
|
|
|
if (!\is_array($item)) { // we assume all items have the same type |
|
79
|
|
|
throw new CanOnlyHydrateFromArrayException($item); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$hydratedItemList = []; |
|
83
|
|
|
foreach ($this->itemList as $item) { |
|
84
|
|
|
$hydratedItemList[] = $fqcn::fromArray($item); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return new self($hydratedItemList); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string|ConstructableFromArrayInterface $fqcn |
|
92
|
|
|
*/ |
|
93
|
|
|
public function hydrateSingleResultAs(string $fqcn) |
|
94
|
|
|
{ |
|
95
|
|
|
if (!is_subclass_of($fqcn, ConstructableFromArrayInterface::class)) { |
|
|
|
|
|
|
96
|
|
|
throw new NotConstructableFromArrayException($fqcn); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$item = $this->getSingleResult(); |
|
100
|
|
|
if (!\is_array($item)) { // we assume all items have the same type |
|
101
|
|
|
throw new CanOnlyHydrateFromArrayException($item); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $fqcn::fromArray($item); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function toArray(): array |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->itemList; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private function getFirstElement() |
|
113
|
|
|
{ |
|
114
|
|
|
return \reset($this->itemList); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|