-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdCodeObjectCollection.php
More file actions
118 lines (96 loc) · 3.13 KB
/
Copy pathIdCodeObjectCollection.php
File metadata and controls
118 lines (96 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
namespace Echron\DataTypes;
use Echron\DataTypes\Observable\Context\Context;
use Echron\DataTypes\Observable\Observable;
use Echron\DataTypes\Observable\Observer;
use Echron\Tools\Normalize\NormalizeConfig;
class IdCodeObjectCollection extends BasicCollection implements Observer
{
private IdValueStore $idValueStore;
private KeyValueStore $codeValueStore;
public function __construct(NormalizeConfig $normalizeConfig = null)
{
parent::__construct();
$this->idValueStore = new IdValueStore();
$this->codeValueStore = new KeyValueStore($normalizeConfig);
}
public function add(IdCodeObject $idCodeObject): int
{
$index = $this->addToCollection($idCodeObject);
$this->idValueStore->add($idCodeObject->getId(), $index);
$this->codeValueStore->add($idCodeObject->getCode(), $index);
return $index;
}
public function removeByCode(string $code): void
{
$index = $this->codeValueStore->getValueByKey($code);
$this->idValueStore->removeByValue($index);
$this->codeValueStore->removeByValue($index);
$this->removeFromCollection($index);
}
public function removeById(int $id): void
{
$index = $this->idValueStore->getValueByKey($id);
$this->idValueStore->removeByValue($index);
$this->codeValueStore->removeByValue($index);
$this->removeFromCollection($index);
}
/**
* @param string $code
* @return IdCodeObject
* @throws Exception\NotInCollectionException
*/
public function getByCode(string $code): IdCodeObject
{
$index = $this->codeValueStore->getValueByKey($code);
return $this->getByIndex($index);
}
/**
* @param int $id
* @return IdCodeObject
* @throws Exception\NotInCollectionException
*/
public function getById(int $id): IdCodeObject
{
$index = $this->idValueStore->getValueByKey($id);
return $this->getByIndex($index);
}
public function hasCode(string $code): bool
{
return $this->codeValueStore->hasKey($code);
}
public function hasId(int $id): bool
{
return $this->idValueStore->hasKey($id);
}
/**
* @return int[]
*/
public function getIds(): array
{
return $this->idValueStore->getKeys();
}
/**
* @return string[]
*/
public function getCodes(): array
{
return $this->codeValueStore->getKeys();
}
//TODO: make this protected
public function update(Observable $subject, Context $context)
{
throw new \Exception('Not implemented');
// if ($context instanceof PropertyChangeContext) {
// switch ($context->getProperty()) {
// case 'id':
// $this->_updateId($context->getBefore(), $context->getAfter());
// break;
// case 'code':
// $this->updateCode($context->getBefore(), $context->getAfter());
// break;
// }
// }
}
}