Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.62% covered (warning)
84.62%
11 / 13
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
DateValueObjectSchemaProvider
84.62% covered (warning)
84.62%
11 / 13
33.33% covered (danger)
33.33%
1 / 3
4.06
0.00% covered (danger)
0.00%
0 / 1
 supports
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addDisplaySchemaFor
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addCreationSchemaFor
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
2.00
1<?php
2namespace Apie\SchemaGenerator\SchemaProviders;
3
4use Apie\Core\RegexUtils;
5use Apie\Core\ValueObjects\Interfaces\TimeRelatedValueObjectInterface;
6use Apie\DateformatToRegex\DateFormatToRegex;
7use Apie\SchemaGenerator\Builders\ComponentsBuilder;
8use Apie\SchemaGenerator\Interfaces\SchemaProvider;
9use cebe\openapi\spec\Components;
10use cebe\openapi\spec\Schema;
11use ReflectionClass;
12
13/**
14 * @implements SchemaProvider<TimeRelatedValueObjectInterface>
15 */
16class DateValueObjectSchemaProvider implements SchemaProvider
17{
18    public function supports(ReflectionClass $class): bool
19    {
20        return $class->implementsInterface(TimeRelatedValueObjectInterface::class);
21    }
22
23    public function addDisplaySchemaFor(
24        ComponentsBuilder $componentsBuilder,
25        string $componentIdentifier,
26        ReflectionClass $class,
27        bool $nullable = false
28    ): Components {
29        return $this->addCreationSchemaFor($componentsBuilder, $componentIdentifier, $class, $nullable);
30    }
31
32    public function addCreationSchemaFor(
33        ComponentsBuilder $componentsBuilder,
34        string $componentIdentifier,
35        ReflectionClass $class,
36        bool $nullable = false
37    ): Components {
38        $className = $class->name;
39        $schema = new Schema([
40            'type' => 'string',
41            'pattern' => RegexUtils::removeDelimiters(
42                DateFormatToRegex::formatToRegex($className::getDateFormat())
43            )
44        ]);
45        if ($nullable) {
46            $schema->nullable = true;
47        }
48        $componentsBuilder->setSchema($componentIdentifier, $schema);
49
50        return $componentsBuilder->getComponents();
51    }
52}