Completed
Push — work-fleets ( b0e897...355465 )
by SuperNova.WS
13:11 queued 07:54
created

HelperArray::makeArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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