Completed
Push — work-fleets ( d64c53...5442ac )
by SuperNova.WS
05:10
created

HelperArray   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 108
rs 10
ccs 20
cts 20
cp 1
wmc 16
lcom 1
cbo 0

7 Methods

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