diff --git a/src/Entity.php b/src/Entity.php index c3ddfc1..c4d2876 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -60,7 +60,20 @@ public function __unset($name) public function validate() : bool { - $validation = (new Validator())->schemaValidation($this->data, $this->model->getSchema()); + 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; @@ -78,6 +91,7 @@ public function validate() : bool return true; } + public function equals($rightHandedObject) : bool { return (string) $this === (string) $rightHandedObject; @@ -104,6 +118,6 @@ public static function createFromJsonFile($filePath) : Entity public static function createFromJson($json) : Entity { - return new static(new Model($json)); + return new self(new Model($json)); } } diff --git a/src/Model.php b/src/Model.php index 4cf5eb2..1c58a22 100644 --- a/src/Model.php +++ b/src/Model.php @@ -8,23 +8,39 @@ final class Model { - private $schema; + private $model; + private $requiredFields; public function __construct(string $jsonSchema) { - $this->schema = Schema::fromJsonString($jsonSchema); + $this->model = json_decode($jsonSchema); + if (json_last_error() === JSON_ERROR_NONE) { + $this->requiredFields = $this->model->required; + } } - public function getSchema() : Schema + public function getModel() : \stdClass + { + return $this->model; + } + public function getRequiredFields() : array { - return $this->schema; + return $this->requiredFields; + } + + public function getSchema(?\stdClass $model = null) : Schema + { + if ($model !== null) { + return new Schema($model); + } + return new Schema($this->model); } - public static function fromJsonFile(string $filePath) : Model + public static function createFromJsonFile(string $filePath) : Model { if (!file_exists($filePath)) { throw new UnexpectedValueException(sprintf('Model definition file (%s) does not exist!', $filePath)); } - return new static(file_get_contents($filePath)); + return new self(file_get_contents($filePath)); } } diff --git a/tests/resources/test-schema-invalid-json.json b/tests/resources/test-schema-invalid-json.json index 5f7b8ef..363faf7 100644 --- a/tests/resources/test-schema-invalid-json.json +++ b/tests/resources/test-schema-invalid-json.json @@ -1 +1,4 @@ -{"test": } \ No newline at end of file +{ + "required": ["id"], + "test": +} \ No newline at end of file diff --git a/tests/unit/EntityTest.php b/tests/unit/EntityTest.php index 43de87b..4cba7d7 100644 --- a/tests/unit/EntityTest.php +++ b/tests/unit/EntityTest.php @@ -47,6 +47,20 @@ public function shouldReturnEntityObjectSuccessfully() : void $this->assertFalse(isset($entity->name)); } + /** + * @test + */ + public function shouldValidatePartiallySuccessfully() : void + { + $entity = Entity::createFromJsonFile(__DIR__.'/../resources/test-schema.json'); + $entity->name = 'John Doe'; + $entity->age = 31; + $requiredFields = ['name', 'age']; + $this->assertTrue($entity->validatePartially($requiredFields)); + $requiredFields = ['name', 'age', 'email']; + $this->expectException(\Selami\Entity\Exception\InvalidArgumentException::class); + $entity->validatePartially($requiredFields); + } /** * @test */ @@ -80,13 +94,15 @@ public function shouldCompareTwoEntityObjectSuccessfully() : void } + + /** * @test * @expectedException \Selami\Entity\Exception\InvalidArgumentException */ public function shouldFailForRequiredInput() : void { - $model = Model::fromJsonFile(__DIR__.'/../resources/test-schema.json'); + $model = Model::createFromJsonFile(__DIR__.'/../resources/test-schema.json'); $entity = new Entity($model); $entity->name = 'John Doe'; $entity->age = 31; diff --git a/tests/unit/ModelTest.php b/tests/unit/ModelTest.php index 4b9a193..c223f74 100644 --- a/tests/unit/ModelTest.php +++ b/tests/unit/ModelTest.php @@ -23,9 +23,19 @@ protected function _after() */ public function shouldReturnModelObjectSuccessfully() : void { - $model = Model::fromJsonFile(__DIR__.'/../resources/test-schema.json'); + $model = Model::createFromJsonFile(__DIR__.'/../resources/test-schema.json'); $schema = $model->getSchema(); $this->assertEquals('http://api.example.com/profile.json#', $schema->id()); + + $jsonSchema = file_get_contents(__DIR__.'/../resources/test-schema.json'); + $tmpSchema = json_decode($jsonSchema); + $requiredFields = $tmpSchema->required; + $model = new Model($jsonSchema); + $this->assertEquals($tmpSchema, $model->getModel()); + $modelRequiredFields = $model->getRequiredFields(); + $this->assertEquals($modelRequiredFields, $requiredFields); + $schema = $model->getSchema($tmpSchema); + $this->assertEquals('http://api.example.com/profile.json#', $schema->id()); } /** @@ -34,7 +44,7 @@ public function shouldReturnModelObjectSuccessfully() : void */ public function shouldFailForAFileThatDoesNotExist() : void { - $model = Model::fromJsonFile(__DIR__.'/../resources/test-schema-does-not-exist.json'); + $model = Model::createFromJsonFile(__DIR__.'/../resources/test-schema-does-not-exist.json'); $model->getSchema(); } @@ -44,7 +54,7 @@ public function shouldFailForAFileThatDoesNotExist() : void */ public function shouldFailForAFileThatContainsInvalidJson() : void { - $model = Model::fromJsonFile(__DIR__.'/../resources/test-schema-invalid-json.json'); + $model = Model::createFromJsonFile(__DIR__.'/../resources/test-schema-invalid-json.json'); $model->getSchema(); } }