Completed
Push — lazy_content_type ( 8d23f7...0f043a )
by André
11:01
created

ProxyTrait::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Repository\Values;
10
11
use Generator;
12
13
/**
14
 * Trait for proxies, covers all relevant magic methods and exposes private method to load object.
15
 *
16
 * @internal Meant for internal use by Repository, type hint against API object instead.
17
 */
18
trait ProxyTrait
19
{
20
    /**
21
     * @var object
22
     */
23
    private $object;
24
25
    /**
26
     * @var Generator|null
27
     */
28
    private $generator;
29
30
    public function __construct(Generator $generator)
31
    {
32
        $this->generator = $generator;
33
    }
34
35
    public function __call($name, $arguments)
36
    {
37
        if (isset($this->generator)) {
38
            $this->loadObject();
39
        }
40
41
        return $this->object->$name(...$arguments);
42
    }
43
44
    public function __invoke(...$args)
45
    {
46
        if (isset($this->generator)) {
47
            $this->loadObject();
48
        }
49
50
        return ($this->object)(...$args);
51
    }
52
53
    public function __get($name)
54
    {
55
        if (isset($this->generator)) {
56
            $this->loadObject();
57
        }
58
59
        return $this->object->$name;
60
    }
61
62
    public function __isset($name)
63
    {
64
        if (isset($this->generator)) {
65
            $this->loadObject();
66
        }
67
68
        return isset($this->object->$name);
69
    }
70
71
    public function __unset($name)
72
    {
73
        if (isset($this->generator)) {
74
            $this->loadObject();
75
        }
76
77
        unset($this->object->$name);
78
    }
79
80
    public function __set($name, $value)
81
    {
82
        if (isset($this->generator)) {
83
            $this->loadObject();
84
        }
85
86
        $this->object->$name = $value;
87
    }
88
89
    public function __toString()
90
    {
91
        if (isset($this->generator)) {
92
            $this->loadObject();
93
        }
94
95
        return (string)$this->object;
96
    }
97
98
    public function __sleep()
99
    {
100
        if (isset($this->generator)) {
101
            $this->loadObject();
102
        }
103
104
        return array('object');
105
    }
106
107
    public function __debugInfo()
108
    {
109
        if (isset($this->generator)) {
110
            $this->loadObject();
111
        }
112
113
        return [
114
            'object' => $this->object,
115
        ];
116
    }
117
118
    /**
119
     * Loads the generator to object value and unset's generator.
120
     */
121
    private function loadObject()
122
    {
123
        $this->object = $this->generator->current();
124
        unset($this->generator);
125
    }
126
}
127