Collection.php
3.23 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace OTGS\Installer;
class Collection {
/**
* @var array
*/
private $array;
/**
* The items contained in the collection.
*
* @var mixed[]
*/
protected $items = [];
private function __construct( array $array ) {
$this->array = $array;
}
/**
* @param array $array
*
* @return Collection
*/
public static function of( array $array ) {
return new static( $array );
}
/**
* @param callable $fn
*
* @return Collection
*/
public function filter( callable $fn ) {
return self::of( array_filter( $this->array, $fn ) );
}
/**
* @param callable $fn
*
* @return Collection
*/
public function map( callable $fn ) {
$keys = array_keys( $this->array );
$items = array_map( $fn, $this->array, $keys );
$combined = array_combine( $keys, $items );
return self::of( false !== $combined ? $combined : [] );
}
/**
* Converts array from key => vales to an array of pairs [ key, value ]
* @return Collection
*/
public function entities() {
$toPairs = function ( $value, $key ) { return [ $key, $value ]; };
return $this->map( $toPairs );
}
/**
* @param string $column
*
* @return Collection
*/
public function pluck( $column ) {
return self::of( array_column( $this->array, $column ) );
}
/**
* @param callable $fn
* @param mixed $initial
*
* @return mixed
*/
public function reduce( callable $fn, $initial = 0 ) {
return array_reduce( $this->array, $fn, $initial );
}
/**
* @return Collection
*/
public function values() {
return self::of( array_values( $this->array ) );
}
/**
* @param array $other
*
* @return Collection
*/
public function mergeRecursive( array $other ) {
return self::of( array_merge_recursive( $this->array, $other ) );
}
/**
* @param string $key
*
* @return mixed|Collection|NullCollection|array
*/
public function get( $key = null ) {
if ( null !== $key ) {
$data = array_key_exists( $key, $this->array ) ? $this->array[ $key ] : null;
if ( is_array( $data ) ) {
return self::of( $data );
} elseif ( is_null( $data ) ) {
return new NullCollection();
}
return $data;
}
return $this->array;
}
public function getOrNull( $key = null ) {
return $this->get( $key );
}
public function contains( $value ) {
return in_array( $value, $this->array, true );
}
public function head() {
if ( count( $this->array ) ) {
$temp = array_values( $this->array );
$result = $temp[0];
if ( is_array( $result ) ) {
return self::of( $result );
}
return $result;
}
return new NullCollection();
}
/**
* Determine if an item exists at an offset.
*
* @param mixed $key
* @return bool
*/
public function offsetExists($key)
{
return array_key_exists($key, $this->items);
}
/**
* Determine if an item exists in the collection by key.
*
* @param mixed $key
* @return bool
*/
public function has($key)
{
return $this->offsetExists($key);
}
}
class NullCollection {
public function map( callable $fn ) { return $this; }
public function filter( callable $fn ) { return $this; }
public function head() { return $this; }
public function pluck() { return $this; }
public function get() { return $this; }
public function getOrNull() { return null; }
}