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\AnySchema; |
12
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Schema\ObjectSchema; |
13
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema; |
14
|
|
|
use KleijnWeb\PhpApi\Hydrator\ProcessorBuilder; |
15
|
|
|
use KleijnWeb\PhpApi\Hydrator\Processors\AnyProcessor; |
16
|
|
|
use KleijnWeb\PhpApi\Hydrator\Processors\Object\LooseSimpleObjectProcessor; |
17
|
|
|
use KleijnWeb\PhpApi\Hydrator\Processors\Object\ObjectProcessor; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author John Kleijn <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class LooseSimpleObjectFactory extends ObjectFactory |
23
|
|
|
{ |
24
|
|
|
const PRIORITY = 500; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Schema $schema |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
|
View Code Duplication |
public function supports(Schema $schema): bool |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
/** @var ObjectSchema $schema */ |
33
|
|
|
if (!parent::supports($schema) || $schema->hasComplexType()) { |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (isset($schema->getDefinition()->additionalProperties) |
38
|
|
|
&& !$schema->getDefinition()->additionalProperties) { |
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return true; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return int |
47
|
|
|
*/ |
48
|
|
|
public function getPriority(): int |
49
|
|
|
{ |
50
|
|
|
return self::PRIORITY; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param ObjectSchema $schema |
55
|
|
|
* @param ProcessorBuilder $builder |
56
|
|
|
* @return ObjectProcessor |
57
|
|
|
*/ |
58
|
|
|
public function instantiate(ObjectSchema $schema, ProcessorBuilder $builder): ObjectProcessor |
59
|
|
|
{ |
60
|
|
|
/** @var AnyProcessor $anyProcessor */ |
61
|
|
|
$anyProcessor = $builder->build(new AnySchema()); |
62
|
|
|
|
63
|
|
|
return new LooseSimpleObjectProcessor($schema, $anyProcessor); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.