1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Samrap\Acf; |
4
|
|
|
|
5
|
|
|
use Samrap\Acf\Behaviors\FieldBehavior; |
6
|
|
|
use Samrap\Acf\Behaviors\SubFieldBehavior; |
7
|
|
|
use Samrap\Acf\Fluent\Builder; |
8
|
|
|
use Samrap\Acf\Fluent\Runner; |
9
|
|
|
|
10
|
|
|
class Acf |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The class' single instance. |
14
|
|
|
* |
15
|
|
|
* @var \Samrap\Acf\Acf |
16
|
|
|
*/ |
17
|
|
|
private static $instance; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The behavior instances. |
21
|
|
|
* |
22
|
|
|
* @var \Samrap\Acf\Behaviors\BehaviorInterface[] |
23
|
|
|
*/ |
24
|
|
|
private $behaviors = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The available macros defined. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $macros = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Private constructor to prevent instantiation. |
35
|
|
|
* |
36
|
|
|
* @codeCoverageIgnore The class cannot be instantiated. |
37
|
|
|
*/ |
38
|
|
|
private function __construct() |
39
|
|
|
{ |
40
|
|
|
// |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the single instance of this object. |
45
|
|
|
* |
46
|
|
|
* @return \Samrap\Acf\Acf |
47
|
|
|
*/ |
48
|
|
|
private static function getInstance() |
49
|
|
|
{ |
50
|
|
|
if (! self::$instance) { |
51
|
|
|
self::$instance = new self(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return self::$instance; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return a new builder instance for a field call. |
59
|
|
|
* |
60
|
|
|
* @param string $name |
61
|
|
|
* @param int $id |
62
|
|
|
* @return \Samrap\Acf\Fluent\Builder |
63
|
|
|
*/ |
64
|
|
|
public static function field($name, $id = null) |
65
|
|
|
{ |
66
|
|
|
return self::getInstance() |
67
|
|
|
->getBuilder(FieldBehavior::class) |
68
|
|
|
->field($name) |
69
|
|
|
->id($id); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return a new builder instance for a subfield call. |
74
|
|
|
* |
75
|
|
|
* @param string $name |
76
|
|
|
* @return \Samrap\Acf\Fluent\Builder |
77
|
|
|
*/ |
78
|
|
|
public static function subField($name) |
79
|
|
|
{ |
80
|
|
|
return self::getInstance() |
81
|
|
|
->getBuilder(SubFieldBehavior::class) |
82
|
|
|
->field($name); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return a new builder instance for an option field call. |
87
|
|
|
* |
88
|
|
|
* @param string $name |
89
|
|
|
* @return \Samrap\Acf\Fluent\Builder |
90
|
|
|
*/ |
91
|
|
|
public static function option($name) |
92
|
|
|
{ |
93
|
|
|
return self::getInstance() |
94
|
|
|
->getBuilder(FieldBehavior::class) |
95
|
|
|
->field($name) |
96
|
|
|
->id('option'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Add a macro to the ACF builder. |
101
|
|
|
* |
102
|
|
|
* @param string $method |
103
|
|
|
* @param callable $operation |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
public static function macro($method, $operation) |
107
|
|
|
{ |
108
|
|
|
self::getInstance()->macros[$method] = $operation; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Return a builder instance with the given behavior. |
113
|
|
|
* |
114
|
|
|
* @param string $behavior |
115
|
|
|
* @return \Samrap\Acf\Fluent\Builder |
116
|
|
|
*/ |
117
|
|
|
private function getBuilder($behavior) |
118
|
|
|
{ |
119
|
|
|
// Create a new behavior of the given type if one does not yet exist. |
120
|
|
|
if (! isset($this->behaviors[$behavior])) { |
121
|
|
|
$this->behaviors[$behavior] = new $behavior(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return new Builder(new Runner($this->behaviors[$behavior]), $this->macros); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|