-
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.
ValueObject support added which does not include id attribute
This can also be used for form validation or etc…
- Loading branch information
Showing
7 changed files
with
344 additions
and
106 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,89 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Selami\Entity; | ||
|
||
use stdClass; | ||
use Opis\JsonSchema\Validator; | ||
use Selami\Entity\Exception\InvalidArgumentException; | ||
|
||
trait ObjectTrait | ||
{ | ||
/** | ||
* @var Model | ||
*/ | ||
private $model; | ||
|
||
/* | ||
* @var stdClass | ||
*/ | ||
private $data; | ||
|
||
public function __get($name) | ||
{ | ||
return $this->data->{$name}; | ||
} | ||
|
||
public function __set($name, $value) : void | ||
{ | ||
$this->data->{$name} = $value; | ||
} | ||
|
||
public function __isset($name) : bool | ||
{ | ||
return property_exists($this->data, $name); | ||
} | ||
|
||
public function __unset($name) | ||
{ | ||
unset($this->data->{$name}); | ||
} | ||
|
||
public function validate() : bool | ||
{ | ||
return $this->validateData($this->data, $this->model->getSchema()); | ||
} | ||
|
||
public function validatePartially(array $requiredFields) : bool | ||
{ | ||
$model = $this->model->getModel(); | ||
$model->required = $requiredFields; | ||
$schema = $this->model->getSchema($model); | ||
return $this->validateData($this->data, $schema); | ||
} | ||
|
||
private function validateData($data, $schema) : bool | ||
{ | ||
$validation = (new Validator())->schemaValidation($data, $schema); | ||
if (!$validation->isValid()) { | ||
$errors = $validation->getErrors(); | ||
$message = 'Data validation failed.' . PHP_EOL; | ||
foreach ($errors as $error) { | ||
$message .= sprintf( | ||
'ERROR: %s. %s', | ||
$error->keyword(), | ||
json_encode($error->keywordArgs(), JSON_PRETTY_PRINT) | ||
) . PHP_EOL; | ||
} | ||
throw new InvalidArgumentException( | ||
$message | ||
); | ||
} | ||
return true; | ||
} | ||
|
||
public function equals($rightHandedObject) : bool | ||
{ | ||
return (string) $this === (string) $rightHandedObject; | ||
} | ||
|
||
public function jsonSerialize() : stdClass | ||
{ | ||
return $this->data; | ||
} | ||
|
||
public function __toString() : string | ||
{ | ||
return (string) json_encode($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,35 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Selami\Entity; | ||
|
||
use stdClass; | ||
use JsonSerializable; | ||
use Selami\Entity\Exception\UnexpectedValueException; | ||
|
||
final class ValueObject implements JsonSerializable | ||
{ | ||
use ObjectTrait; | ||
|
||
public function __construct(Model $model, ?stdClass $data = null) | ||
{ | ||
$this->model = $model; | ||
$this->data = $data; | ||
if ($data === null) { | ||
$this->data = new stdClass(); | ||
} | ||
} | ||
|
||
public static function createFromJsonFile($filePath) : ValueObject | ||
{ | ||
if (!file_exists($filePath)) { | ||
throw new UnexpectedValueException(sprintf('Model definition file (%s) does not exist!', $filePath)); | ||
} | ||
$json = file_get_contents($filePath); | ||
return static::createFromJson($json); | ||
} | ||
public static function createFromJson($json) : ValueObject | ||
{ | ||
return new static(new Model($json)); | ||
} | ||
} |
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,81 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"$id": "http://api.example.com/profile.json#", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"minLength": 1, | ||
"maxLength": 64, | ||
"pattern": "^[a-zA-Z0-9\\-]+(\\s[a-zA-Z0-9\\-]+)*$" | ||
}, | ||
"age": { | ||
"type": "integer", | ||
"minimum": 18, | ||
"maximum": 100 | ||
}, | ||
"email": { | ||
"type": "string", | ||
"maxLength": 128, | ||
"format": "email" | ||
}, | ||
"website": { | ||
"type": ["string", "null"], | ||
"maxLength": 128, | ||
"format": "hostname" | ||
}, | ||
"location": { | ||
"type": "object", | ||
"properties": { | ||
"country": { | ||
"enum": ["US", "CA", "GB"] | ||
}, | ||
"address": { | ||
"type": "string", | ||
"maxLength": 128 | ||
} | ||
}, | ||
"required": ["country", "address"], | ||
"additionalProperties": false | ||
}, | ||
"available_for_hire": { | ||
"type": "boolean" | ||
}, | ||
"interests": { | ||
"type": "array", | ||
"minItems": 3, | ||
"maxItems": 100, | ||
"uniqueItems": true, | ||
"items": { | ||
"type": "string", | ||
"maxLength": 120 | ||
} | ||
}, | ||
"skills": { | ||
"type": "array", | ||
"maxItems": 100, | ||
"uniqueItems": true, | ||
"items": { | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"minLenght": 1, | ||
"maxLength": 64 | ||
}, | ||
"value": { | ||
"type": "number", | ||
"minimum": 0, | ||
"maximum": 100, | ||
"multipleOf": 0.25 | ||
} | ||
}, | ||
"required": ["name", "value"], | ||
"additionalProperties": false | ||
} | ||
} | ||
}, | ||
"required": ["name", "age", "email", "location", | ||
"available_for_hire", "interests", "skills"], | ||
"additionalProperties": false | ||
} |
Oops, something went wrong.