-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support mixed types for validators (#8)
* Support mixed types for validators Changes: - Allow arrays to be received as a body parameter - All validators expect mixed input/output instead of just string - More supporting tests for validators and data mapping - Fixed documentation on validators Fixes: - When missing a JSON attribute throw exception not fatal error - Correctly passing the validators from the parameters
- Loading branch information
1 parent
907a58c
commit d62aa14
Showing
17 changed files
with
229 additions
and
24 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace willitscale\StreetlampTests\TestApp; | ||
|
||
use JsonSerializable; | ||
use willitscale\Streetlamp\Attributes\DataBindings\Json\JsonObject; | ||
use willitscale\Streetlamp\Attributes\DataBindings\Json\JsonProperty; | ||
|
||
#[JsonObject] | ||
class DataType implements JsonSerializable | ||
{ | ||
public function __construct( | ||
#[JsonProperty(true)] private string $name, | ||
#[JsonProperty(true)] private int $age | ||
) { | ||
} | ||
|
||
public function jsonSerialize(): mixed | ||
{ | ||
return (object)get_object_vars($this); | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
namespace willitscale\StreetlampTests\TestApp; | ||
|
||
use Attribute; | ||
use willitscale\Streetlamp\Attributes\Validators\ValidatorInterface; | ||
|
||
#[Attribute] | ||
class DataValidator implements ValidatorInterface | ||
{ | ||
public function validate(mixed $value): bool | ||
{ | ||
$value = json_decode($value); | ||
|
||
if (!is_array($value)) { | ||
return false; | ||
} | ||
|
||
foreach ($value as $object) { | ||
if (!isset($object->name) || !isset($object->age)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function sanitize(mixed $value): mixed | ||
{ | ||
$value = json_decode($value); | ||
|
||
foreach ($value as &$object) { | ||
$object = new DataType($object->name, $object->age); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
Oops, something went wrong.