Skip to content

Commit 0990b48

Browse files
committed
Validates objects in Blueprints.
1 parent 63ae28d commit 0990b48

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

src/Blueprint.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ public function __construct($value = null) {
1414
}
1515

1616
public function setValue($new_value) {
17-
Helpers::checkType('new_value', 'stdClass', $new_value);
17+
if (gettype($new_value) !== 'object') {
18+
$this->value = $new_value;
19+
return $this;
20+
}
21+
1822
$previous_type = $this->value instanceof Element ? $this->value->getProp($this->type_prop) : null;
1923
$new_type = isset($new_value->{$this->type_prop}) ? $new_value->{$this->type_prop} : null;
2024
$type = $new_type ?: $previous_type ?: $this->default_type;
@@ -37,10 +41,20 @@ protected function getSetProps($object = null) {
3741
}
3842

3943
public function getValue() {
44+
if (gettype($new_value) !== 'object') {
45+
return $this->value;
46+
}
47+
4048
return $this->value->getValue();
4149
}
4250

4351
public function validate() {
52+
$value_type = gettype($this->value);
53+
if (gettype($this->value) !== 'object') {
54+
$encoded_value = json_encode($this->value);
55+
return [new Error("`$encoded_value` must be `object` not `$value_type`")];
56+
}
57+
4458
$errors = [];
4559
if ($this->value !== null) {
4660
$type = $this->value->getProp($this->type_prop);
@@ -55,10 +69,19 @@ public function validate() {
5569
}
5670

5771
public function setProp($prop_key, $prop_value) {
58-
return $this->value->setProp($prop_key, $prop_value);
72+
if (gettype($new_value) !== 'object') {
73+
return null;
74+
}
75+
76+
$this->value->setProp($prop_key, $prop_value);
77+
return $this;
5978
}
6079

6180
public function getProp($prop_key) {
81+
if (gettype($new_value) !== 'object') {
82+
return null;
83+
}
84+
6285
return $this->value->getProp($prop_key);
6386
}
6487
}

0 commit comments

Comments
 (0)