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