Completed
Push — work-fleets ( 4edb07...0059e8 )
by SuperNova.WS
06:33
created

HelperArray   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
dl 0
loc 143
ccs 35
cts 35
cp 1
rs 10
wmc 22
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 9 5
1
<?php
2
3
class HelperArray {
4
5
  /**
6
   * Overwrites old array with new
7
   */
8
  const 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
  const CLONE_NONE = 0;
17
  const CLONE_SHALLOW = 1;
18
  const CLONE_DEEP = 2;
19
20
  /**
21
   * Convert $delimiter delimited string to array
22
   *
23 6
   * @param mixed  $string
24 6
   * @param string $delimiter
25
   *
26
   * @return array
27
   */
28
  public static function stringToArray($string, $delimiter = ',') {
29
    return is_string($string) && !empty($string) ? explode($delimiter, $string) : array();
30
  }
31
32 3
  /**
33 3
   * Convert single value to array by reference
34 2
   *
35 2
   * @param mixed &$value
36 3
   */
37
  public static function makeArrayRef(&$value, $index = 0) {
38
    if (!is_array($value)) {
39
      $value = array($index => $value);
40
    }
41
  }
42
43
44
  /**
45
   * Convert single value to array
46 3
   *
47 3
   * @param mixed $value
48
   *
49 3
   * @return array
50
   */
51
  public static function makeArray($value, $index = 0) {
52
    static::makeArrayRef($value, $index);
53
54
    return $value;
55
  }
56
57
  /**
58
   * Filters array by callback
59
   *
60 15
   * @param mixed    $array
61 15
   * @param callable $callback
62
   *
63 15
   * @return array
64 10
   */
65 10
  public static function filter(&$array, $callback) {
66 6
    $result = array();
67 6
68 10
    if (is_array($array) && !empty($array)) {
69 10
      foreach ($array as $value) {
70
        if (call_user_func($callback, $value)) {
71 15
          $result[] = $value;
72
        }
73
      }
74
    }
75
76
    return $result;
77
  }
78
79
  /**
80
   * Filter empty() values from array
81 8
   *
82 8
   * @param $array
83
   *
84
   * @return array
85
   */
86
  public static function filterEmpty($array) {
87
    return static::filter($array, 'Validators::isNotEmpty');
88
  }
89
90
  /**
91 1
   * @param string $string
92 1
   * @param string $delimiter
93
   *
94
   * @return array
95
   */
96
  public static function stringToArrayFilterEmpty($string, $delimiter = ',') {
97
    return static::filterEmpty(static::stringToArray($string, $delimiter));
98
  }
99
100 1
  /**
101 1
   * @param mixed|array &$arrayOld
102 1
   * @param mixed|array $arrayNew
103
   * @param int         $mergeStrategy - default is HelperArray::ARRAY_REPLACE
104
   */
105 1
  public static function merge(&$arrayOld, $arrayNew = array(), $mergeStrategy = HelperArray::OVERWRITE) {
106 1
    static::makeArrayRef($arrayNew);
107 1
    static::makeArrayRef($arrayOld);
108
109 1
    switch($mergeStrategy) {
110 1
      case HelperArray::MERGE_PHP:
111 1
        $arrayOld = array_merge($arrayOld, $arrayNew);
112 1
      break;
113 1
114
      default:
115
        $arrayOld = $arrayNew;
116
      break;
117
    }
118
  }
119
120
  /**
121
   * @param array &$array
122
   * @param mixed $key
123
   * @param mixed $alternative
124
   *
125
   * @return mixed
126
   */
127
  public static function keyExistsOr(&$array, $key, $alternative) {
128
    return array_key_exists($key, $array) ? $array[$key] : $alternative;
129
  }
130
131
  /**
132
   * @param array &$array
133
   * @param int   $deep
134
   */
135
  public static function cloneDeep(&$array, $deep = HelperArray::CLONE_DEEP) {
136
    foreach ($array as &$value) {
137
      if (is_object($value)) {
138
        $value = clone $value;
139
      } elseif (is_array($value) && $deep == HelperArray::CLONE_DEEP) {
140
        static::cloneDeep($value, $deep);
141
      }
142
    }
143
  }
144
145
}
146