Skip to content

Commit 9388e2d

Browse files
committed
Improves error messages.
1 parent a1c8be1 commit 9388e2d

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

src/Atom.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ public function toJson() {
2727
*/
2828
public function fromJson($json) {
2929
Helpers::checkType('json', 'string', $json);
30-
return $this->setValue(json_decode($json));
30+
$decoded_value = json_decode($json);
31+
if ($decoded_value === null && ($json !== 'null' || $json !== '')) {
32+
throw new \Exception('Invalid JSON.');
33+
}
34+
return $this->setValue($decoded_value);
3135
}
3236

3337
/**

src/Element.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ public function getKnownProps() {
4949
* @return Element $this
5050
*/
5151
public function setValue($new_value) {
52-
Helpers::checkType('new_value', 'stdClass', $new_value);
52+
if (gettype($new_value) !== 'object') {
53+
return parent::setValue($new_value);
54+
}
55+
5356
$new_props = $this->getSetProps($new_value);
5457

5558
foreach ($new_props as $new_prop) {
@@ -66,6 +69,10 @@ public function setValue($new_value) {
6669
*/
6770
protected function getSetProps($object = null) {
6871
$object = $object ?: $this->value;
72+
if (gettype($object) !== 'object') {
73+
return [];
74+
}
75+
6976
return array_keys((array) $object);
7077
}
7178

@@ -74,6 +81,10 @@ protected function getSetProps($object = null) {
7481
* @return stdClass
7582
*/
7683
public function getValue() {
84+
if (gettype($this->value) !== 'object') {
85+
return parent::getValue();
86+
}
87+
7788
$value = clone($this->value);
7889
$set_props = $this->getSetProps();
7990

@@ -92,6 +103,12 @@ public function getValue() {
92103
* @return [Error]
93104
*/
94105
public function validate() {
106+
$value_type = gettype($this->value);
107+
if (gettype($this->value) !== 'object') {
108+
$encoded_value = json_encode($this->value);
109+
return [new Error("`$encoded_value` must be `object` not `$value_type`")];
110+
}
111+
95112
$errors = [];
96113

97114
// Gets properties.
@@ -132,6 +149,10 @@ public function validate() {
132149
* @return Element $this
133150
*/
134151
public function setProp($prop_key, $prop_value) {
152+
if (gettype($this->value) !== 'object') {
153+
return $this;
154+
}
155+
135156
Helpers::checkType('prop_key', 'string', $prop_key);
136157

137158
if ($prop_value === null) {
@@ -158,6 +179,10 @@ public function setProp($prop_key, $prop_value) {
158179
* @return mixed
159180
*/
160181
public function getProp($prop_key) {
182+
if (gettype($this->value) !== 'object') {
183+
return null;
184+
}
185+
161186
Helpers::checkType('prop_key', 'string', $prop_key);
162187
return isset($this->value->{$prop_key}) ? $this->value->{$prop_key} : null;
163188
}
@@ -168,6 +193,10 @@ public function getProp($prop_key) {
168193
* @return mixed
169194
*/
170195
public function getPropValue($prop_key) {
196+
if (gettype($this->value) !== 'object') {
197+
return null;
198+
}
199+
171200
Helpers::checkType('prop_key', 'string', $prop_key);
172201
return $this->_getPropValue(explode('.', $prop_key));
173202
}
@@ -184,6 +213,10 @@ private function _getPropValue(array $prop_key) {
184213
}
185214

186215
public function unsetProp($prop_key) {
216+
if (gettype($this->value) !== 'object') {
217+
return $this;
218+
}
219+
187220
Helpers::checkType('prop_key', 'string', $prop_key);
188221
unset($this->value->{$prop_key});
189222
return $this;

tests/assets/test.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"type": "http://activitystrea.ms/schema/1.0/badge"
3434
}
3535
},
36+
"context": [1, {"a": 1}, 0, null],
3637
"authority": {
3738
"name": "",
3839
"mbox": "mailto:quan@ll.com",

0 commit comments

Comments
 (0)