1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the pinepain/js-sandbox PHP library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Licensed under the MIT license: http://opensource.org/licenses/MIT |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the |
11
|
|
|
* LICENSE file that was distributed with this source or visit |
12
|
|
|
* http://opensource.org/licenses/MIT |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
namespace Pinepain\JsSandbox\Specs\Builder; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use Pinepain\JsSandbox\Specs\BindingSpecInterface; |
20
|
|
|
use Pinepain\JsSandbox\Specs\Builder\Exceptions\BindingSpecBuilderException; |
21
|
|
|
use Pinepain\JsSandbox\Specs\Builder\Exceptions\FunctionSpecBuilderException; |
22
|
|
|
use Pinepain\JsSandbox\Specs\Builder\Exceptions\ObjectSpecBuilderException; |
23
|
|
|
use Pinepain\JsSandbox\Specs\Builder\Exceptions\PropertySpecBuilderException; |
24
|
|
|
use Pinepain\JsSandbox\Specs\FunctionSpecInterface; |
25
|
|
|
use Pinepain\JsSandbox\Specs\PropertySpecInterface; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class ObjectSpecBuilder implements ObjectSpecBuilderInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var PropertySpecBuilderInterface |
32
|
|
|
*/ |
33
|
|
|
private $property; |
34
|
|
|
/** |
35
|
|
|
* @var FunctionSpecBuilderInterface |
36
|
|
|
*/ |
37
|
|
|
private $function; |
38
|
|
|
/** |
39
|
|
|
* @var BindingSpecBuilderInterface |
40
|
|
|
*/ |
41
|
|
|
private $binding; |
42
|
|
|
|
43
|
5 |
|
public function __construct(PropertySpecBuilderInterface $property, FunctionSpecBuilderInterface $function, BindingSpecBuilderInterface $binding) |
44
|
|
|
{ |
45
|
5 |
|
$this->property = $property; |
46
|
5 |
|
$this->function = $function; |
47
|
5 |
|
$this->binding = $binding; |
48
|
5 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
5 |
|
public function build(array $definitions): array |
54
|
|
|
{ |
55
|
5 |
|
$out = []; |
56
|
|
|
|
57
|
5 |
|
foreach ($definitions as $name => $definition) { |
58
|
5 |
|
if (!is_string($definition)) { |
59
|
1 |
|
$out[$name] = $definition; |
60
|
1 |
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
$out[$name] = $this->buildConcreteDefinition($name, $definition); |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
return $out; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $name |
71
|
|
|
* @param string $definition |
72
|
|
|
* |
73
|
|
|
* @return BindingSpecInterface|FunctionSpecInterface|PropertySpecInterface |
74
|
|
|
* @throws ObjectSpecBuilderException |
75
|
|
|
*/ |
76
|
4 |
|
protected function buildConcreteDefinition(string $name, string $definition) |
77
|
|
|
{ |
78
|
|
|
try { |
79
|
4 |
|
return $this->property->build($definition); |
80
|
3 |
|
} catch (PropertySpecBuilderException $e) { |
81
|
|
|
// |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
try { |
85
|
3 |
|
return $this->function->build($definition); |
86
|
2 |
|
} catch (FunctionSpecBuilderException $e) { |
87
|
|
|
// |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
try { |
91
|
2 |
|
return $this->binding->build($definition); |
92
|
1 |
|
} catch (BindingSpecBuilderException $e) { |
93
|
|
|
// |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
throw new ObjectSpecBuilderException("Unable to build spec for '{$name}' from definition '{$definition}'"); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|