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\BindingSpec; |
20
|
|
|
use Pinepain\JsSandbox\Specs\BindingSpecInterface; |
21
|
|
|
use Pinepain\JsSandbox\Specs\Builder\Exceptions\BindingSpecBuilderException; |
22
|
|
|
use Pinepain\JsSandbox\Specs\Builder\Exceptions\FunctionSpecBuilderException; |
23
|
|
|
use Pinepain\JsSandbox\Specs\Builder\Exceptions\PropertySpecBuilderException; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class BindingSpecBuilder implements BindingSpecBuilderInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var PropertySpecBuilderInterface |
30
|
|
|
*/ |
31
|
|
|
private $property_spec_builder; |
32
|
|
|
/** |
33
|
|
|
* @var FunctionSpecBuilderInterface |
34
|
|
|
*/ |
35
|
|
|
private $function_spec_builder; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param PropertySpecBuilderInterface $property_spec_builder |
39
|
|
|
* @param FunctionSpecBuilderInterface $function_spec_builder |
40
|
|
|
*/ |
41
|
5 |
|
public function __construct(PropertySpecBuilderInterface $property_spec_builder, FunctionSpecBuilderInterface $function_spec_builder) |
42
|
|
|
{ |
43
|
5 |
|
$this->property_spec_builder = $property_spec_builder; |
44
|
5 |
|
$this->function_spec_builder = $function_spec_builder; |
45
|
5 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
5 |
|
public function build(string $definition): BindingSpecInterface |
51
|
|
|
{ |
52
|
5 |
|
$definition = trim($definition); |
53
|
|
|
|
54
|
5 |
|
if (!$definition) { |
55
|
1 |
|
throw new BindingSpecBuilderException('Definition must be non-empty string'); |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
if (preg_match('/^(?<name>\w+)\s*=>\s*(?<variation>.+)$/', $definition, $matches)) { |
59
|
|
|
/** @var PropertySpecBuilderInterface|FunctionSpecBuilderInterface $builder */ |
60
|
3 |
|
foreach ([$this->property_spec_builder, $this->function_spec_builder] as $builder) { |
61
|
|
|
try { |
62
|
3 |
|
$spec = $builder->build($matches['variation']); |
63
|
|
|
|
64
|
2 |
|
return new BindingSpec($matches['name'], $spec); |
65
|
2 |
|
} catch (FunctionSpecBuilderException | PropertySpecBuilderException $e) { |
66
|
|
|
// continue |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
throw new BindingSpecBuilderException("Unable to extract spec from definition: '{$definition}'"); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
throw new BindingSpecBuilderException("Unable to parse definition: '{$definition}'"); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|