Completed
Push — master ( d221a8...d5a821 )
by Rudi
03:20
created

Pair::copy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
     * Constructor
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 __get($name)
44
    {
45
        if ($name === 'key' || $name === 'value') {
46
            $this->$name = null;
47
            return;
48
        }
49
50
        throw new OutOfBoundsException();
51
    }
52
53
    /**
54
     * Debug Info
55
     *
56
     * @return array
57
     */
58
    public function __debugInfo()
59
    {
60
        return $this->toArray();
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function toArray(): array
67
    {
68
        return ['key' => $this->key, 'value' => $this->value];
69
    }
70
71
    /**
72
     *
73
     */
74
    public function jsonSerialize()
75
    {
76
        return $this->toArray();
77
    }
78
79
    /**
80
     * To String
81
     */
82
    public function __toString()
83
    {
84
        return 'object(' . get_class($this) . ')';
85
    }
86
}
87