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