luca-rath /
php-json-schema
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace JsonSchema\Property; |
||
| 6 | |||
| 7 | use JsonSchema\Schema\SchemaInterface; |
||
| 8 | use Webmozart\Assert\Assert; |
||
| 9 | |||
| 10 | class Property implements PropertyInterface |
||
| 11 | { |
||
| 12 | private string $name; |
||
| 13 | private bool $required; |
||
| 14 | private SchemaInterface $schema; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var string[]|null |
||
| 18 | */ |
||
| 19 | private ?array $dependentRequired = null; |
||
| 20 | private ?SchemaInterface $dependentSchema = null; |
||
| 21 | |||
| 22 | 12 | public function __construct(string $name, bool $required, SchemaInterface $schema) |
|
| 23 | { |
||
| 24 | 12 | $this->name = $name; |
|
| 25 | 12 | $this->required = $required; |
|
| 26 | 12 | $this->schema = $schema; |
|
| 27 | 12 | } |
|
| 28 | |||
| 29 | 3 | public function getName(): string |
|
| 30 | { |
||
| 31 | 3 | return $this->name; |
|
| 32 | } |
||
| 33 | |||
| 34 | 3 | public function isRequired(): bool |
|
| 35 | { |
||
| 36 | 3 | return $this->required; |
|
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @return string[]|null |
||
| 41 | */ |
||
| 42 | 1 | public function getDependentRequired(): ?array |
|
| 43 | { |
||
| 44 | 1 | return $this->dependentRequired; |
|
| 45 | } |
||
| 46 | |||
| 47 | 1 | public function getDependentSchema(): ?object |
|
| 48 | { |
||
| 49 | 1 | if (null === $this->dependentSchema) { |
|
| 50 | 1 | return null; |
|
| 51 | } |
||
| 52 | |||
| 53 | 1 | return $this->dependentSchema->toJsonSchema(); |
|
| 54 | } |
||
| 55 | |||
| 56 | 1 | public function name(string $name): self |
|
| 57 | { |
||
| 58 | 1 | $instance = clone $this; |
|
| 59 | 1 | $instance->name = $name; |
|
| 60 | |||
| 61 | 1 | return $instance; |
|
| 62 | } |
||
| 63 | |||
| 64 | 1 | public function required(bool $required): self |
|
| 65 | { |
||
| 66 | 1 | $instance = clone $this; |
|
| 67 | 1 | $instance->required = $required; |
|
| 68 | |||
| 69 | 1 | return $instance; |
|
| 70 | } |
||
| 71 | |||
| 72 | 1 | public function schema(SchemaInterface $schema): self |
|
| 73 | { |
||
| 74 | 1 | $instance = clone $this; |
|
| 75 | 1 | $instance->schema = $schema; |
|
| 76 | |||
| 77 | 1 | return $instance; |
|
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param string[]|null $dependentRequired |
||
| 82 | */ |
||
| 83 | 5 | public function dependentRequired(?array $dependentRequired): self |
|
| 84 | { |
||
| 85 | 5 | if (null !== $dependentRequired) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 86 | 5 | Assert::isNonEmptyList($dependentRequired); |
|
| 87 | 3 | Assert::uniqueValues($dependentRequired); |
|
| 88 | 2 | Assert::allStringNotEmpty($dependentRequired); |
|
| 89 | } |
||
| 90 | |||
| 91 | 1 | $instance = clone $this; |
|
| 92 | 1 | $instance->dependentRequired = $dependentRequired; |
|
| 93 | |||
| 94 | 1 | return $instance; |
|
| 95 | } |
||
| 96 | |||
| 97 | 1 | public function dependentSchema(?SchemaInterface $dependentSchema): self |
|
| 98 | { |
||
| 99 | 1 | $instance = clone $this; |
|
| 100 | 1 | $instance->dependentSchema = $dependentSchema; |
|
| 101 | |||
| 102 | 1 | return $instance; |
|
| 103 | } |
||
| 104 | |||
| 105 | 4 | public function toJsonSchema(): object |
|
| 106 | { |
||
| 107 | 4 | return $this->schema->toJsonSchema(); |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return static |
||
| 112 | */ |
||
| 113 | 1 | public static function create(string $name, bool $required, SchemaInterface $schema): self |
|
| 114 | { |
||
| 115 | 1 | return new static($name, $required, $schema); |
|
| 116 | } |
||
| 117 | } |
||
| 118 |