1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the CSDataGridBundle package.
5
+ *
6
+ * (c) Pierre du Plessis <[email protected] >
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
12
+ namespace CS \DataGridBundle \Grid ;
13
+
14
+ use CS \DataGridBundle \Util \ArrayStack ;
15
+ use CS \DataGridBundle \Exception ;
16
+
17
+ class Collection extends ArrayStack
18
+ {
19
+ /**
20
+ * Adds a new element to the collection
21
+ *
22
+ * @param string $label
23
+ * @param integer $priority
24
+ */
25
+ public function add ($ label , $ priority = 0 )
26
+ {
27
+ $ this ->offsetSet ($ priority , $ label );
28
+
29
+ return $ this ;
30
+ }
31
+
32
+ /**
33
+ * Gets an element by name
34
+ *
35
+ * @param string $label
36
+ * @throws Exception\InvalidElementException
37
+ * @return mixed
38
+ */
39
+ public function get ($ label )
40
+ {
41
+ foreach ($ this ->all () as $ key => $ value )
42
+ {
43
+ if ((string ) $ value === $ label )
44
+ {
45
+ return $ value ;
46
+ }
47
+ }
48
+
49
+ throw new Exception \InvalidElementException (sprintf ("The element %s does not exist " , $ label ));
50
+ }
51
+
52
+ /**
53
+ * Checks if an element exists
54
+ *
55
+ * @param string $label
56
+ * @return bool
57
+ */
58
+ public function has ($ label )
59
+ {
60
+ try {
61
+ $ element = $ this ->get ($ label ) !== false ;
62
+ } catch (Exception \InvalidElementException $ e ) {
63
+ return false ;
64
+ }
65
+
66
+ return true ;
67
+ }
68
+
69
+ /**
70
+ * Removes an element from the collection
71
+ *
72
+ * @param array|string $label
73
+ */
74
+ public function remove ($ label )
75
+ {
76
+ $ labels = is_array ($ label ) ? $ label : (array ) $ label ;
77
+
78
+ unset($ label );
79
+
80
+ if ($ this ->count () > 0 ) {
81
+ foreach ($ this ->all () as $ key => $ data ) {
82
+ foreach ($ labels as $ label ) {
83
+ if (strtolower ((string ) $ data ) === strtolower ($ label )) {
84
+ $ this ->offsetUnset ($ key );
85
+ }
86
+ }
87
+ }
88
+ }
89
+
90
+ return $ this ;
91
+ }
92
+
93
+ /**
94
+ * Adds columns recursively
95
+ *
96
+ * @param array $data
97
+ * @return $this
98
+ */
99
+ public function addRecursive ($ data = array ())
100
+ {
101
+ if (is_array ($ data ) && !empty ($ data )) {
102
+ foreach ($ data as $ key => $ column ) {
103
+ $ this ->add ($ column , $ key );
104
+ }
105
+ }
106
+
107
+ return $ this ;
108
+ }
109
+ }
0 commit comments