Completed
Push — work-fleets ( 82c271...98be23 )
by SuperNova.WS
05:29
created

HelperArray::merge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

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