|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Nozavroni/Collections |
|
4
|
|
|
* Just another collections library for PHP5.6+. |
|
5
|
|
|
* @version {version} |
|
6
|
|
|
* @copyright Copyright (c) 2017 Luke Visinoni <[email protected]> |
|
7
|
|
|
* @author Luke Visinoni <[email protected]> |
|
8
|
|
|
* @license https://github.com/deni-zen/csvelte/blob/master/LICENSE The MIT License (MIT) |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace Noz\Collection; |
|
11
|
|
|
|
|
12
|
|
|
use InvalidArgumentException; |
|
13
|
|
|
|
|
14
|
|
|
use Countable; |
|
15
|
|
|
use Traversable; |
|
16
|
|
|
use SplDoublyLinkedList; |
|
17
|
|
|
|
|
18
|
|
|
use Noz\Contracts\Structure\Listable; |
|
19
|
|
|
use Noz\Contracts\Immutable; |
|
20
|
|
|
use Noz\Contracts\Arrayable; |
|
21
|
|
|
use Noz\Contracts\Invokable; |
|
22
|
|
|
|
|
23
|
|
|
use Noz\Traits\IsArrayable; |
|
24
|
|
|
use Noz\Traits\IsImmutable; |
|
25
|
|
|
|
|
26
|
|
|
use function |
|
27
|
|
|
Noz\to_array, |
|
28
|
|
|
Noz\is_traversable; |
|
29
|
|
|
|
|
30
|
|
|
class LList implements |
|
|
|
|
|
|
31
|
|
|
Listable, |
|
32
|
|
|
Immutable, |
|
33
|
|
|
Countable, |
|
34
|
|
|
Arrayable, |
|
35
|
|
|
Invokable |
|
36
|
|
|
{ |
|
37
|
|
|
use IsImmutable, IsArrayable; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var SplDoublyLinkedList |
|
41
|
|
|
*/ |
|
42
|
|
|
private $data; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* LList constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param array|Traversable $data The list constructor |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct($data) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->setData($data); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set internal data array. |
|
56
|
|
|
* |
|
57
|
|
|
* @param array|Traversable $data The list constructor |
|
58
|
|
|
*/ |
|
59
|
|
|
private function setData($data) |
|
60
|
|
|
{ |
|
61
|
|
|
if (!is_traversable($data)) { |
|
62
|
|
|
throw new InvalidArgumentException( |
|
63
|
|
|
'%s expects traversable data.', |
|
64
|
|
|
__CLASS__ |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
$dll = new SplDoublyLinkedList($data); |
|
|
|
|
|
|
68
|
|
|
$dll->setIteratorMode(SplDoublyLinkedList::IT_MODE_KEEP); |
|
69
|
|
|
foreach ($data as $key => $val) { |
|
70
|
|
|
$dll->push($val); |
|
71
|
|
|
} |
|
72
|
|
|
$this->data = $dll; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get internal data array. |
|
77
|
|
|
* |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function getData() |
|
81
|
|
|
{ |
|
82
|
|
|
return to_array($this->data); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Invoke LList. |
|
87
|
|
|
* |
|
88
|
|
|
* This method is called when a LList object is invoked (called as if it were a function). |
|
89
|
|
|
* |
|
90
|
|
|
* @return mixed |
|
91
|
|
|
*/ |
|
92
|
|
|
public function __invoke() |
|
93
|
|
|
{ |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Count elements of an object |
|
99
|
|
|
* @link http://php.net/manual/en/countable.count.php |
|
100
|
|
|
* @return int The custom count as an integer. |
|
|
|
|
|
|
101
|
|
|
* </p> |
|
102
|
|
|
* <p> |
|
103
|
|
|
* The return value is cast to an integer. |
|
104
|
|
|
* @since 5.1.0 |
|
105
|
|
|
*/ |
|
106
|
|
|
public function count() |
|
107
|
|
|
{ |
|
108
|
|
|
// TODO: Implement count() method. |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|