Completed
Push — master ( 5856c5...90f7ba )
by Rudi
05:12
created

Pair   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 86
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __get() 0 9 3
A copy() 0 4 1
A __debugInfo() 0 4 1
A toArray() 0 4 1
A jsonSerialize() 0 4 1
A __toString() 0 4 1
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 713
    public function __construct($key = null, $value = null)
30
    {
31 713
        $this->key   = $key;
32 713
        $this->value = $value;
33 713
    }
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 3
    public function __get($name)
44
    {
45 3
        if ($name === 'key' || $name === 'value') {
46 2
            $this->$name = null;
47 2
            return;
48
        }
49
50 1
        throw new OutOfBoundsException();
51
    }
52
53
    /**
54
     * Returns a copy of the Pair
55
     *
56
     * @return Pair
57
     */
58 30
    public function copy(): Pair
59
    {
60 30
        return new self($this->key, $this->value);
61
    }
62
63
    /**
64
     * Debug Info
65
     *
66
     * @return array
67
     */
68 3
    public function __debugInfo()
69
    {
70 3
        return $this->toArray();
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76 8
    public function toArray(): array
77
    {
78 8
        return ['key' => $this->key, 'value' => $this->value];
79
    }
80
81
    /**
82
     *
83
     */
84 1
    public function jsonSerialize()
85
    {
86 1
        return $this->toArray();
87
    }
88
89
    /**
90
     * To String
91
     */
92 1
    public function __toString()
93
    {
94 1
        return 'object(' . get_class($this) . ')';
95
    }
96
}
97