Skip to content
Closed
Changes from all commits
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
18 changes: 15 additions & 3 deletions src/JsonSchema/Constraints/TypeConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,18 @@ protected function validateType($value, $type)
}

if ('object' === $type) {
return is_object($value);
return is_object($value) || (is_array($value) && (count($value) == 0 || $this->isAssociativeArray($value)));
//return ($this::CHECK_MODE_TYPE_CAST == $this->checkMode) ? is_array($value) : is_object($value);
}

if ('array' === $type) {
return is_array($value);
return is_array($value) && (count($value) == 0 || !$this->isAssociativeArray($value));
}

if ('string' === $type) {
return is_string($value);
}

if ('email' === $type) {
return is_string($value);
}
Expand All @@ -142,4 +142,16 @@ protected function validateType($value, $type)

throw new InvalidArgumentException((is_object($value) ? 'object' : $value) . ' is an invalid type for ' . $type);
}

/**
* Check if the provided array is associative or not
*
* @param array $arr
*
* @return bool
*/
private function isAssociativeArray($arr)
{
return (array_keys($arr) !== range(0, count($arr) - 1));
}
}