1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StoutLogic\AcfBuilder; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Builds a configuration. |
7
|
|
|
* Can have parent contexts to delegate missing methods to. |
8
|
|
|
*/ |
9
|
|
|
abstract class ParentDelegationBuilder implements Builder |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The parent Builder, if this is a child Builder |
13
|
|
|
* @var Builder |
14
|
|
|
*/ |
15
|
|
|
private $parentContext; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Builds the configuration |
19
|
|
|
* @return array configuration |
20
|
|
|
*/ |
21
|
|
|
abstract public function build(); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Builder $builder |
25
|
|
|
*/ |
26
|
|
|
public function setParentContext(Builder $builder) |
27
|
|
|
{ |
28
|
|
|
$this->parentContext = $builder; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return Builder |
33
|
|
|
*/ |
34
|
|
|
public function getParentContext() |
35
|
|
|
{ |
36
|
|
|
return $this->parentContext; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns the root context |
41
|
|
|
* @return Builder |
42
|
|
|
*/ |
43
|
|
|
public function getRootContext() |
44
|
|
|
{ |
45
|
|
|
if ($parentContext = $this->getParentContext()) { |
46
|
|
|
if ($parentContext instanceof ParentDelegationBuilder) { |
47
|
|
|
return $parentContext->getRootContext(); |
48
|
|
|
} |
49
|
|
|
return $parentContext; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* If a method is missing, check to see if it exist on the $parentContext |
57
|
|
|
* and delegate the call to it. |
58
|
|
|
* @param string $method |
59
|
|
|
* @param array $args |
60
|
|
|
* @throws \Exception when a method is not found on the $parentContext |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public function __call($method, $args) |
64
|
|
|
{ |
65
|
|
|
if ($this->parentContext) { |
66
|
|
|
return call_user_func_array([$this->parentContext, $method], $args); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
throw new \Exception('No such function: '.$method); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|