|
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
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Element; |
|
11
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Visitor\VisiteeMixin; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Represents standard JSON Schema but with support for complex types |
|
15
|
|
|
* |
|
16
|
|
|
* @author John Kleijn <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class Schema implements Element |
|
19
|
|
|
{ |
|
20
|
|
|
use VisiteeMixin; |
|
21
|
|
|
|
|
22
|
|
|
const TYPE_ANY = 'any'; |
|
23
|
|
|
const TYPE_ARRAY = 'array'; |
|
24
|
|
|
const TYPE_BOOL = 'boolean'; |
|
25
|
|
|
const TYPE_INT = 'integer'; |
|
26
|
|
|
const TYPE_NUMBER = 'number'; |
|
27
|
|
|
const TYPE_NULL = 'null'; |
|
28
|
|
|
const TYPE_OBJECT = 'object'; |
|
29
|
|
|
const TYPE_STRING = 'string'; |
|
30
|
|
|
|
|
31
|
|
|
const FORMAT_DATE = 'date'; |
|
32
|
|
|
const FORMAT_DATE_TIME = 'date-time'; |
|
33
|
|
|
|
|
34
|
|
|
const FORMAT_INT32 = 'int32'; |
|
35
|
|
|
const FORMAT_INT64 = 'int64'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var \stdClass |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $definition; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $type; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Schema constructor. |
|
49
|
|
|
* |
|
50
|
|
|
* @param \stdClass $definition |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct(\stdClass $definition) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->type = $definition->type; |
|
55
|
|
|
$this->definition = $definition; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $type |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
|
|
public function isPrimitive(string $type): bool |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->type === $type; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getType(): string |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->type; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return \stdClass |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getDefinition() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->definition; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $type |
|
86
|
|
|
* |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
|
|
public function isType($type) |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->isPrimitive($type); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|