|
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\Delegator; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Shared\Message\Message; |
|
18
|
|
|
use Phossa2\Shared\Exception\NotFoundException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* DelegatorAwareTrait |
|
22
|
|
|
* |
|
23
|
|
|
* Implmentation of DelegatorAwareInterface |
|
24
|
|
|
* |
|
25
|
|
|
* @package Phossa2\Shared |
|
26
|
|
|
* @author Hong Zhang <[email protected]> |
|
27
|
|
|
* @see DelegatorAwareInterface |
|
28
|
|
|
* @version 2.0.15 |
|
29
|
|
|
* @since 2.0.8 added |
|
30
|
|
|
* @since 2.0.15 moved to new namespace |
|
31
|
|
|
* @since 2.0.17 updated getDelegator() |
|
32
|
|
|
*/ |
|
33
|
|
|
trait DelegatorAwareTrait |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* the delegator |
|
37
|
|
|
* |
|
38
|
|
|
* @var DelegatorInterface |
|
39
|
|
|
* @access protected |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $delegator; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritDoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function setDelegator(DelegatorInterface $delegator = null) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->delegator = $delegator; |
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritDoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function hasDelegator()/*# : bool */ |
|
56
|
|
|
{ |
|
57
|
|
|
return null !== $this->delegator; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritDoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getDelegator( |
|
64
|
|
|
/*# bool */ $recursive = false |
|
65
|
|
|
)/*# : DelegatorInterface */ { |
|
66
|
|
|
if ($this->hasDelegator()) { |
|
67
|
|
|
$dele = $this->delegator; |
|
68
|
|
|
if ($this->isRecursiveDelegator($recursive, $dele)) { |
|
69
|
|
|
return $dele->getDelegator($recursive); |
|
70
|
|
|
} |
|
71
|
|
|
return $dele; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
throw new NotFoundException( |
|
75
|
|
|
Message::get(Message::MSG_DELEGATOR_UNKNOWN, get_called_class()), |
|
76
|
|
|
Message::MSG_DELEGATOR_UNKNOWN |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Is delegatorAware recursively |
|
82
|
|
|
* |
|
83
|
|
|
* @param bool $recursive |
|
84
|
|
|
* @param object $object |
|
85
|
|
|
* @access protected |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function isRecursiveDelegator( |
|
88
|
|
|
/*# bool */ $recursive, $object |
|
89
|
|
|
)/*#: bool */ { |
|
90
|
|
|
return $recursive && |
|
91
|
|
|
$object instanceof DelegatorAwareInterface && |
|
92
|
|
|
$object->hasDelegator(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|