Completed
Push — work-fleets ( 0059e8...71ccb1 )
by SuperNova.WS
06:08
created

HelperArray   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
c 8
b 0
f 0
dl 0
loc 158
ccs 48
cts 48
cp 1
rs 10
wmc 23
lcom 1
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A stringToArray() 0 3 3
A makeArrayRef() 0 5 2
A makeArray() 0 5 1
B filter() 0 13 5
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
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 10
      foreach ($array as $value) {
79 10
        if (call_user_func($callback, $value)) {
80 6
          $result[] = $value;
81 6
        }
82 10
      }
83 10
    }
84
85 15
    return $result;
86
  }
87
88
  /**
89
   * Filter empty() values from array
90
   *
91
   * @param $array
92
   *
93
   * @return array
94
   */
95 8
  public static function filterEmpty($array) {
96 8
    return static::filter($array, 'Validators::isNotEmpty');
97
  }
98
99
  /**
100
   * @param string $string
101
   * @param string $delimiter
102
   *
103
   * @return array
104
   */
105 1
  public static function stringToArrayFilterEmpty($string, $delimiter = ',') {
106 1
    return static::filterEmpty(static::stringToArray($string, $delimiter));
107
  }
108
109
  /**
110
   * @param mixed|array &$arrayOld
111
   * @param mixed|array $arrayNew
112
   * @param int         $mergeStrategy - default is HelperArray::MERGE_OVERWRITE
113
   */
114 1
  public static function merge(&$arrayOld, $arrayNew = array(), $mergeStrategy = HelperArray::MERGE_OVERWRITE) {
115 1
    static::makeArrayRef($arrayNew);
116 1
    static::makeArrayRef($arrayOld);
117
118
    switch($mergeStrategy) {
119 1
      case HelperArray::MERGE_PHP:
120 1
        $arrayOld = array_merge($arrayOld, $arrayNew);
121 1
      break;
122
123 1
      default:
124 1
        $arrayOld = $arrayNew;
125 1
      break;
126 1
    }
127 1
  }
128
129
  /**
130
   * @param array &$array
131
   * @param mixed $key
132
   * @param mixed $alternative
133
   *
134
   * @return mixed
135
   */
136 1
  public static function keyExistsOr(&$array, $key, $alternative) {
137 1
    return array_key_exists($key, $array) ? $array[$key] : $alternative;
138
  }
139
140
  /**
141
   * Clone objects in array
142
   *
143
   * @param array &$array - Any dimensional array with presumed objects in there
144
   * @param int   $deep - HelperArray::CLONE_ARRAY_xxx constants
145
   */
146 3
  public static function cloneDeep(&$array, $deep = HelperArray::CLONE_ARRAY_RECURSIVE) {
147 3
    if ($deep == HelperArray::CLONE_ARRAY_NONE) {
148 1
      return;
149
    }
150
151 2
    foreach ($array as &$value) {
152 2
      if (is_object($value)) {
153 2
        $value = clone $value;
154 2
      } elseif (is_array($value) && $deep == HelperArray::CLONE_ARRAY_RECURSIVE) {
155 1
        static::cloneDeep($value, $deep);
156 1
      }
157 2
    }
158 2
  }
159
160
}
161