1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of riesenia/pohoda package. |
5
|
|
|
* |
6
|
|
|
* Licensed under the MIT License |
7
|
|
|
* (c) RIESENIA.com |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Riesenia\Pohoda; |
13
|
|
|
|
14
|
|
|
use Riesenia\Pohoda\Common\OptionsResolver; |
15
|
|
|
|
16
|
|
|
class Category extends AbstractAgenda |
17
|
|
|
{ |
18
|
|
|
/** @var string[] */ |
19
|
|
|
protected array $elements = ['name', 'description', 'sequence', 'displayed', 'picture', 'note']; |
20
|
|
|
|
21
|
4 |
|
public function getImportRoot(): string |
22
|
|
|
{ |
23
|
4 |
|
return 'ctg:category'; |
24
|
|
|
} |
25
|
|
|
|
26
|
3 |
|
public function canImportRecursive(): bool |
27
|
|
|
{ |
28
|
3 |
|
return true; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Add subcategory. |
33
|
|
|
* |
34
|
|
|
* @param self $category |
35
|
|
|
* |
36
|
|
|
* @return $this |
37
|
|
|
*/ |
38
|
1 |
|
public function addSubcategory(self $category): self |
39
|
|
|
{ |
40
|
1 |
|
if (!isset($this->data['subCategories']) |
41
|
1 |
|
|| !( |
42
|
1 |
|
is_array($this->data['subCategories']) |
43
|
1 |
|
|| (is_object($this->data['subCategories']) && is_a($this->data['subCategories'], \ArrayAccess::class)) |
44
|
1 |
|
) |
45
|
|
|
) { |
46
|
1 |
|
$this->data['subCategories'] = []; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
$this->data['subCategories'][] = $category; |
50
|
|
|
|
51
|
1 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
2 |
|
public function getXML(): \SimpleXMLElement |
58
|
|
|
{ |
59
|
2 |
|
$xml = $this->createXML()->addChild('ctg:categoryDetail', '', $this->namespace('ctg')); |
60
|
2 |
|
$xml->addAttribute('version', '2.0'); |
61
|
|
|
|
62
|
2 |
|
$this->categoryXML($xml); |
63
|
|
|
|
64
|
2 |
|
return $xml; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Attach category to XML element. |
69
|
|
|
* |
70
|
|
|
* @param \SimpleXMLElement $xml |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
2 |
|
public function categoryXML(\SimpleXMLElement $xml) |
75
|
|
|
{ |
76
|
2 |
|
$category = $xml->addChild('ctg:category', '', $this->namespace('ctg')); |
77
|
|
|
|
78
|
2 |
|
$this->addElements($category, $this->elements, 'ctg'); |
79
|
|
|
|
80
|
2 |
|
if (isset($this->data['subCategories']) && is_iterable($this->data['subCategories'])) { |
81
|
1 |
|
$subCategories = $category->addChild('ctg:subCategories', '', $this->namespace('ctg')); |
82
|
|
|
|
83
|
1 |
|
foreach ($this->data['subCategories'] as $subCategory) { |
84
|
|
|
/** @var self $subCategory */ |
85
|
1 |
|
$subCategory->categoryXML($subCategories); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
1 |
|
protected function configureOptions(OptionsResolver $resolver): void |
94
|
|
|
{ |
95
|
|
|
// available options |
96
|
1 |
|
$resolver->setDefined($this->elements); |
97
|
|
|
|
98
|
|
|
// validate / format options |
99
|
1 |
|
$resolver->setRequired('name'); |
100
|
1 |
|
$resolver->setNormalizer('name', $this->normalizerFactory->getClosure('string48')); |
101
|
1 |
|
$resolver->setNormalizer('sequence', $this->normalizerFactory->getClosure('int')); |
102
|
1 |
|
$resolver->setNormalizer('displayed', $this->normalizerFactory->getClosure('bool')); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|