|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the pinepain/php-v8-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
|
|
|
public function __construct(PropertySpecBuilderInterface $property, FunctionSpecBuilderInterface $function, BindingSpecBuilderInterface $binding) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->property = $property; |
|
46
|
|
|
$this->function = $function; |
|
47
|
|
|
$this->binding = $binding; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string[]|FunctionSpecInterface[]|PropertySpecInterface[]|BindingSpecInterface[] $definitions |
|
52
|
|
|
* |
|
53
|
|
|
* @return FunctionSpecInterface[]|PropertySpecInterface[]|BindingSpecInterface[] |
|
54
|
|
|
* @throws ObjectSpecBuilderException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function build(array $definitions): array |
|
57
|
|
|
{ |
|
58
|
|
|
foreach ($definitions as $name => $definition) { |
|
59
|
|
|
if (!is_string($definition)) { |
|
60
|
|
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$definitions[$name] = $this->buildConcreteDefinition($name, $definition); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $definitions; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $name |
|
71
|
|
|
* @param string $definition |
|
72
|
|
|
* |
|
73
|
|
|
* @return BindingSpecInterface|FunctionSpecInterface|PropertySpecInterface |
|
74
|
|
|
* @throws ObjectSpecBuilderException |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function buildConcreteDefinition(string $name, string $definition) |
|
77
|
|
|
{ |
|
78
|
|
|
try { |
|
79
|
|
|
return $this->property->build($definition); |
|
80
|
|
|
} catch (PropertySpecBuilderException $e) { |
|
81
|
|
|
// |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
try { |
|
85
|
|
|
return $this->function->build($definition); |
|
86
|
|
|
} catch (FunctionSpecBuilderException $e) { |
|
87
|
|
|
// |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
try { |
|
91
|
|
|
return $this->binding->build($definition); |
|
92
|
|
|
} catch (BindingSpecBuilderException $e) { |
|
93
|
|
|
// |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
throw new ObjectSpecBuilderException("Unable to build spec for '{$name}' from definition '{$definition}'"); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|