Completed
Push — work-fleets ( ff9a05...837dd8 )
by SuperNova.WS
05:12
created

HelperArray::merge()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0327

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 17
rs 9.4285
ccs 11
cts 13
cp 0.8462
crap 3.0327
1
<?php
2
3
class HelperArray {
4
5
  const ARRAY_REPLACE = 0;
6
  const ARRAY_MERGE = 1;
7
8
  /**
9
   * Convert $delimiter delimited string to array
10
   *
11
   * @param mixed  $string
12
   * @param string $delimiter
13
   *
14
   * @return array
15
   */
16 6
  public static function stringToArray($string, $delimiter = ',') {
17 6
    return is_string($string) && !empty($string) ? explode($delimiter, $string) : array();
18
  }
19
20
  /**
21
   * Convert single value to array by reference
22
   *
23
   * @param mixed &$value
24
   */
25 1
  public static function makeArrayRef(&$value, $index = 0) {
26 1
    !is_array($value) ? $value = array($index => $value) : false;
27 1
  }
28
29
30
  /**
31
   * Convert single value to array
32
   *
33
   * @param mixed $value
34
   *
35
   * @return array
36
   */
37 1
  public static function makeArray($value, $index = 0) {
38 1
    static::makeArrayRef($value, $index);
39
40 1
    return $value;
41
  }
42
43
  /**
44
   * Filters array by callback
45
   *
46
   * @param mixed    $array
47
   * @param callable $callback
48
   *
49
   * @return array
50
   */
51 11
  public static function filter(&$array, $callback) {
52 11
    $result = array();
53
54 11
    if (is_array($array) && !empty($array)) {
55 6
      foreach ($array as $value) {
56 6
        if (call_user_func($callback, $value)) {
57 2
          $result[] = $value;
58 2
        }
59 6
      }
60 6
    }
61
62 11
    return $result;
63
  }
64
65
  /**
66
   * Filter empty() values from array
67
   *
68
   * @param $array
69
   *
70
   * @return array
71
   */
72 6
  public static function filterEmpty($array) {
73 6
    return static::filter($array, 'Validators::isNotEmpty');
74
  }
75
76
  /**
77
   * @param string $string
78
   * @param string $delimiter
79
   *
80
   * @return array
81
   */
82 1
  public static function stringToArrayFilterEmpty($string, $delimiter = ',') {
83 1
    return static::filterEmpty(static::stringToArray($string, $delimiter));
84
  }
85
86
  /**
87
   * @param mixed|array &$arrayOld
88
   * @param mixed|array $arrayNew
89
   * @param int         $mergeStrategy - default is HelperArray::ARRAY_REPLACE
90
   */
91 1
  public static function merge(&$arrayOld, $arrayNew = array(), $mergeStrategy = HelperArray::ARRAY_REPLACE) {
92 1
    if (!is_array($arrayOld)) {
93
      $arrayOld = array($arrayOld);
94
    }
95
96 1
    static::makeArrayRef($arrayNew);
97
98
    switch ($mergeStrategy) {
99 1
      case HelperArray::ARRAY_MERGE:
100 1
        $arrayOld = array_merge($arrayOld, $arrayNew);
101 1
      break;
102
103 1
      default:
104 1
        $arrayOld = $arrayNew;
105 1
      break;
106 1
    }
107 1
  }
108
109
}
110