1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\PhpApi\Hydrator package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\PhpApi\Hydrator; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema; |
12
|
|
|
use KleijnWeb\PhpApi\Hydrator\Exception\UnsupportedException; |
13
|
|
|
use KleijnWeb\PhpApi\Hydrator\Processors\Factory\AnyFactory; |
14
|
|
|
use KleijnWeb\PhpApi\Hydrator\Processors\Factory\ArrayFactory; |
15
|
|
|
use KleijnWeb\PhpApi\Hydrator\Processors\Factory\ComplexTypeFactory; |
16
|
|
|
use KleijnWeb\PhpApi\Hydrator\Processors\Factory\DateTimeFactory; |
17
|
|
|
use KleijnWeb\PhpApi\Hydrator\Processors\Factory\Factory; |
18
|
|
|
use KleijnWeb\PhpApi\Hydrator\Processors\Factory\FactoryQueue; |
19
|
|
|
use KleijnWeb\PhpApi\Hydrator\Processors\Factory\LooseSimpleObjectFactory; |
20
|
|
|
use KleijnWeb\PhpApi\Hydrator\Processors\Factory\ScalarFactory; |
21
|
|
|
use KleijnWeb\PhpApi\Hydrator\Processors\Factory\StrictSimpleObjectFactory; |
22
|
|
|
use KleijnWeb\PhpApi\Hydrator\Processors\Processor; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author John Kleijn <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ProcessorBuilder |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var FactoryQueue |
31
|
|
|
*/ |
32
|
|
|
protected $factoryQueue; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* ProcessorBuilder constructor. |
36
|
|
|
* @param ClassNameResolver $classNameResolver |
37
|
|
|
* @param DateTimeSerializer|null $dateTimeSerializer |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ClassNameResolver $classNameResolver, DateTimeSerializer $dateTimeSerializer = null) |
40
|
|
|
{ |
41
|
|
|
$dateTimeSerializer = $dateTimeSerializer ?: new DateTimeSerializer(); |
42
|
|
|
|
43
|
|
|
$this->factoryQueue = new FactoryQueue( |
44
|
|
|
new ComplexTypeFactory($classNameResolver), |
45
|
|
|
new LooseSimpleObjectFactory(), |
46
|
|
|
new StrictSimpleObjectFactory(), |
47
|
|
|
new ArrayFactory(), |
48
|
|
|
new DateTimeFactory($dateTimeSerializer), |
49
|
|
|
new ScalarFactory(), |
50
|
|
|
new AnyFactory($dateTimeSerializer) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Factory $factory |
56
|
|
|
* @return ProcessorBuilder |
57
|
|
|
*/ |
58
|
|
|
public function add(Factory $factory): ProcessorBuilder |
59
|
|
|
{ |
60
|
|
|
$this->factoryQueue->add($factory); |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Schema $schema |
67
|
|
|
* @return Processor |
68
|
|
|
*/ |
69
|
|
|
public function build(Schema $schema): Processor |
70
|
|
|
{ |
71
|
|
|
/** @var Factory $factory */ |
72
|
|
|
foreach (clone $this->factoryQueue as $factory) { |
73
|
|
|
if ($processor = $factory->create($schema, $this)) { |
74
|
|
|
return $processor; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
throw new UnsupportedException("Unsupported schema type " . get_class($schema)); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|