Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Coerce array to objects.
1. Use `property_exists` for object checking
2. Coercive type hinting `(object) $this->data` for array value in object schema type, and `(array) $this->data` for array schema type.
  • Loading branch information
vicary authored Feb 27, 2017
commit 40168fe9c74b9e8c3856e81cad043a02ba0c6e4b
12 changes: 10 additions & 2 deletions src/Jsv4/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,14 @@ private function checkTypes()
$this->data = FALSE;
return;
}
} else if ($type == "object") {
if (is_array($this->data)) {
$this->data = (object) $this->data;
return;
}
} else if ($type == "array") {
$this->data = (array) $this->data;
return;
}
}
}
Expand Down Expand Up @@ -274,7 +282,7 @@ private function checkObject()
}
if (isset($this->schema->required)) {
foreach ($this->schema->required as $index => $key) {
if (!array_key_exists($key, (array) $this->data)) {
if (!property_exists($this->data, $key)) {
if ($this->coerce && $this->createValueForProperty($key)) {
continue;
}
Expand All @@ -286,7 +294,7 @@ private function checkObject()
if (isset($this->schema->properties)) {
foreach ($this->schema->properties as $key => $subSchema) {
$checkedProperties[$key] = TRUE;
if (array_key_exists($key, (array) $this->data)) {
if (!property_exists($this->data, $key)) {
$subResult = $this->subResult($this->data->$key, $subSchema);
$this->includeSubResult($subResult, self::pointerJoin(array($key)), self::pointerJoin(array("properties", $key)));
}
Expand Down