Completed
Pull Request — development (#673)
by Nick
12:54 queued 04:44
created

ArrayUtil   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A explodeTrim() 0 14 3
A humanImplode() 0 15 2
1
<?php
2
3
namespace Oc\Util;
4
5
class ArrayUtil
6
{
7
    /**
8
     * Explodes the given $string by $delimiter, trims all values and only adds non empty values to the result array.
9
     *
10
     * @param string $delimiter
11
     * @param string $string
12
     *
13
     * @return array
14
     */
15
    public static function explodeTrim($delimiter, $string)
16
    {
17
        $result = [];
18
        $values = explode($delimiter, $string);
19
20
        foreach ($values as $value) {
21
            $value = trim($value);
22
            if ($value !== '') {
23
                $result[] = $value;
24
            }
25
        }
26
27
        return $result;
28
    }
29
30
    /**
31
     * Implodes an array of strings in a human language matter.
32
     *
33
     * With default values: ['Foo', 'bar', 'baz'] => 'Foo, bar and baz'
34
     *
35
     * @param array $pieces
36
     * @param string $conjunction
37
     * @param string $glue
38
     *
39
     * @return mixed|string
40
     */
41
    public static function humanImplode(array $pieces, $conjunction = 'and', $glue = ',')
42
    {
43
        $lastElement = array_pop($pieces);
44
45
        if (!empty($pieces)) {
46
            return $lastElement;
47
        }
48
49
        return sprintf(
50
            '%s %s %s',
51
            implode($glue . ' ', $pieces),
52
            $conjunction,
53
            $lastElement
54
        );
55
    }
56
}
57