1
|
|
|
<?php |
2
|
|
|
namespace andmemasin\helpers; |
3
|
|
|
|
4
|
|
|
use yii\base\Component; |
5
|
|
|
use yii\base\InvalidArgumentException; |
6
|
|
|
use yii\base\Model; |
7
|
|
|
use yii\db\ActiveRecord; |
8
|
|
|
use yii\helpers\ArrayHelper; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* my additions to yii's default arrayhelper |
13
|
|
|
* |
14
|
|
|
* @package app\models\helpers |
15
|
|
|
* @author Tonis Ormisson <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class MyArrayHelper extends ArrayHelper { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Take a non-indexed array and make an indexed array using the value |
21
|
|
|
* as both index and value |
22
|
|
|
* @param array $array A NON-INDEXES single dimension array |
23
|
|
|
* @return array |
24
|
|
|
* @throws InvalidArgumentException |
25
|
|
|
*/ |
26
|
|
|
public static function selfIndex($array) { |
27
|
|
|
if (is_array($array)) { |
|
|
|
|
28
|
|
|
$out = []; |
29
|
|
|
foreach ($array as $value) { |
30
|
|
|
$out[$value] = $value; |
31
|
|
|
} |
32
|
|
|
return $out; |
33
|
|
|
} else { |
34
|
|
|
throw new InvalidArgumentException(gettype($array).' used as array in '.__CLASS__.'::'.__FUNCTION__); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Re-index an array based on a map. Map holds the indexes we want to apply to array. |
42
|
|
|
* @param array $map |
43
|
|
|
* @param array $array |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public static function mapIndex($map, $array) { |
47
|
|
|
if (is_array($array)) { |
|
|
|
|
48
|
|
|
$out = []; |
49
|
|
|
foreach ($map as $key => $value) { |
50
|
|
|
$out[$value] = $array[$key]; |
51
|
|
|
} |
52
|
|
|
return $out; |
53
|
|
|
} else { |
54
|
|
|
throw new InvalidArgumentException(gettype($array).' used as array in '.__CLASS__.'::'.__FUNCTION__); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Converts an non-indexed MULTIDIMENSIONAL array (such as data matrix from spreadsheet) |
62
|
|
|
* into an indexed array based on the $i-th element in the array. By default its the |
63
|
|
|
* first [0] element (header row). The indexing element will be excluded from output |
64
|
|
|
* array |
65
|
|
|
* @param array $array |
66
|
|
|
* @param integer $i |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public static function indexByRow($array, $i = 0) { |
70
|
|
|
$keys = $array[$i]; |
71
|
|
|
if (is_array($array) && !empty($array)) { |
72
|
|
|
$newArray = []; |
73
|
|
|
foreach ($array as $key=> $row) { |
74
|
|
|
// don'd add the indexing element into output |
75
|
|
|
if ($key != $i) { |
76
|
|
|
$newRow = []; |
77
|
|
|
$j = 0; |
78
|
|
|
foreach ($row as $cell) { |
79
|
|
|
$newRow[$keys[$j]] = $cell; |
80
|
|
|
$j++; |
81
|
|
|
} |
82
|
|
|
$newArray[] = $newRow; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $newArray; |
87
|
|
|
} else { |
88
|
|
|
throw new InvalidArgumentException(gettype($array).' used as array in '.__CLASS__.'::'.__FUNCTION__); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Take an multi-dimensional array and re-index it by a value in each array element |
96
|
|
|
* @param array $array |
97
|
|
|
* @param string $colName |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public static function indexByColumn($array, $colName) { |
101
|
|
|
if (is_array($array)) { |
|
|
|
|
102
|
|
|
if (!empty($array)) { |
103
|
|
|
$newArray = []; |
104
|
|
|
foreach ($array as $key => $row) { |
105
|
|
|
if ($row instanceof Component) { |
106
|
|
|
/** @var Model $row */ |
107
|
|
|
$rowArr = (array) $row->attributes; |
108
|
|
|
} else if (is_array($row)) { |
109
|
|
|
$rowArr = $row; |
110
|
|
|
} else { |
111
|
|
|
throw new InvalidArgumentException('Only arrays or yii Component objects can be used in '.__CLASS__.'::'.__FUNCTION__); |
112
|
|
|
} |
113
|
|
|
// make it array if input is object |
114
|
|
|
|
115
|
|
|
if (!isset($rowArr[$colName])) { |
116
|
|
|
throw new InvalidArgumentException('"'.$colName.'" missing as key in '.__CLASS__.'::'.__FUNCTION__); |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
$newArray[$rowArr[$colName]] = $row; |
120
|
|
|
} |
121
|
|
|
return $newArray; |
122
|
|
|
} |
123
|
|
|
// do nothing, empty array |
124
|
|
|
return $array; |
125
|
|
|
} else { |
126
|
|
|
throw new InvalidArgumentException(gettype($array).' used as array in '.__CLASS__.'::'.__FUNCTION__); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Remove element(s) from array based on their value |
132
|
|
|
* @param array $array Haystack |
133
|
|
|
* @param mixed $removeValue value that we look for to delete |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public static function removeByValue($array, $removeValue) { |
137
|
|
|
foreach ($array as $key =>$value) { |
138
|
|
|
if ($value === $removeValue) { |
139
|
|
|
unset($array[$key]); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
return $array; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Remove an Object from array of Objects by a value in one specified attribute |
147
|
|
|
* @param Component[] $models |
148
|
|
|
* @param string $attribute |
149
|
|
|
* @param string $value |
150
|
|
|
* @return Component[] |
151
|
|
|
*/ |
152
|
|
|
public static function removeModelByColumnValue($models, $attribute, $value) { |
153
|
|
|
if (!empty($models)) { |
154
|
|
|
foreach ($models as $key => $model) { |
155
|
|
|
if ($model->{$attribute} === $value) { |
156
|
|
|
unset($models[$key]); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
return $models; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|