1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alex19pov31\LinkedData; |
4
|
|
|
|
5
|
|
|
class BaseRepository implements RepositoryInterface |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Данные |
9
|
|
|
* |
10
|
|
|
* @var array|null |
11
|
|
|
*/ |
12
|
|
|
protected $data; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Время кеширования |
16
|
|
|
* |
17
|
|
|
* @var integer |
18
|
|
|
*/ |
19
|
|
|
private $ttl = 0; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Название репозитория |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $name; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Загрузчик данных |
30
|
|
|
* |
31
|
|
|
* @var LoaderInterface|null |
32
|
|
|
*/ |
33
|
|
|
private $loaderData; |
34
|
|
|
|
35
|
|
|
public function __construct(string $name) |
36
|
|
|
{ |
37
|
|
|
$this->name = $name; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Название репозитория |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function getName(): string |
46
|
|
|
{ |
47
|
|
|
return $this->name; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Установить кеш |
52
|
|
|
* |
53
|
|
|
* @param integer $minutes |
54
|
|
|
* @return RepositoryInterface |
55
|
|
|
*/ |
56
|
|
|
public function cache(int $minutes): RepositoryInterface |
57
|
|
|
{ |
58
|
|
|
$this->ttl = $minutes; |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Обновить кеш |
64
|
|
|
* |
65
|
|
|
* @param integer $minutes |
66
|
|
|
* @return RepositoryInterface |
67
|
|
|
*/ |
68
|
|
|
public function updateCache(int $minutes): RepositoryInterface |
69
|
|
|
{ |
70
|
|
|
setCacheData($minutes, 'repository_' . $this->getName(), '/repositories', 'cache', $this->data); |
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Инициализация хранилища |
76
|
|
|
* |
77
|
|
|
* @return RepositoryInterface |
78
|
|
|
*/ |
79
|
|
|
public function init(): RepositoryInterface |
80
|
|
|
{ |
81
|
|
|
if ($this->isEmpty() && !is_null($this->loaderData)) { |
82
|
|
|
if ($this->ttl == 0) { |
83
|
|
|
$this->putData((array) $this->initWithoutCache()); |
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$data = cache($this->ttl, 'repository_' . $this->getName(), '/repositories', 'cache', function () { |
88
|
|
|
return $this->putData((array) $this->initWithoutCache()); |
|
|
|
|
89
|
|
|
}); |
90
|
|
|
|
91
|
|
|
$this->putData((array) $data); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function initWithoutCache(): array |
98
|
|
|
{ |
99
|
|
|
return (array) $this->loaderData->getData(); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Возвращает все данные из хранилища |
104
|
|
|
* |
105
|
|
|
* @return array|null |
106
|
|
|
*/ |
107
|
|
|
public function getData() |
108
|
|
|
{ |
109
|
|
|
return $this->data; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Вернуть элемент по ключу |
114
|
|
|
* |
115
|
|
|
* @param mixed $key |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
public function getItemByKey($key) |
119
|
|
|
{ |
120
|
|
|
$this->init(); |
121
|
|
|
|
122
|
|
|
if (!isset($this->data[$key])) { |
123
|
|
|
if (!is_null($this->loaderData)) { |
124
|
|
|
$data = $this->loaderData->getItem($key); |
125
|
|
|
if (!empty($data)) { |
126
|
|
|
$this->addItem($key, $data); |
127
|
|
|
return $data; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->data[$key]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Добавить элемент в хранилище |
139
|
|
|
* |
140
|
|
|
* @param mixed $key |
141
|
|
|
* @param mixed $data |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function addItem($key, $data) |
145
|
|
|
{ |
146
|
|
|
$this->data[$key] = $data; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Загрузить данные в хранилище |
151
|
|
|
* |
152
|
|
|
* @param array $data |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
public function putData(array $data) |
156
|
|
|
{ |
157
|
|
|
foreach ($data as $key => $value) { |
158
|
|
|
$this->addItem($key, $value); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Проверка наличия данных в хранилище |
164
|
|
|
* |
165
|
|
|
* @return boolean |
166
|
|
|
*/ |
167
|
|
|
public function isEmpty(): bool |
168
|
|
|
{ |
169
|
|
|
return empty($this->data); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Количество элементов в хранилище |
174
|
|
|
* |
175
|
|
|
* @return integer |
176
|
|
|
*/ |
177
|
|
|
public function getCount(): int |
178
|
|
|
{ |
179
|
|
|
return count($this->data); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Регистрация загрузчика данных |
184
|
|
|
* |
185
|
|
|
* @param LoaderInterface $loader |
186
|
|
|
* @return void |
187
|
|
|
*/ |
188
|
|
|
public function setLoaderData(LoaderInterface $loader) |
189
|
|
|
{ |
190
|
|
|
$this->loaderData = $loader; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.