Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
62.50% covered (warning)
62.50%
10 / 16
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
UriSchemaProvider
62.50% covered (warning)
62.50%
10 / 16
50.00% covered (danger)
50.00%
2 / 4
7.90
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
 createSchema
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 addDisplaySchemaFor
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 addCreationSchemaFor
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
1<?php
2namespace Apie\SchemaGenerator\SchemaProviders;
3
4use Apie\SchemaGenerator\Builders\ComponentsBuilder;
5use Apie\SchemaGenerator\Interfaces\SchemaProvider;
6use cebe\openapi\spec\Components;
7use cebe\openapi\spec\Schema;
8use ReflectionClass;
9use Uri\Rfc3986\Uri;
10
11/**
12 * @implements SchemaProvider<Uri>
13 */
14class UriSchemaProvider implements SchemaProvider
15{
16    public function supports(ReflectionClass $class): bool
17    {
18        return $class->name === Uri::class;
19    }
20
21    private function createSchema(): Schema
22    {
23        // json_encode Duration returns this structure: {"seconds":0,"nanoseconds":6000000,"negative":false}
24        return new Schema([
25            'type' => 'string',
26            'format' => 'uri',
27            'example' => 'https://www.example.com/path?query#hash',
28        ]);
29    }
30
31    public function addDisplaySchemaFor(
32        ComponentsBuilder $componentsBuilder,
33        string $componentIdentifier,
34        ReflectionClass $class,
35        bool $nullable = false
36    ): Components {
37        $schema = $this->createSchema();
38        if ($nullable) {
39            $schema->nullable = true;
40        }
41        $componentsBuilder->setSchema($componentIdentifier, $schema);
42
43        return $componentsBuilder->getComponents();
44    }
45
46    public function addCreationSchemaFor(
47        ComponentsBuilder $componentsBuilder,
48        string $componentIdentifier,
49        ReflectionClass $class,
50        bool $nullable = false
51    ): Components {
52        $schema = $this->createSchema();
53        if ($nullable) {
54            $schema->nullable = true;
55        }
56        $componentsBuilder->setSchema($componentIdentifier, $schema);
57
58        return $componentsBuilder->getComponents();
59    }
60}