Passed
Pull Request — master (#316)
by Sergei
21:20 queued 18:29
created

ArrayAccessTrait::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ActiveRecord\Trait;
6
7
use function property_exists;
8
9
/**
10
 * Trait to implement {@see ArrayAccess} interface for ActiveRecord.
11
 */
12
trait ArrayAccessTrait
13
{
14
    /**
15
     * Returns whether there is an element at the specified offset.
16
     *
17
     * This method is required by the SPL interface {@see ArrayAccess}.
18
     *
19
     * It is implicitly called when you use something like `isset($model[$offset])`.
20
     *
21
     * @return bool whether or not an offset exists.
22
     */
23
    public function offsetExists(mixed $offset): bool
24
    {
25
        return isset($this->$offset);
26
    }
27
28
    public function offsetGet(mixed $offset): mixed
29
    {
30
        return $this->$offset;
31
    }
32
33
    /**
34
     * Sets the element at the specified offset.
35
     *
36
     * This method is required by the SPL interface {@see ArrayAccess}.
37
     *
38
     * It is implicitly called when you use something like `$model[$offset] = $item;`.
39
     */
40
    public function offsetSet(mixed $offset, mixed $value): void
41
    {
42
        $this->$offset = $value;
43
    }
44
45
    /**
46
     * Sets the element value at the specified offset to null.
47
     *
48
     * This method is required by the SPL interface {@see ArrayAccess}.
49
     *
50
     * It is implicitly called when you use something like `unset($model[$offset])`.
51
     */
52
    public function offsetUnset(mixed $offset): void
53
    {
54
        if (is_string($offset) && property_exists($this, $offset)) {
55
            $this->$offset = null;
56
        } else {
57
            unset($this->$offset);
58
        }
59
    }
60
}
61