Completed
Pull Request — development (#404)
by Mirko
15:54 queued 08:42
created

ArrayUtil::humanLangImplode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace AppBundle\Util;
4
5
class ArrayUtil
6
{
7
    /**
8
     * @param string $delimiter
9
     * @param string $string
10
     *
11
     * @return array
12
     */
13
    public static function trimExplode($delimiter, $string)
14
    {
15
        $result = [];
16
        $temp = explode($delimiter, $string);
17
        foreach ($temp as $value) {
18
            $value = trim($value);
19
            if ($value !== '') {
20
                $result[] = $value;
21
            }
22
        }
23
24
        return $result;
25
    }
26
27
    /**
28
     * @param array $pieces
29
     * @param string $conjunction
30
     * @param string $glue
31
     *
32
     * @return mixed|string
33
     */
34
    public static function humanLangImplode(array $pieces, $conjunction = 'and', $glue = ',')
35
    {
36
        $lastElement = array_pop($pieces);
37
        if (!empty($pieces)) {
38
            return implode($glue . ' ', $pieces) . ' ' . $conjunction . ' ' . $lastElement;
39
        }
40
41
        return $lastElement;
42
    }
43
}
44