ConvertibleTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 82
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
A toArray() 0 4 1
A toReadableString() 0 10 2
A toString() 0 4 1
A toJson() 0 14 2
1
<?php
2
3
namespace Arrayzy\Traits;
4
5
use Arrayzy\AbstractArray;
6
7
/**
8
 * Trait with helpful convertible methods.
9
 *
10
 * @author Victor Bocharsky <[email protected]>
11
 *
12
 * @property array $elements
13
 */
14
trait ConvertibleTrait
15
{
16
    /**
17
     * Convert the current array to a string with default separator.
18
     *
19
     * @return string A string representation of the current array
20
     */
21 3
    public function __toString()
22
    {
23 3
        return $this->toString();
24
    }
25
26
    /**
27
     * Convert the current array to a native PHP array.
28
     *
29
     * @return array A native PHP array
30
     */
31 197
    public function toArray()
32
    {
33 197
        return $this->elements;
34
    }
35
36
    /**
37
     * Implode the current array to a readable string with specified separator.
38
     *
39
     * @param string $separator The element's separator
40
     * @param string $conjunction The last element conjunction
41
     *
42
     * @return string A readable string representation of the current array
43
     *
44
     * @link http://php.net/manual/en/function.implode.php
45
     */
46 1
    public function toReadableString($separator = AbstractArray::DEFAULT_SEPARATOR, $conjunction = ' and ')
47
    {
48 1
        $elements = $this->elements;
49 1
        $lastElement = array_pop($elements);
50
51 1
        $string = implode($separator, $elements) . (count($elements) ? $conjunction : '') . $lastElement;
52 1
        unset($elements, $lastElement);
53
54 1
        return $string;
55
    }
56
57
    /**
58
     * Implode the current array to a string with specified separator.
59
     *
60
     * @param string $separator The element's separator
61
     *
62
     * @return string A string representation of the current array
63
     *
64
     * @link http://php.net/manual/en/function.implode.php
65
     */
66 3
    public function toString($separator = AbstractArray::DEFAULT_SEPARATOR)
67
    {
68 3
        return implode($separator, $this->elements);
69
    }
70
71
    /**
72
     * Encode the current array to a JSON string.
73
     *
74
     * @param int $options The bitmask
75
     * @param int $depth The maximum depth (must be greater than zero)
76
     *
77
     * @return string A JSON string representation of the current array
78
     *
79
     * @link http://php.net/manual/en/function.json-encode.php
80
     */
81 4
    public function toJson($options = 0, $depth = 512)
82
    {
83
        $params = [
84 4
            $this->elements,
85 4
            $options,
86 4
        ];
87
88 4
        if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
89 4
            $params[] = $depth;
90 4
        }
91
92
        // use call_user_func_array() here to fully cover this method in test
93 4
        return call_user_func_array('json_encode', $params);
94
    }
95
}
96