diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-01-18 10:01:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-18 10:01:06 +0100 |
commit | 3fb29fdda04ca86e04422d49b86cf646d53c4f9d (patch) | |
tree | adf8512f93f5559ba87d0c9931969ae4ebea7133 /application/bookmark/BookmarkArray.php | |
parent | 796c4c57d085ae4589b53dfe8369ae9ba30ffdaf (diff) | |
parent | e26e2060f5470ce8bf4c5973284bae07b8af170a (diff) | |
download | Shaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.tar.gz Shaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.tar.zst Shaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.zip |
Store bookmarks as PHP objects and add a service layer to retriā¦ (#1307)
Store bookmarks as PHP objects and add a service layer to retrieve them
Diffstat (limited to 'application/bookmark/BookmarkArray.php')
-rw-r--r-- | application/bookmark/BookmarkArray.php | 259 |
1 files changed, 259 insertions, 0 deletions
diff --git a/application/bookmark/BookmarkArray.php b/application/bookmark/BookmarkArray.php new file mode 100644 index 00000000..d87d43b4 --- /dev/null +++ b/application/bookmark/BookmarkArray.php | |||
@@ -0,0 +1,259 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Bookmark; | ||
4 | |||
5 | use Shaarli\Bookmark\Exception\InvalidBookmarkException; | ||
6 | |||
7 | /** | ||
8 | * Class BookmarkArray | ||
9 | * | ||
10 | * Implementing ArrayAccess, this allows us to use the bookmark list | ||
11 | * as an array and iterate over it. | ||
12 | * | ||
13 | * @package Shaarli\Bookmark | ||
14 | */ | ||
15 | class BookmarkArray implements \Iterator, \Countable, \ArrayAccess | ||
16 | { | ||
17 | /** | ||
18 | * @var Bookmark[] | ||
19 | */ | ||
20 | protected $bookmarks; | ||
21 | |||
22 | /** | ||
23 | * @var array List of all bookmarks IDS mapped with their array offset. | ||
24 | * Map: id->offset. | ||
25 | */ | ||
26 | protected $ids; | ||
27 | |||
28 | /** | ||
29 | * @var int Position in the $this->keys array (for the Iterator interface) | ||
30 | */ | ||
31 | protected $position; | ||
32 | |||
33 | /** | ||
34 | * @var array List of offset keys (for the Iterator interface implementation) | ||
35 | */ | ||
36 | protected $keys; | ||
37 | |||
38 | /** | ||
39 | * @var array List of all recorded URLs (key=url, value=bookmark offset) | ||
40 | * for fast reserve search (url-->bookmark offset) | ||
41 | */ | ||
42 | protected $urls; | ||
43 | |||
44 | public function __construct() | ||
45 | { | ||
46 | $this->ids = []; | ||
47 | $this->bookmarks = []; | ||
48 | $this->keys = []; | ||
49 | $this->urls = []; | ||
50 | $this->position = 0; | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * Countable - Counts elements of an object | ||
55 | * | ||
56 | * @return int Number of bookmarks | ||
57 | */ | ||
58 | public function count() | ||
59 | { | ||
60 | return count($this->bookmarks); | ||
61 | } | ||
62 | |||
63 | /** | ||
64 | * ArrayAccess - Assigns a value to the specified offset | ||
65 | * | ||
66 | * @param int $offset Bookmark ID | ||
67 | * @param Bookmark $value instance | ||
68 | * | ||
69 | * @throws InvalidBookmarkException | ||
70 | */ | ||
71 | public function offsetSet($offset, $value) | ||
72 | { | ||
73 | if (! $value instanceof Bookmark | ||
74 | || $value->getId() === null || empty($value->getUrl()) | ||
75 | || ($offset !== null && ! is_int($offset)) || ! is_int($value->getId()) | ||
76 | || $offset !== null && $offset !== $value->getId() | ||
77 | ) { | ||
78 | throw new InvalidBookmarkException($value); | ||
79 | } | ||
80 | |||
81 | // If the bookmark exists, we reuse the real offset, otherwise new entry | ||
82 | if ($offset !== null) { | ||
83 | $existing = $this->getBookmarkOffset($offset); | ||
84 | } else { | ||
85 | $existing = $this->getBookmarkOffset($value->getId()); | ||
86 | } | ||
87 | |||
88 | if ($existing !== null) { | ||
89 | $offset = $existing; | ||
90 | } else { | ||
91 | $offset = count($this->bookmarks); | ||
92 | } | ||
93 | |||
94 | $this->bookmarks[$offset] = $value; | ||
95 | $this->urls[$value->getUrl()] = $offset; | ||
96 | $this->ids[$value->getId()] = $offset; | ||
97 | } | ||
98 | |||
99 | /** | ||
100 | * ArrayAccess - Whether or not an offset exists | ||
101 | * | ||
102 | * @param int $offset Bookmark ID | ||
103 | * | ||
104 | * @return bool true if it exists, false otherwise | ||
105 | */ | ||
106 | public function offsetExists($offset) | ||
107 | { | ||
108 | return array_key_exists($this->getBookmarkOffset($offset), $this->bookmarks); | ||
109 | } | ||
110 | |||
111 | /** | ||
112 | * ArrayAccess - Unsets an offset | ||
113 | * | ||
114 | * @param int $offset Bookmark ID | ||
115 | */ | ||
116 | public function offsetUnset($offset) | ||
117 | { | ||
118 | $realOffset = $this->getBookmarkOffset($offset); | ||
119 | $url = $this->bookmarks[$realOffset]->getUrl(); | ||
120 | unset($this->urls[$url]); | ||
121 | unset($this->ids[$offset]); | ||
122 | unset($this->bookmarks[$realOffset]); | ||
123 | } | ||
124 | |||
125 | /** | ||
126 | * ArrayAccess - Returns the value at specified offset | ||
127 | * | ||
128 | * @param int $offset Bookmark ID | ||
129 | * | ||
130 | * @return Bookmark|null The Bookmark if found, null otherwise | ||
131 | */ | ||
132 | public function offsetGet($offset) | ||
133 | { | ||
134 | $realOffset = $this->getBookmarkOffset($offset); | ||
135 | return isset($this->bookmarks[$realOffset]) ? $this->bookmarks[$realOffset] : null; | ||
136 | } | ||
137 | |||
138 | /** | ||
139 | * Iterator - Returns the current element | ||
140 | * | ||
141 | * @return Bookmark corresponding to the current position | ||
142 | */ | ||
143 | public function current() | ||
144 | { | ||
145 | return $this[$this->keys[$this->position]]; | ||
146 | } | ||
147 | |||
148 | /** | ||
149 | * Iterator - Returns the key of the current element | ||
150 | * | ||
151 | * @return int Bookmark ID corresponding to the current position | ||
152 | */ | ||
153 | public function key() | ||
154 | { | ||
155 | return $this->keys[$this->position]; | ||
156 | } | ||
157 | |||
158 | /** | ||
159 | * Iterator - Moves forward to next element | ||
160 | */ | ||
161 | public function next() | ||
162 | { | ||
163 | ++$this->position; | ||
164 | } | ||
165 | |||
166 | /** | ||
167 | * Iterator - Rewinds the Iterator to the first element | ||
168 | * | ||
169 | * Entries are sorted by date (latest first) | ||
170 | */ | ||
171 | public function rewind() | ||
172 | { | ||
173 | $this->keys = array_keys($this->ids); | ||
174 | $this->position = 0; | ||
175 | } | ||
176 | |||
177 | /** | ||
178 | * Iterator - Checks if current position is valid | ||
179 | * | ||
180 | * @return bool true if the current Bookmark ID exists, false otherwise | ||
181 | */ | ||
182 | public function valid() | ||
183 | { | ||
184 | return isset($this->keys[$this->position]); | ||
185 | } | ||
186 | |||
187 | /** | ||
188 | * Returns a bookmark offset in bookmarks array from its unique ID. | ||
189 | * | ||
190 | * @param int $id Persistent ID of a bookmark. | ||
191 | * | ||
192 | * @return int Real offset in local array, or null if doesn't exist. | ||
193 | */ | ||
194 | protected function getBookmarkOffset($id) | ||
195 | { | ||
196 | if (isset($this->ids[$id])) { | ||
197 | return $this->ids[$id]; | ||
198 | } | ||
199 | return null; | ||
200 | } | ||
201 | |||
202 | /** | ||
203 | * Return the next key for bookmark creation. | ||
204 | * E.g. If the last ID is 597, the next will be 598. | ||
205 | * | ||
206 | * @return int next ID. | ||
207 | */ | ||
208 | public function getNextId() | ||
209 | { | ||
210 | if (!empty($this->ids)) { | ||
211 | return max(array_keys($this->ids)) + 1; | ||
212 | } | ||
213 | return 0; | ||
214 | } | ||
215 | |||
216 | /** | ||
217 | * @param $url | ||
218 | * | ||
219 | * @return Bookmark|null | ||
220 | */ | ||
221 | public function getByUrl($url) | ||
222 | { | ||
223 | if (! empty($url) | ||
224 | && isset($this->urls[$url]) | ||
225 | && isset($this->bookmarks[$this->urls[$url]]) | ||
226 | ) { | ||
227 | return $this->bookmarks[$this->urls[$url]]; | ||
228 | } | ||
229 | return null; | ||
230 | } | ||
231 | |||
232 | /** | ||
233 | * Reorder links by creation date (newest first). | ||
234 | * | ||
235 | * Also update the urls and ids mapping arrays. | ||
236 | * | ||
237 | * @param string $order ASC|DESC | ||
238 | */ | ||
239 | public function reorder($order = 'DESC') | ||
240 | { | ||
241 | $order = $order === 'ASC' ? -1 : 1; | ||
242 | // Reorder array by dates. | ||
243 | usort($this->bookmarks, function ($a, $b) use ($order) { | ||
244 | /** @var $a Bookmark */ | ||
245 | /** @var $b Bookmark */ | ||
246 | if ($a->isSticky() !== $b->isSticky()) { | ||
247 | return $a->isSticky() ? -1 : 1; | ||
248 | } | ||
249 | return $a->getCreated() < $b->getCreated() ? 1 * $order : -1 * $order; | ||
250 | }); | ||
251 | |||
252 | $this->urls = []; | ||
253 | $this->ids = []; | ||
254 | foreach ($this->bookmarks as $key => $bookmark) { | ||
255 | $this->urls[$bookmark->getUrl()] = $key; | ||
256 | $this->ids[$bookmark->getId()] = $key; | ||
257 | } | ||
258 | } | ||
259 | } | ||