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
|
<?php
namespace TenQuality\Data;
use ArrayObject; use TenQuality\Data\Contracts\Arrayable; use TenQuality\Data\Contracts\Stringable; use TenQuality\Data\Contracts\JSONable;
/** * Collection of data. * * @author Cami M <[email protected]> * @copyright 10 Quality <[email protected]> * @package TenQuality\Data\Model * @version 1.0.2 */ class Collection extends ArrayObject implements Arrayable, JSONable, Stringable { /** * Returns collection sorted by specific attribute name. * Only works with Models or arrays with string defined keys. * @since 1.0.2 * * @link http://php.net/manual/en/array.constants.php * * @param string $attribute Attribute to sort by. * @param string $sortFlag Sort direction. Use PHP sort constants * * @return array (this for chaining) */ public function sortBy($attribute, $sortFlag = SORT_REGULAR) { $values = array(); for ($i = count( $this ) -1; $i >= 0; --$i) { $values[] = $this[$i]->$attribute; } $values = array_unique($values); sort($values, $sortFlag); $new = new self(); foreach ($values as $value) { for ($i = count($this) -1; $i >= 0; --$i) { if ($value == $this[$i]->$attribute) { $new[] = $this[$i]; } } } return $new; }
/** * Returns collection grouped by an attribute's name. * Only works with Models or arrays with string defined keys. * @since 1.0.2 * * @param string $attribute Attribute to group by. * * @return array (this for chaining) */ public function groupby($attribute) { $new = new self(); for ($i = 0; $i < count( $this ); ++$i) { $key = (string)$this[$i]->$attribute; if (!isset( $new[$key])) $new[$key] = new self(); $new[$key][] = $this[$i]; } return $new; } /** * Returns collection as pure array. * Does depth array casting. * @since 1.0.2 * * @return array */ public function __toArray() { $output = []; $value = null; foreach ($this as $key => $value) { $output[$key] = !is_object($value) ? $value : (method_exists($value, '__toArray') ? $value->__toArray() : (array)$value ); } return $output; } /** * Returns collection as pure array. * Does depth array casting. * @since 1.0.2 * * @return array */ public function toArray() { return $this->__toArray(); } /** * Returns collection as a string. * @since 1.0.2 * * @param string */ public function __toString() { return json_encode($this->__toArray()); } /** * Returns object as JSON string. * @since 1.0.2 * * @link http://php.net/manual/en/function.json-encode.php * * @param int $options JSON encoding options. See @link. * @param int $depth JSON encoding depth. See @link. * * @return string */ public function __toJSON($options = 0, $depth = 512) { return json_encode($this->__toArray(), $options, $depth); } /** * Returns object as JSON string. * @since 1.0.2 * * @link http://php.net/manual/en/function.json-encode.php * * @param int $options JSON encoding options. See @link. * @param int $depth JSON encoding depth. See @link. * * @return string */ public function toJSON($options = 0, $depth = 512) { return $this->__toJSON($options, $depth); } }
|