|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Leonidas\Library\Core\Models\Taxonomy; |
|
4
|
|
|
|
|
5
|
|
|
use Leonidas\Contracts\Options\TaxonomyOptionHandlerInterface; |
|
6
|
|
|
use Leonidas\Library\Core\Models\AbstractWpConfigModelFactory; |
|
7
|
|
|
use Leonidas\Library\Core\Models\Taxonomy\Taxonomy; |
|
8
|
|
|
use WP_Taxonomy; |
|
9
|
|
|
|
|
10
|
|
|
class Factory extends AbstractWpConfigModelFactory |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
public function create(array $taxonomies): array |
|
16
|
|
|
{ |
|
17
|
|
|
return parent::create($taxonomies); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
public function build(string $name, array $args): object |
|
24
|
|
|
{ |
|
25
|
|
|
$labels = $args['labels'] ?? []; |
|
26
|
|
|
$objectTypes = $args['object_types']; |
|
27
|
|
|
$options = $args['options'] ?? []; |
|
28
|
|
|
$rewrite = $args['rewrite'] ?? []; |
|
29
|
|
|
|
|
30
|
|
|
unset($args['labels'], $args['rewrite'], $args['object_types'], $args['options']); |
|
31
|
|
|
|
|
32
|
|
|
$taxonomy = (new Taxonomy($name, $objectTypes)) |
|
33
|
|
|
->setArgs($args) |
|
34
|
|
|
->setLabels($labels) |
|
35
|
|
|
->setRewrite($rewrite) |
|
36
|
|
|
->register() |
|
37
|
|
|
->getRegisteredTaxonomy(); |
|
38
|
|
|
|
|
39
|
|
|
if (isset($this->optionHandlers)) { |
|
40
|
|
|
$this->processOptions($options, $taxonomy); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $taxonomy; |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function processOptions($options, WP_Taxonomy $taxonomy) |
|
50
|
|
|
{ |
|
51
|
|
|
foreach ($options as $option => $args) { |
|
52
|
|
|
$handler = $this->optionHandlers[$option] ?? null; |
|
53
|
|
|
|
|
54
|
|
|
if (!$handler) { |
|
55
|
|
|
throw new \Exception("There is no registered handler for the {$option} option provided"); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($handler && in_array(TaxonomyOptionHandlerInterface::class, class_implements($handler))) { |
|
59
|
|
|
$handler::handle($taxonomy, $args); |
|
60
|
|
|
} else { |
|
61
|
|
|
throw new \Exception("{$handler} is not a valid option handler"); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|