• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wol-soft / php-json-schema-model-generator / 14407937636

11 Apr 2025 04:41PM UTC coverage: 98.813%. Remained the same
14407937636

Pull #88

github

web-flow
Merge 7dcf4b3f6 into 8823c7c14
Pull Request #88: Fix code typo in README

2914 of 2949 relevant lines covered (98.81%)

517.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.15
/src/PropertyProcessor/ComposedValue/IfProcessor.php
1
<?php
2

3
declare(strict_types = 1);
4

5
namespace PHPModelGenerator\PropertyProcessor\ComposedValue;
6

7
use PHPModelGenerator\Exception\SchemaException;
8
use PHPModelGenerator\Model\Property\CompositionPropertyDecorator;
9
use PHPModelGenerator\Model\Property\PropertyInterface;
10
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
11
use PHPModelGenerator\Model\Validator;
12
use PHPModelGenerator\Model\Validator\ComposedPropertyValidator;
13
use PHPModelGenerator\Model\Validator\ConditionalPropertyValidator;
14
use PHPModelGenerator\Model\Validator\RequiredPropertyValidator;
15
use PHPModelGenerator\PropertyProcessor\Property\AbstractValueProcessor;
16
use PHPModelGenerator\PropertyProcessor\PropertyMetaDataCollection;
17
use PHPModelGenerator\PropertyProcessor\PropertyFactory;
18
use PHPModelGenerator\PropertyProcessor\PropertyProcessorFactory;
19
use PHPModelGenerator\Utils\RenderHelper;
20

21
/**
22
 * Class IfProcessor
23
 *
24
 * @package PHPModelGenerator\PropertyProcessor\ComposedValue
25
 */
26
class IfProcessor extends AbstractValueProcessor implements ComposedPropertiesInterface
27
{
28
    /**
29
     * @inheritdoc
30
     */
31
    protected function generateValidators(PropertyInterface $property, JsonSchema $propertySchema): void
32
    {
33
        $json = $propertySchema->getJson()['propertySchema']->getJson();
38✔
34

35
        if (!isset($json['then']) && !isset($json['else'])) {
38✔
36
            throw new SchemaException(
1✔
37
                sprintf(
1✔
38
                    'Incomplete conditional composition for property %s in file %s',
1✔
39
                    $property->getName(),
1✔
40
                    $property->getJsonSchema()->getFile(),
1✔
41
                )
1✔
42
            );
1✔
43
        }
44

45
        $propertyFactory = new PropertyFactory(new PropertyProcessorFactory());
37✔
46

47
        $properties = [];
37✔
48

49
        foreach (['if', 'then', 'else'] as $compositionElement) {
37✔
50
            if (!isset($json[$compositionElement])) {
37✔
51
                $properties[$compositionElement] = null;
×
52
                continue;
×
53
            }
54

55
            $compositionSchema = $propertySchema->getJson()['propertySchema']->withJson($json[$compositionElement]);
37✔
56

57
            $compositionProperty = new CompositionPropertyDecorator(
37✔
58
                $property->getName(),
37✔
59
                $compositionSchema,
37✔
60
                $propertyFactory
37✔
61
                    ->create(
37✔
62
                        new PropertyMetaDataCollection([$property->getName() => $property->isRequired()]),
37✔
63
                        $this->schemaProcessor,
37✔
64
                        $this->schema,
37✔
65
                        $property->getName(),
37✔
66
                        $compositionSchema,
37✔
67
                    )
37✔
68
            );
37✔
69

70
            $compositionProperty->onResolve(static function () use ($compositionProperty): void {
37✔
71
                $compositionProperty->filterValidators(static fn(Validator $validator): bool =>
37✔
72
                    !is_a($validator->getValidator(), RequiredPropertyValidator::class) &&
37✔
73
                    !is_a($validator->getValidator(), ComposedPropertyValidator::class),
37✔
74
                );
37✔
75
            });
37✔
76

77
            $properties[$compositionElement] = $compositionProperty;
37✔
78
        }
79

80
        $property->addValidator(
37✔
81
            new ConditionalPropertyValidator(
37✔
82
                $this->schemaProcessor->getGeneratorConfiguration(),
37✔
83
                $property,
37✔
84
                $properties,
37✔
85
                [
37✔
86
                    'ifProperty' => $properties['if'],
37✔
87
                    'thenProperty' => $properties['then'],
37✔
88
                    'elseProperty' => $properties['else'],
37✔
89
                    'schema' => $this->schema,
37✔
90
                    'generatorConfiguration' => $this->schemaProcessor->getGeneratorConfiguration(),
37✔
91
                    'viewHelper' => new RenderHelper($this->schemaProcessor->getGeneratorConfiguration()),
37✔
92
                    'onlyForDefinedValues' => $propertySchema->getJson()['onlyForDefinedValues'],
37✔
93
                ],
37✔
94
            ),
37✔
95
            100,
37✔
96
        );
37✔
97

98
        //parent::generateValidators($property, $propertySchema);
99
    }
100
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc