Completed
Push — work-fleets ( e0e753...5ee2f8 )
by SuperNova.WS
05:10
created

HelperArray::stringToArray()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 2
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
class HelperArray {
4
5
  /**
6
   * Convert $delimiter delimited string to array
7
   *
8
   * @param mixed  $string
9
   * @param string $delimiter
10
   *
11
   * @return array
12
   */
13
  public static function stringToArray($string, $delimiter = ',') {
14
    return is_string($string) && !empty($string) ? explode($delimiter, $string) : array();
15
  }
16
17
  /**
18
   * Convert single value to array
19
   *
20
   * @param mixed $value
21
   *
22
   * @return array
23
   */
24
  public static function makeArray(&$value, $index = 0) {
25
    return !is_array($value) ? array($index => $value) : $value;
26
  }
27
28
  /**
29
   * @param mixed    $array
30
   * @param callable $callback
31
   *
32
   * @return array
33
   */
34
  public static function filter(&$array, $callback) {
35
    $result = array();
36
37
    if (is_array($array) && !empty($array)) {
38
      foreach ($array as $value) {
39
        if (call_user_func($callback, $value)) {
40
          $result[] = $value;
41
        }
42
      }
43
44
    }
45
46
    return $result;
47
  }
48
49
  protected static function isNotEmpty($value) {
50
    return !empty($value);
51
  }
52
53
  /**
54
   * Filter empty() values from array
55
   *
56
   * @param $array
57
   *
58
   * @return array
59
   */
60
  public static function filterEmpty($array) {
61
    return static::filter($array, array(get_called_class(), 'isNotEmpty'));
62
  }
63
64
  public static function stringToArrayFilterEmpty($string, $delimiter = ',') {
65
    return static::filterEmpty(static::stringToArray($string, $delimiter));
66
  }
67
68
}
69