Completed
Branch master (6af305)
by Andrii
02:24
created

CollectionTrait::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * Collection library for PHP
5
 *
6
 * @link      https://github.com/hiqdev/php-collection
7
 * @package   php-collection
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\php\collection;
13
14
/**
15
 * Collection Trait.
16
 * Array inside of object.
17
 */
18
trait CollectionTrait
19
{
20
    use BaseTrait;
21
22
    /**
23
     * Returns property of item by name.
24
     *
25
     * @param mixed $name
26
     *
27
     * @return mixed
28
     */
29
    public function get($name)
30
    {
31
        return $this->getItem($name);
32
    }
33
34
    /**
35
     * Sets an item. Silently resets if already exists.
36
     *
37
     * @param int|string   $name
38
     * @param mixed        $value the element value
39
     * @param string|array $where where to put, see [[setItem()]]
40
     *
41
     * @see setItem()
42
     */
43 3
    public function set($name, $value, $where = '')
44
    {
45
        $this->setItem($name, $value, $where);
46 3
    }
47
48
    /**
49
     * Adds an item. Does not touch if already exists.
50
     *
51
     * @param int|string   $name  item name.
52
     * @param array        $value item value.
53
     * @param string|array $where where to put, see [[setItem()]]
54
     *
55
     * @return $this for chaining
56
     *
57
     * @see setItem()
58
     */
59 3
    public function add($name, $value = null, $where = '')
60
    {
61
        if (!$this->has($name)) {
62
            $this->set($name, $value, $where);
63
        }
64
65 3
        return $this;
66 1
    }
67
    /**
68
     * Check collection has the item.
69
     *
70
     * @param string $name item name.
71
     *
72
     * @return bool whether item exist.
73
     */
74
    public function has($name)
75
    {
76
        return $this->hasItem($name);
77
    }
78
79
    /**
80
     * Delete an item.
81
     * You can't create function `unset`.
82
     *
83
     * @param $name
84
     */
85 1
    public function delete($name)
86
    {
87
        $this->unsetItem($name);
88 1
    }
89
90
    /**
91
     * This method is overridden to support accessing items like properties.
92
     *
93
     * @param string $name item or property name
94
     *
95
     * @return mixed item of found or the named property value
96
     */
97
    public function __get($name)
98
    {
99
        return $this->getItem($name);
100
    }
101
102
    /**
103
     * This method is overridden to support accessing items like properties.
104
     *
105
     * @param string $name  item or property name
106
     * @param string $value value to be set
107
     *
108
     * @return mixed item of found or the named property value
109
     */
110 1
    public function __set($name, $value)
111
    {
112
        $this->set($name, $value);
113 1
    }
114
115
    /**
116
     * Checks if a property value is null.
117
     * This method overrides the parent implementation by checking if the named item is loaded.
118
     *
119
     * @param string $name the property name or the event name
120
     *
121
     * @return bool whether the property value is null
122
     */
123
    public function __isset($name)
124
    {
125
        return ($name && parent::__isset($name)) || $this->issetItem($name);
126
    }
127
128
    /**
129
     * Checks if a property value is null.
130
     * This method overrides the parent implementation by checking if the named item is loaded.
131
     *
132
     * @param string $name the property name or the event name
133
     *
134
     * @return bool whether the property value is null
135
     */
136
    public function __unset($name)
137
    {
138
        $this->unsetItem($name);
139
    }
140
}
141