Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
DateTimeSchemaProvider
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 supports
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 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%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2namespace Apie\SchemaGenerator\SchemaProviders;
3
4use Apie\Core\RegexUtils;
5use Apie\DateformatToRegex\DateFormatToRegex;
6use Apie\SchemaGenerator\Builders\ComponentsBuilder;
7use Apie\SchemaGenerator\Interfaces\SchemaProvider;
8use cebe\openapi\spec\Components;
9use cebe\openapi\spec\Schema;
10use DateTimeInterface;
11use ReflectionClass;
12
13/**
14 * @implements SchemaProvider<DateTimeInterface>
15 */
16class DateTimeSchemaProvider implements SchemaProvider
17{
18    public function supports(ReflectionClass $class): bool
19    {
20        return $class->implementsInterface(DateTimeInterface::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        $schema = new Schema([
39            'type' => 'string',
40            'format' => 'datetime',
41            'pattern' => RegexUtils::removeDelimiters(
42                DateFormatToRegex::formatToRegex(DateTimeInterface::ATOM)
43            )
44        ]);
45        if ($nullable) {
46            $schema->nullable = true;
47        }
48        $componentsBuilder->setSchema($componentIdentifier, $schema);
49
50        return $componentsBuilder->getComponents();
51    }
52}