Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
StringValueObjectSchemaProvider
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
1 / 1
 supports
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 addDisplaySchemaFor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addCreationSchemaFor
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2namespace Apie\SchemaGenerator\SchemaProviders;
3
4use Apie\Core\RegexUtils;
5use Apie\Core\ValueObjects\Interfaces\HasRegexValueObjectInterface;
6use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface;
7use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface;
8use Apie\Core\ValueObjects\Utils;
9use Apie\SchemaGenerator\Builders\ComponentsBuilder;
10use Apie\SchemaGenerator\Interfaces\SchemaProvider;
11use Apie\SchemaGenerator\Other\JsonSchemaFormatValidator;
12use cebe\openapi\spec\Components;
13use cebe\openapi\spec\Schema;
14use League\OpenAPIValidation\Schema\TypeFormats\FormatsContainer;
15use ReflectionClass;
16
17/**
18 * @implements SchemaProvider<StringValueObjectInterface>
19 */
20class StringValueObjectSchemaProvider implements SchemaProvider
21{
22    public function supports(ReflectionClass $class): bool
23    {
24        if (!in_array(ValueObjectInterface::class, $class->getInterfaceNames())) {
25            return false;
26        }
27        $returnType = (string) $class->getMethod('toNative')->getReturnType();
28        return $returnType === 'string' || $returnType === '?string';
29    }
30
31    public function addDisplaySchemaFor(
32        ComponentsBuilder $componentsBuilder,
33        string $componentIdentifier,
34        ReflectionClass $class,
35        bool $nullable = false
36    ): Components {
37        return $this->addCreationSchemaFor($componentsBuilder, $componentIdentifier, $class, $nullable);
38    }
39
40    public function addCreationSchemaFor(
41        ComponentsBuilder $componentsBuilder,
42        string $componentIdentifier,
43        ReflectionClass $class,
44        bool $nullable = false
45    ): Components {
46        $format = strtolower(Utils::getDisplayNameForValueObject($class));
47        if (class_exists(FormatsContainer::class) && !FormatsContainer::getFormat('string', $format)) {
48            FormatsContainer::registerFormat('string', $format, new JsonSchemaFormatValidator($class->name));
49        }
50        $schema = new Schema([
51            'type' => 'string',
52            'format' => $format,
53        ]);
54        ComponentsBuilder::addDescriptionOfObject($schema, $class);
55        if ($class->implementsInterface(HasRegexValueObjectInterface::class)) {
56            $className = $class->name;
57            $schema->pattern = RegexUtils::removeDelimiters($className::getRegularExpression());
58        }
59        if ($nullable) {
60            $schema->nullable = true;
61        }
62        $componentsBuilder->setSchema($componentIdentifier, $schema);
63
64        return $componentsBuilder->getComponents();
65    }
66}