1
|
|
|
<?php |
2
|
|
|
namespace NOSQL\Dto\Model; |
3
|
|
|
|
4
|
|
|
use NOSQL\Exceptions\NOSQLValidationException; |
5
|
|
|
use NOSQL\Services\base\NOSQLBase; |
6
|
|
|
use PSFS\base\dto\Dto; |
7
|
|
|
use PSFS\base\types\helpers\InjectorHelper; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class NOSQLModelDto |
11
|
|
|
* @package NOSQL\Dto\Model |
12
|
|
|
*/ |
13
|
|
|
abstract class NOSQLModelDto extends Dto { |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
* @label Model identifier |
17
|
|
|
*/ |
18
|
|
|
protected $_id; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
public function getPk() |
24
|
|
|
{ |
25
|
|
|
return $this->_id; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function resetPk() { |
29
|
|
|
$this->_id = null; |
30
|
|
|
$this->_last_update = null; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $id |
35
|
|
|
* @throws NOSQLValidationException |
36
|
|
|
*/ |
37
|
|
|
public function setPk(string $id) |
38
|
|
|
{ |
39
|
|
|
if(!empty($this->_id)) { |
40
|
|
|
throw new NOSQLValidationException(t('Primary key already defined'), NOSQLValidationException::NOSQL_VALIDATION_ID_ALREADY_DEFINED); |
41
|
|
|
} |
42
|
|
|
$this->_id = $id; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $format |
47
|
|
|
* @return \DateTime|string |
48
|
|
|
*/ |
49
|
|
|
public function getLastUpdate($format = null) |
50
|
|
|
{ |
51
|
|
|
return null !== $format && null !== $this->_last_update ? $this->_last_update->format($format) : $this->_last_update; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param \DateTime|null $last_update |
56
|
|
|
* @throws \Exception |
57
|
|
|
*/ |
58
|
|
|
public function setLastUpdate(\DateTime $last_update = null) |
59
|
|
|
{ |
60
|
|
|
$this->_last_update = $last_update ?: new \DateTime(); |
61
|
|
|
} |
62
|
|
|
/** |
63
|
|
|
* @var \DateTime |
64
|
|
|
* @label Last update at |
65
|
|
|
*/ |
66
|
|
|
protected $_last_update; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param bool $throwException |
70
|
|
|
* @return array |
71
|
|
|
* @throws NOSQLValidationException |
72
|
|
|
* @throws \ReflectionException |
73
|
|
|
*/ |
74
|
|
|
public function validate($throwException = false) { |
75
|
|
|
$errors = []; |
76
|
|
|
$reflection = new \ReflectionClass(get_called_class()); |
77
|
|
|
foreach($reflection->getProperties() as $property) { |
78
|
|
|
$required = InjectorHelper::checkIsRequired($property->getDocComment()); |
79
|
|
|
$value = $property->getValue($this); |
80
|
|
|
if($required && empty($value)) { |
81
|
|
|
if($throwException) { |
82
|
|
|
throw new NOSQLValidationException(t('Empty value for property ') . $property->getName(), NOSQLValidationException::NOSQL_VALIDATION_REQUIRED); |
83
|
|
|
} else { |
84
|
|
|
$errors[] = $property->getName(); |
85
|
|
|
} |
86
|
|
|
} else { |
87
|
|
|
$this->checkType($throwException, $property, $value, $errors); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
return $errors; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function toArray() |
94
|
|
|
{ |
95
|
|
|
$array = parent::toArray(); |
96
|
|
|
if(null !== $this->getPk()) { |
|
|
|
|
97
|
|
|
$array['_id'] = $this->getPk(); |
98
|
|
|
} |
99
|
|
|
$array['_last_update'] = $this->getLastUpdate(\DateTime::ATOM); |
100
|
|
|
return $array; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $throwException |
105
|
|
|
* @param \ReflectionProperty $property |
106
|
|
|
* @param $value |
107
|
|
|
* @param array $errors |
108
|
|
|
* @throws NOSQLValidationException |
109
|
|
|
*/ |
110
|
|
|
private function checkType($throwException, \ReflectionProperty $property, $value, array &$errors) |
111
|
|
|
{ |
112
|
|
|
$type = InjectorHelper::extractVarType($property->getDocComment()); |
|
|
|
|
113
|
|
|
switch (strtolower($type)) { |
114
|
|
|
case NOSQLBase::NOSQL_TYPE_LONG: |
115
|
|
|
case NOSQLBase::NOSQL_TYPE_INTEGER: |
116
|
|
|
case NOSQLBase::NOSQL_TYPE_DOUBLE: |
117
|
|
|
if (!is_numeric($value)) { |
118
|
|
|
$errors[] = $property->getName(); |
119
|
|
|
} |
120
|
|
|
break; |
121
|
|
|
case NOSQLBase::NOSQL_TYPE_ENUM: |
122
|
|
|
$values = explode('|', InjectorHelper::getValues($property->getDocComment())); |
|
|
|
|
123
|
|
|
if (!in_array($value, $values)) { |
124
|
|
|
$errors[] = $property->getName(); |
125
|
|
|
} |
126
|
|
|
break; |
127
|
|
|
case NOSQLBase::NOSQL_TYPE_ARRAY: |
128
|
|
|
if (!is_array($value)) { |
129
|
|
|
$errors[] = $property->getName(); |
130
|
|
|
} |
131
|
|
|
break; |
132
|
|
|
case NOSQLBase::NOSQL_TYPE_BOOLEAN: |
133
|
|
|
if (!in_array($value, [true, false, 0, 1])) { |
134
|
|
|
$errors[] = $property->getName(); |
135
|
|
|
} |
136
|
|
|
break; |
137
|
|
|
} |
138
|
|
|
if (in_array($property->getName(), $errors) && $throwException) { |
139
|
|
|
throw new NOSQLValidationException(t('Format not valid for property ') . $property->getName(), NOSQLValidationException::NOSQL_VALIDATION_NOT_VALID); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |