Skip to content

TypeConstraint checks if a value is really a numeric array or it's an object #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
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));
}
}