|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @copyright 2013 - 2017 Cross Solution <http://cross-solution.de> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** */ |
|
11
|
|
|
namespace Jobs\Repository; |
|
12
|
|
|
|
|
13
|
|
|
use Jobs\Entity\Category; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* ${CARET} |
|
17
|
|
|
* |
|
18
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
19
|
|
|
* @todo write test |
|
20
|
|
|
*/ |
|
21
|
|
|
class DefaultCategoriesBuilder |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The module config path. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $moduleConfigPath; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The global config paths. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string[] |
|
35
|
|
|
*/ |
|
36
|
|
|
private $globalConfigPaths; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Prototype of a Category |
|
40
|
|
|
* |
|
41
|
|
|
* @var Category |
|
42
|
|
|
*/ |
|
43
|
|
|
private $categoryPrototype; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct($moduleConfigPath, array $globalConfigPaths, Category $categoryPrototype) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->moduleConfigPath = $moduleConfigPath; |
|
48
|
|
|
$this->globalConfigPaths = $globalConfigPaths; |
|
49
|
|
|
$this->categoryPrototype = $categoryPrototype; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function build($type) |
|
53
|
|
|
{ |
|
54
|
|
|
$categories = $this->loadCategoriesConfig($type); |
|
55
|
|
|
|
|
56
|
|
|
if (!is_array($categories)) { |
|
57
|
|
|
$categories = ['name' => $type, 'value' => $type]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$category = $this->buildCategory($categories); |
|
61
|
|
|
|
|
62
|
|
|
return $category; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function loadCategoriesConfig($type) |
|
66
|
|
|
{ |
|
67
|
|
|
$file = "jobs.categories.$type.php"; |
|
68
|
|
|
|
|
69
|
|
|
foreach ($this->globalConfigPaths as $path) { |
|
70
|
|
|
if (file_exists($path . $file)) { |
|
71
|
|
|
return include $path . $file; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (file_exists($this->moduleConfigPath . $file)) { |
|
76
|
|
|
return include $this->moduleConfigPath . $file; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function buildCategory($spec) |
|
83
|
|
|
{ |
|
84
|
|
|
if (is_string($spec)) { |
|
85
|
|
|
$spec = ['name' => $spec]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$category = clone $this->categoryPrototype; |
|
89
|
|
|
$category |
|
90
|
|
|
->setName(isset($spec['name']) ? $spec['name'] : '') |
|
91
|
|
|
->setValue(isset($spec['value']) ? $spec['value'] : '') |
|
92
|
|
|
; |
|
93
|
|
|
|
|
94
|
|
|
if (isset($spec['children']) && is_array($spec['children'])) { |
|
95
|
|
|
foreach ($spec['children'] as $childSpec) { |
|
96
|
|
|
$child = $this->buildCategory($childSpec); |
|
97
|
|
|
$category->addChild($child); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $category; |
|
102
|
|
|
} |
|
103
|
|
|
} |