Passed
Push — master ( 5af86a...598037 )
by Rudi
43s queued 11s
created

Pair::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 0
cts 5
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
namespace Ds;
3
4
use OutOfBoundsException;
5
6
/**
7
 * A pair which represents a key and an associated value.
8
 *
9
 * @package Ds
10
 */
11
final class Pair implements \JsonSerializable
12
{
13
    /**
14
     * @param mixed $key The pair's key
15
     */
16
    public $key;
17
18
    /**
19
     * @param mixed $value The pair's value
20
     */
21
    public $value;
22
23
    /**
24
     * Creates a new instance.
25
     *
26
     * @param mixed $key
27
     * @param mixed $value
28
     */
29
    public function __construct($key = null, $value = null)
30
    {
31
        $this->key   = $key;
32
        $this->value = $value;
33
    }
34
35
    /**
36
     * This allows unset($pair->key) to not completely remove the property,
37
     * but be set to null instead.
38
     *
39
     * @param mixed $name
40
     *
41
     * @return mixed|null
42
     */
43
    public function __unset($name)
44
    {
45
        if ($name === 'key' || $name === 'value') {
46
            $this->$name = null;
47
            return;
48
        }
49
50
        throw new OutOfBoundsException();
51
    }
52
53
    /**
54
     * Returns a copy of the Pair
55
     *
56
     * @return Pair
57
     */
58
    public function copy(): Pair
59
    {
60
        return new self($this->key, $this->value);
61
    }
62
63
    /**
64
     * Returns a representation to be used for var_dump and print_r.
65
     *
66
     * @return array
67
     */
68
    public function __debugInfo()
69
    {
70
        return $this->toArray();
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    public function toArray(): array
77
    {
78
        return ['key' => $this->key, 'value' => $this->value];
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function jsonSerialize()
85
    {
86
        return $this->toArray();
87
    }
88
89
    /**
90
     * Returns a string representation of the pair.
91
     */
92
    public function __toString()
93
    {
94
        return 'object(' . get_class($this) . ')';
95
    }
96
}
97