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\Processors\Factory; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Schema\ObjectSchema; |
12
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema; |
13
|
|
|
use KleijnWeb\PhpApi\Hydrator\ProcessorBuilder; |
14
|
|
|
use KleijnWeb\PhpApi\Hydrator\Processors\Object\ObjectProcessor; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author John Kleijn <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
abstract class ObjectFactory implements Factory |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param Schema $schema |
23
|
|
|
* @param ProcessorBuilder $builder |
24
|
|
|
* @return ObjectProcessor|null |
25
|
|
|
*/ |
26
|
|
|
public function create(Schema $schema, ProcessorBuilder $builder) |
27
|
|
|
{ |
28
|
|
|
if (!$this->supports($schema)) { |
29
|
|
|
return null; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** @var ObjectSchema $objectSchema */ |
33
|
|
|
$objectSchema = $schema; |
34
|
|
|
$processor = $this->instantiate($objectSchema, $builder); |
35
|
|
|
|
36
|
|
|
foreach ($objectSchema->getPropertySchemas() as $propertyName => $propertySchema) { |
|
|
|
|
37
|
|
|
$processor->setPropertyProcessor($propertyName, $builder->build($propertySchema)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $processor; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Schema $schema |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public function supports(Schema $schema) |
48
|
|
|
{ |
49
|
|
|
return $schema instanceof ObjectSchema; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param ObjectSchema $schema |
54
|
|
|
* @param ProcessorBuilder $builder |
55
|
|
|
* @return ObjectProcessor |
56
|
|
|
*/ |
57
|
|
|
abstract protected function instantiate(ObjectSchema $schema, ProcessorBuilder $builder): ObjectProcessor; |
58
|
|
|
} |
59
|
|
|
|