|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BoneMvc\Module\Unicorn\Collection; |
|
6
|
|
|
|
|
7
|
|
|
use BoneMvc\Module\Unicorn\Entity\Unicorn; |
|
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
9
|
|
|
use JsonSerializable; |
|
10
|
|
|
use LogicException; |
|
11
|
|
|
|
|
12
|
|
|
class UnicornCollection extends ArrayCollection implements JsonSerializable |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @param Unicorn $unicorn |
|
16
|
|
|
* @return $this |
|
17
|
|
|
* @throws LogicException |
|
18
|
|
|
*/ |
|
19
|
|
|
public function update(Unicorn $unicorn): UnicornCollection |
|
20
|
|
|
{ |
|
21
|
|
|
$key = $this->findKey($unicorn); |
|
22
|
|
|
if($key) { |
|
|
|
|
|
|
23
|
|
|
$this->offsetSet($key,$unicorn); |
|
24
|
|
|
return $this; |
|
25
|
|
|
} |
|
26
|
|
|
throw new LogicException('Unicorn was not in the collection.'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param Unicorn $unicorn |
|
31
|
|
|
*/ |
|
32
|
|
|
public function append(Unicorn $unicorn): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->add($unicorn); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return Unicorn|null |
|
39
|
|
|
*/ |
|
40
|
|
|
public function current(): ?Unicorn |
|
41
|
|
|
{ |
|
42
|
|
|
return parent::current(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param Unicorn $unicorn |
|
47
|
|
|
* @return int|null |
|
48
|
|
|
*/ |
|
49
|
|
|
public function findKey(Unicorn $unicorn): ?int |
|
50
|
|
|
{ |
|
51
|
|
|
$it = $this->getIterator(); |
|
52
|
|
|
$it->rewind(); |
|
53
|
|
|
while($it->valid()) { |
|
54
|
|
|
if($it->current()->getId() == $unicorn->getId()) { |
|
55
|
|
|
return $it->key(); |
|
56
|
|
|
} |
|
57
|
|
|
$it->next(); |
|
58
|
|
|
} |
|
59
|
|
|
return null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param int $id |
|
64
|
|
|
* @return Unicorn|null |
|
65
|
|
|
*/ |
|
66
|
|
|
public function findById(int $id): ?Unicorn |
|
67
|
|
|
{ |
|
68
|
|
|
$it = $this->getIterator(); |
|
69
|
|
|
$it->rewind(); |
|
70
|
|
|
while($it->valid()) { |
|
71
|
|
|
if($it->current()->getId() == $id) { |
|
72
|
|
|
return $it->current(); |
|
73
|
|
|
} |
|
74
|
|
|
$it->next(); |
|
75
|
|
|
} |
|
76
|
|
|
return null; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
public function toArray(): array |
|
83
|
|
|
{ |
|
84
|
|
|
$collection = []; |
|
85
|
|
|
$it = $this->getIterator(); |
|
86
|
|
|
$it->rewind(); |
|
87
|
|
|
while($it->valid()) { |
|
88
|
|
|
/** @var Unicorn $row */ |
|
89
|
|
|
$row = $it->current(); |
|
90
|
|
|
$collection[] = $row->toArray(); |
|
91
|
|
|
$it->next(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $collection; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
|
|
public function jsonSerialize(): string |
|
101
|
|
|
{ |
|
102
|
|
|
return \json_encode($this->toArray()); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
|
|
public function __toString(): string |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->jsonSerialize(); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: