Completed
Push — master ( ed33b0...7335c0 )
by Bocharsky
04:31
created

ConvertibleTrait   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 32
c 5
b 1
f 2
lcom 1
cbo 0
dl 0
loc 81
ccs 20
cts 20
cp 1
rs 9.6

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 13 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
     * Converts array to a string with default separator.
18
     *
19
     * @return string The string representation of array
20
     */
21 3
    public function __toString()
22
    {
23 3
        return $this->toString();
24
    }
25
26
    /**
27
     * Converts instance to a native PHP array.
28
     *
29
     * @return array The native PHP array
30
     */
31 196
    public function toArray()
32
    {
33 196
        return $this->elements;
34
    }
35
36
    /**
37
     * Implodes 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 The readable string representation of 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
     * Implodes array to a string with specified separator.
59
     *
60
     * @param string $separator The element's separator
61
     *
62
     * @return string The string representation of 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
     * Encodes 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 The JSON string representation of 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 4
        return call_user_func_array('json_encode', $params);
93
    }
94
}
95