Completed
Push — work-fleets ( 1beb80...0f036f )
by SuperNova.WS
09:30 queued 03:01
created

HelperArray   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 67.61%

Importance

Changes 12
Bugs 0 Features 0
Metric Value
dl 0
loc 208
ccs 48
cts 71
cp 0.6761
rs 9.2
c 12
b 0
f 0
wmc 34
lcom 1
cbo 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A stringToArray() 0 3 3
A makeArrayRef() 0 5 2
A makeArray() 0 5 1
B filter() 0 14 5
A map() 0 13 4
A filterEmpty() 0 3 1
A stringToArrayFilterEmpty() 0 3 1
A merge() 0 14 2
A keyExistsOr() 0 3 2
B cloneDeep() 0 13 6
B array_repack() 0 17 7
1
<?php
2
3
class HelperArray {
4
5
  /**
6
   * Overwrites old array with new
7
   */
8
  const MERGE_OVERWRITE = 0;
9
  /**
10
   * Merges old array with new with array_merge()
11
   * String keys replaced, numeric keys renumbered
12
   */
13
  const MERGE_PHP = 1;
14
15
16
  /**
17
   * No array cloning - just stay with same objects
18
   */
19
  const CLONE_ARRAY_NONE = 0;
20
  /**
21
   * Clone objects on first level of array
22
   */
23
  const CLONE_ARRAY_SHALLOW = 1;
24
  /**
25
   * Clone objects recursive on any array level
26
   */
27
  const CLONE_ARRAY_RECURSIVE = 2;
28
29
  /**
30
   * Convert $delimiter delimited string to array
31
   *
32
   * @param mixed  $string
33
   * @param string $delimiter
34
   *
35
   * @return array
36
   */
37 6
  public static function stringToArray($string, $delimiter = ',') {
38 6
    return is_string($string) && !empty($string) ? explode($delimiter, $string) : array();
39
  }
40
41
  /**
42
   * Convert single value to array by reference
43
   *
44
   * @param mixed &$value
45
   */
46 3
  public static function makeArrayRef(&$value, $index = 0) {
47 3
    if (!is_array($value)) {
48 2
      $value = array($index => $value);
49 2
    }
50 3
  }
51
52
53
  /**
54
   * Convert single value to array
55
   *
56
   * @param mixed $value
57
   *
58
   * @return array
59
   */
60 3
  public static function makeArray($value, $index = 0) {
61 3
    static::makeArrayRef($value, $index);
62
63 3
    return $value;
64
  }
65
66
  /**
67
   * Filters array by callback
68
   *
69
   * @param mixed    $array
70
   * @param callable $callback
71
   *
72
   * @return array
73
   */
74 15
  public static function filter(&$array, $callback) {
75 15
    $result = array();
76
77 15
    if (is_array($array) && !empty($array)) {
78
      // TODO - array_filter
79 10
      foreach ($array as $value) {
80 10
        if (call_user_func($callback, $value)) {
81 6
          $result[] = $value;
82 6
        }
83 10
      }
84 10
    }
85
86 15
    return $result;
87
  }
88
89
  /**
90
   * Filters array by callback
91
   *
92
   * @param mixed    $array
93
   * @param callable $callback
94
   * @param bool     $withKeys
95
   *
96
   * @return array
97
   */
98
  public static function map(&$array, $callback, $withKeys = false) {
99
    $result = array();
100
101
    if (is_array($array) && !empty($array)) {
102
      if ($withKeys) {
103
        $result = array_map($callback, $array, array_keys($array));
104
      } else {
105
        $result = array_map($callback, $array);
106
      }
107
    }
108
109
    return $result;
110
  }
111
112
  /**
113
   * Filter empty() values from array
114
   *
115
   * @param $array
116
   *
117
   * @return array
118
   */
119 8
  public static function filterEmpty($array) {
120 8
    return static::filter($array, 'Validators::isNotEmpty');
121
  }
122
123
  /**
124
   * @param string $string
125
   * @param string $delimiter
126
   *
127
   * @return array
128
   */
129 1
  public static function stringToArrayFilterEmpty($string, $delimiter = ',') {
130 1
    return static::filterEmpty(static::stringToArray($string, $delimiter));
131
  }
132
133
  /**
134
   * @param mixed|array &$arrayOld
135
   * @param mixed|array $arrayNew
136
   * @param int         $mergeStrategy - default is HelperArray::MERGE_OVERWRITE
137
   */
138 1
  public static function merge(&$arrayOld, $arrayNew = array(), $mergeStrategy = HelperArray::MERGE_OVERWRITE) {
139 1
    static::makeArrayRef($arrayNew);
140 1
    static::makeArrayRef($arrayOld);
141
142
    switch ($mergeStrategy) {
143 1
      case HelperArray::MERGE_PHP:
144 1
        $arrayOld = array_merge($arrayOld, $arrayNew);
145 1
      break;
146
147 1
      default:
148 1
        $arrayOld = $arrayNew;
149 1
      break;
150 1
    }
151 1
  }
152
153
  /**
154
   * Checks if key exists in array. If yes - return value on key otherwise return default value
155
   *
156
   * @param array &$array
157
   * @param mixed $key
158
   * @param mixed $default
159
   *
160
   * @return mixed
161
   */
162 1
  public static function keyExistsOr(&$array, $key, $default) {
163 1
    return array_key_exists($key, $array) ? $array[$key] : $default;
164
  }
165
166
  /**
167
   * Clone objects in array
168
   *
169
   * @param array &$array - Any dimensional array with presumed objects in there
170
   * @param int   $deep - HelperArray::CLONE_ARRAY_xxx constants
171
   */
172 3
  public static function cloneDeep(&$array, $deep = HelperArray::CLONE_ARRAY_RECURSIVE) {
173 3
    if ($deep == HelperArray::CLONE_ARRAY_NONE) {
174 1
      return;
175
    }
176
177 2
    foreach ($array as &$value) {
178 2
      if (is_object($value)) {
179 2
        $value = clone $value;
180 2
      } elseif (is_array($value) && $deep == HelperArray::CLONE_ARRAY_RECURSIVE) {
181 1
        static::cloneDeep($value, $deep);
182 1
      }
183 2
    }
184 2
  }
185
186
  /**
187
   * Repacking array to provided level, removing null elements
188
   *
189
   * @param array &$array
190
   * @param int   $level
191
   */
192
  public static function array_repack(&$array, $level = 0) {
193
    // TODO $lock_table не нужна тут
194
    if (!is_array($array)) {
195
      return;
196
    }
197
198
    foreach ($array as $key => &$value) {
199
      if ($value === null) {
200
        unset($array[$key]);
201
      } elseif ($level > 0 && is_array($value)) {
202
        static::array_repack($value, $level - 1);
203
        if (empty($value)) {
204
          unset($array[$key]);
205
        }
206
      }
207
    }
208
  }
209
210
}
211