1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Shared |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Shared\Reference; |
16
|
|
|
|
17
|
|
|
use Phossa2\Shared\Message\Message; |
18
|
|
|
use Phossa2\Shared\Exception\NotFoundException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* DelegatorTrait |
22
|
|
|
* |
23
|
|
|
* Implementation of DelegatorInterface |
24
|
|
|
* |
25
|
|
|
* @package Phossa2\Shared |
26
|
|
|
* @author Hong Zhang <[email protected]> |
27
|
|
|
* @see DelegatorInterface |
28
|
|
|
* @version 2.0.8 |
29
|
|
|
* @since 2.0.8 added |
30
|
|
|
*/ |
31
|
|
|
trait DelegatorTrait |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* lookup pool |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
* @access private |
38
|
|
|
*/ |
39
|
|
|
private $lookup_pool = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* lookup cache key |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
* @access private |
46
|
|
|
*/ |
47
|
|
|
private $cache_key; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* lookup cache object |
51
|
|
|
* |
52
|
|
|
* @var object |
53
|
|
|
* @access private |
54
|
|
|
*/ |
55
|
|
|
private $cache_obj; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
public function addToLookup($object) |
61
|
|
|
{ |
62
|
|
|
// remove if exists already |
63
|
|
|
$this->removeFromLookup($object); |
64
|
|
|
|
65
|
|
|
// append to the pool end |
66
|
|
|
$this->lookup_pool[] = $object; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritDoc} |
73
|
|
|
*/ |
74
|
|
|
public function hasInLookup(/*# string */ $key)/*# : bool */ |
75
|
|
|
{ |
76
|
|
|
foreach ($this->lookup_pool as $object) { |
77
|
|
|
if ($this->hasInObject($object, $key)) { |
78
|
|
|
$this->cache_key = $key; |
79
|
|
|
$this->cache_obj = $object; |
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritDoc} |
88
|
|
|
*/ |
89
|
|
|
public function getFromLookup(/*# string */ $key) |
90
|
|
|
{ |
91
|
|
|
// check cache first |
92
|
|
|
if ($key === $this->cache_key) { |
93
|
|
|
return $this->getFromObject($this->cache_obj, $key); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// try lookup |
97
|
|
|
if ($this->hasInLookup($key)) { |
98
|
|
|
return $this->getFromObject($this->cache_obj, $key); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// not found |
102
|
|
|
throw new NotFoundException( |
103
|
|
|
Message::get(Message::MSG_REF_UNKNOWN, $key), |
104
|
|
|
Message::MSG_REF_UNKNOWN |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Remove one object from the pool |
110
|
|
|
* |
111
|
|
|
* @param object $object |
112
|
|
|
* @return $this |
113
|
|
|
* @access protected |
114
|
|
|
*/ |
115
|
|
|
protected function removeFromLookup($object) |
116
|
|
|
{ |
117
|
|
|
foreach ($this->lookup_pool as $idx => $obj) { |
118
|
|
|
if ($object === $obj) { |
119
|
|
|
unset($this->lookup_pool[$idx]); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Try has in object |
127
|
|
|
* |
128
|
|
|
* @param object $object |
129
|
|
|
* @param name $key |
130
|
|
|
* @return bool |
131
|
|
|
* @access protected |
132
|
|
|
*/ |
133
|
|
|
abstract protected function hasInObject( |
134
|
|
|
$object, |
135
|
|
|
/*# string */ $key |
136
|
|
|
)/*# : bool */; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Try get from object |
140
|
|
|
* |
141
|
|
|
* @param object $object |
142
|
|
|
* @param name $key |
143
|
|
|
* @return mixed |
144
|
|
|
* @access protected |
145
|
|
|
*/ |
146
|
|
|
abstract protected function getFromObject( |
147
|
|
|
$object, |
148
|
|
|
/*# string */ $key |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|