Skip to content

Commit 881660b

Browse files
refactor array list
1 parent 31729ca commit 881660b

File tree

4 files changed

+104
-74
lines changed

4 files changed

+104
-74
lines changed

src/DataStructures/ArrayList.php

Lines changed: 13 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,23 @@
1414
*/
1515
class ArrayList implements ListInterface
1616
{
17+
use CountTrait;
18+
use IteratorTrait;
19+
use SerializeTrait;
20+
1721
/**
1822
* List of data
23+
*
1924
* @var array
2025
*/
21-
protected $content;
26+
protected array $content;
2227

2328
/**
2429
* Current index
30+
*
2531
* @var int
2632
*/
27-
protected $position = 0;
33+
protected int $position = 0;
2834

2935
/**
3036
* ArrayList constructor.
@@ -51,7 +57,7 @@ public function __toString()
5157
*
5258
* @return mixed
5359
*/
54-
public function first()
60+
public function first(): mixed
5561
{
5662
$content = array_values($this->content);
5763
return array_shift($content);
@@ -62,7 +68,7 @@ public function first()
6268
*
6369
* @return mixed
6470
*/
65-
public function last()
71+
public function last(): mixed
6672
{
6773
$content = array_values($this->content);
6874
return array_pop($content);
@@ -74,7 +80,7 @@ public function last()
7480
* @param int|string $key KeyType to verify on list.
7581
* @return mixed
7682
*/
77-
public function get(int|string $key)
83+
public function get(int|string $key): mixed
7884
{
7985
if (array_key_exists($key, $this->content)) {
8086
return $this->content[$key];
@@ -116,12 +122,11 @@ public function has(int|string $key): bool
116122
}
117123

118124
/**
119-
* Remove a value by it's key on list
125+
* Remove a value by its key on list
120126
*
121127
* @param $key int|string to remove from list.
122-
* @return mixed
123128
*/
124-
public function remove(int|string $key)
129+
public function remove(int|string $key): void
125130
{
126131
unset($this->content[$key]);
127132
}
@@ -157,70 +162,4 @@ public function append(ListInterface $list): void
157162
$this->push($item);
158163
}
159164
}
160-
161-
/**
162-
* Return a JSON representation of list
163-
*
164-
* @return array|mixed
165-
*/
166-
public function jsonSerialize(): mixed
167-
{
168-
return $this->content;
169-
}
170-
171-
/**
172-
* Return the current element
173-
*
174-
* @return mixed
175-
*/
176-
public function current(): mixed
177-
{
178-
return $this->content[$this->position];
179-
}
180-
181-
/**
182-
* Move forward to next element
183-
*/
184-
public function next(): void
185-
{
186-
$this->position++;
187-
}
188-
189-
/**
190-
* Return the key of the current element
191-
*
192-
* @return mixed
193-
*/
194-
public function key(): mixed
195-
{
196-
return $this->position;
197-
}
198-
199-
/**
200-
* Checks if current position is valid
201-
*
202-
* @return bool
203-
*/
204-
public function valid(): bool
205-
{
206-
return isset($this->content[$this->position]);
207-
}
208-
209-
/**
210-
* Rewind the Iterator to the first element
211-
*/
212-
public function rewind(): void
213-
{
214-
$this->position = 0;
215-
}
216-
217-
/**
218-
* Count elements of an object
219-
*
220-
* @return int
221-
*/
222-
public function count(): int
223-
{
224-
return count($this->content);
225-
}
226165
}

src/DataStructures/CountTrait.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ArangoDB\DataStructures;
6+
7+
trait CountTrait
8+
{
9+
/**
10+
* Count elements of an object
11+
*
12+
* @return int
13+
*/
14+
public function count(): int
15+
{
16+
return count($this->content);
17+
}
18+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ArangoDB\DataStructures;
6+
7+
trait IteratorTrait
8+
{
9+
/**
10+
* Return the current element
11+
*
12+
* @return mixed
13+
*/
14+
public function current(): mixed
15+
{
16+
return $this->content[$this->position];
17+
}
18+
19+
/**
20+
* Move forward to next element
21+
*/
22+
public function next(): void
23+
{
24+
$this->position++;
25+
}
26+
27+
/**
28+
* Return the key of the current element
29+
*
30+
* @return mixed
31+
*/
32+
public function key(): mixed
33+
{
34+
return $this->position;
35+
}
36+
37+
/**
38+
* Checks if current position is valid
39+
*
40+
* @return bool
41+
*/
42+
public function valid(): bool
43+
{
44+
return isset($this->content[$this->position]);
45+
}
46+
47+
/**
48+
* Rewind the Iterator to the first element
49+
*/
50+
public function rewind(): void
51+
{
52+
$this->position = 0;
53+
}
54+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
4+
declare(strict_types=1);
5+
6+
namespace ArangoDB\DataStructures;
7+
8+
trait SerializeTrait
9+
{
10+
/**
11+
* Return a JSON representation of list
12+
*
13+
* @return array|mixed
14+
*/
15+
public function jsonSerialize(): mixed
16+
{
17+
return $this->content;
18+
}
19+
}

0 commit comments

Comments
 (0)