1 | <?php |
||
2 | |||
3 | namespace Del\Collection; |
||
4 | |||
5 | use Del\Entity\User as UserEntity; |
||
6 | use Doctrine\Common\Collections\ArrayCollection; |
||
7 | use LogicException; |
||
8 | |||
9 | class User extends ArrayCollection |
||
10 | { |
||
11 | /** |
||
12 | * @param UserEntity $user |
||
13 | * @return $this |
||
14 | */ |
||
15 | 1 | public function update(UserEntity $user) |
|
16 | { |
||
17 | 1 | $key = $this->findKey($user); |
|
18 | 1 | if($key) { |
|
19 | |||
20 | 1 | $this->offsetSet($key,$user); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
21 | 1 | return $this; |
|
22 | |||
23 | } |
||
24 | 1 | throw new LogicException('User was not in the collection.'); |
|
25 | } |
||
26 | |||
27 | /** |
||
28 | * @param UserEntity $user |
||
29 | */ |
||
30 | 4 | public function append(UserEntity $user) |
|
31 | { |
||
32 | 4 | $this->add($user); |
|
33 | } |
||
34 | |||
35 | /** |
||
36 | * @return UserEntity|null |
||
37 | */ |
||
38 | 1 | public function current() |
|
39 | { |
||
40 | 1 | return parent::current(); |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * @param UserEntity $user |
||
45 | * @return bool|int |
||
46 | */ |
||
47 | 2 | public function findKey(UserEntity $user) |
|
48 | { |
||
49 | 2 | $it = $this->getIterator(); |
|
50 | 2 | $it->rewind(); |
|
51 | 2 | while($it->valid()) { |
|
52 | 2 | if($it->current()->getId() == $user->getId()) { |
|
53 | 1 | return $it->key(); |
|
0 ignored issues
–
show
|
|||
54 | } |
||
55 | 2 | $it->next(); |
|
56 | } |
||
57 | 2 | return false; |
|
58 | } |
||
59 | |||
60 | |||
61 | |||
62 | 2 | public function findById($id) |
|
63 | { |
||
64 | 2 | $it = $this->getIterator(); |
|
65 | 2 | $it->rewind(); |
|
66 | 2 | while($it->valid()) { |
|
67 | 2 | if($it->current()->getId() == $id) { |
|
68 | 1 | return $it->current(); |
|
69 | } |
||
70 | 2 | $it->next(); |
|
71 | } |
||
72 | |||
73 | 1 | return false; |
|
74 | } |
||
75 | |||
76 | } |