Completed
Push — master ( 1b0d8b...4b53bc )
by John
03:07
created

ScalarSchema::getEnum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\Descriptions package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace KleijnWeb\PhpApi\Descriptions\Description\Schema;
9
10
/**
11
 * Represents standard JSON Schema but with support for complex types
12
 *
13
 * @author John Kleijn <[email protected]>
14
 */
15
class ScalarSchema extends Schema
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $format;
21
22
    /**
23
     * @var array|string
24
     */
25
    protected $enum;
26
27
    /**
28
     * @var string|null
29
     */
30
    protected $pattern;
31
32
    /**
33
     * ScalarSchema constructor.
34
     *
35
     * @param \stdClass $definition
36
     */
37
    public function __construct(\stdClass $definition)
38
    {
39
        parent::__construct($definition);
40
        $this->format  = isset($definition->format) ? $definition->format : null;
41
        $this->enum    = isset($definition->enum) ? (array)$definition->enum : null;
42
        $this->pattern = isset($definition->pattern) ? $definition->pattern : null;
43
    }
44
45
    /**
46
     * @return bool
47
     */
48
    public function isDateTime(): bool
49
    {
50
        return $this->isType(self::TYPE_STRING)
51
        && ($this->hasFormat(self::FORMAT_DATE) || $this->hasFormat(self::FORMAT_DATE_TIME));
52
    }
53
54
    /**
55
     * @return array|string
56
     */
57
    public function getEnum()
58
    {
59
        return $this->enum;
60
    }
61
62
    /**
63
     * @return null|string
64
     */
65
    public function getPattern()
66
    {
67
        return $this->pattern;
68
    }
69
70
71
    /**
72
     * @return string
73
     */
74
    public function getFormat()
75
    {
76
        return $this->format;
77
    }
78
79
    /**
80
     * @param string $format
81
     *
82
     * @return bool
83
     */
84
    public function hasFormat(string $format): bool
85
    {
86
        return $this->format === $format;
87
    }
88
}
89