-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e4f3af5
commit 5593a6b
Showing
8 changed files
with
234 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cortex\JsonSchema\Types\Concerns; | ||
|
||
trait HasConst | ||
{ | ||
/** | ||
* @var int|string|bool|float|null | ||
*/ | ||
protected mixed $const = null; | ||
|
||
protected bool $hasConst = false; | ||
|
||
/** | ||
* Set the constant value. | ||
* | ||
* @param int|string|bool|float|null $value | ||
*/ | ||
public function const(mixed $value): static | ||
{ | ||
$this->const = $value; | ||
$this->hasConst = true; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Add const value to schema array. | ||
* | ||
* @param array<string, mixed> $schema | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
protected function addConstToSchema(array $schema): array | ||
{ | ||
if ($this->hasConst) { | ||
$schema['const'] = $this->const; | ||
} | ||
|
||
return $schema; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cortex\JsonSchema\Types\Concerns; | ||
|
||
trait HasEnum | ||
{ | ||
/** | ||
* @var non-empty-array<int|string|bool|float|null>|null | ||
*/ | ||
protected ?array $enum = null; | ||
|
||
/** | ||
* Set the allowed enum values. | ||
* | ||
* @param non-empty-array<int|string|bool|float|null> $values | ||
*/ | ||
public function enum(array $values): static | ||
{ | ||
$this->enum = array_values(array_unique($values, SORT_REGULAR)); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Add enum values to schema array. | ||
* | ||
* @param array<string, mixed> $schema | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
protected function addEnumToSchema(array $schema): array | ||
{ | ||
if ($this->enum !== null) { | ||
$schema['enum'] = $this->enum; | ||
} | ||
|
||
return $schema; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters