Entity::__unset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Selami\Entity;
5
6
use JsonSerializable;
7
use stdClass;
8
use Opis\JsonSchema\Validator;
9
use Selami\Entity\Exception\InvalidArgumentException;
10
use Selami\Entity\Exception\UnexpectedValueException;
11
use Ramsey\Uuid\Uuid;
12
13
final class Entity implements JsonSerializable
14
{
15
    /**
16
     * @var Model
17
     */
18
    private $model;
19
    /*
20
     * @var stdClass
21
     */
22
    private $data;
23
24
    public function __construct(Model $model, ?stdClass $data = null)
25
    {
26
        $this->model = $model;
27
        $this->data = $data;
28
        if ($data === null) {
29
            $this->data = new stdClass();
30
        }
31
        $this->checkAndSetId();
32
    }
33
34
    private function checkAndSetId() : void
35
    {
36
        if (!isset($this->data->id)) {
37
            $this->data->id = Uuid::uuid4()->toString();
38
        }
39
    }
40
41
    public function __get($name)
42
    {
43
        return $this->data->{$name};
44
    }
45
46
    public function __set($name, $value) : void
47
    {
48
        $this->data->{$name} = $value;
49
    }
50
51
    public function __isset($name) : bool
52
    {
53
        return property_exists($this->data, $name);
54
    }
55
56
    public function __unset($name)
57
    {
58
        unset($this->data->{$name});
59
    }
60
61
    public function validate() : bool
62
    {
63
        $validation = (new Validator())->schemaValidation($this->data, $this->model->getSchema());
64
        if (!$validation->isValid()) {
65
            $errors = $validation->getErrors();
66
            $message = 'Data validation failed.' . PHP_EOL;
67
            foreach ($errors as $error) {
68
                $message .= sprintf(
69
                    'ERROR: %s. %s',
70
                    $error->keyword(),
71
                    json_encode($error->keywordArgs(), JSON_PRETTY_PRINT)
72
                ) . PHP_EOL;
73
            }
74
            throw new InvalidArgumentException(
75
                $message
76
            );
77
        }
78
        return true;
79
    }
80
81
    public function equals($rightHandedObject) : bool
82
    {
83
        return (string) $this === (string) $rightHandedObject;
84
    }
85
86
    public function jsonSerialize() : string
87
    {
88
        return (string) json_encode($this->data);
89
    }
90
91
    public function __toString()
92
    {
93
        return $this->jsonSerialize();
94
    }
95
96
    public static function createFromJsonFile($filePath) : Entity
97
    {
98
        if (!file_exists($filePath)) {
99
            throw new UnexpectedValueException(sprintf('Model definition file (%s) does not exist!', $filePath));
100
        }
101
        $json = file_get_contents($filePath);
102
        return self::createFromJson($json);
103
    }
104
105
    public static function createFromJson($json) : Entity
106
    {
107
        return new static(new Model($json));
108
    }
109
}
110