From 8edc76d374a2b9d56205a1900d963ab1f385f197 Mon Sep 17 00:00:00 2001 From: Alessandro Pellizzari Date: Thu, 19 May 2016 12:46:00 +0100 Subject: [PATCH 1/3] TypeConstraint checks if a value is really a numeric array or it's an object --- src/JsonSchema/Constraints/TypeConstraint.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/JsonSchema/Constraints/TypeConstraint.php b/src/JsonSchema/Constraints/TypeConstraint.php index 837cfd8b..6eebb205 100644 --- a/src/JsonSchema/Constraints/TypeConstraint.php +++ b/src/JsonSchema/Constraints/TypeConstraint.php @@ -116,18 +116,18 @@ protected function validateType($value, $type) } if ('object' === $type) { - return is_object($value); + return is_object($value) || (is_array($value) && $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) && !$this->isAssociativeArray($value); } if ('string' === $type) { return is_string($value); } - + if ('email' === $type) { return is_string($value); } @@ -142,4 +142,16 @@ protected function validateType($value, $type) throw new InvalidArgumentException((is_object($value) ? 'object' : $value) . ' is an invalid type for ' . $type); } + + /** + * Check is the provided array is associative or not + * + * @param $arr + * + * @return bool + */ + private function isAssociativeArray($arr) + { + return (array_keys($arr) !== range(0, count($arr) - 1)); + } } From 954a01f8733040653d668071b6f9756d3399bfe8 Mon Sep 17 00:00:00 2001 From: Alessandro Pellizzari Date: Thu, 19 May 2016 13:53:30 +0100 Subject: [PATCH 2/3] Fixed typo --- src/JsonSchema/Constraints/TypeConstraint.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/JsonSchema/Constraints/TypeConstraint.php b/src/JsonSchema/Constraints/TypeConstraint.php index 6eebb205..fdfdc4db 100644 --- a/src/JsonSchema/Constraints/TypeConstraint.php +++ b/src/JsonSchema/Constraints/TypeConstraint.php @@ -144,9 +144,9 @@ protected function validateType($value, $type) } /** - * Check is the provided array is associative or not + * Check if the provided array is associative or not * - * @param $arr + * @param array $arr * * @return bool */ From 9220db10b1c796cd46226efd26c9f7e112b1cbd9 Mon Sep 17 00:00:00 2001 From: Alessandro Pellizzari Date: Wed, 1 Jun 2016 17:49:15 +0100 Subject: [PATCH 3/3] PHP empty arrays can be both Javascript arrays and objects. --- src/JsonSchema/Constraints/TypeConstraint.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/JsonSchema/Constraints/TypeConstraint.php b/src/JsonSchema/Constraints/TypeConstraint.php index fdfdc4db..a5154d2b 100644 --- a/src/JsonSchema/Constraints/TypeConstraint.php +++ b/src/JsonSchema/Constraints/TypeConstraint.php @@ -116,12 +116,12 @@ protected function validateType($value, $type) } if ('object' === $type) { - return is_object($value) || (is_array($value) && $this->isAssociativeArray($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) && !$this->isAssociativeArray($value); + return is_array($value) && (count($value) == 0 || !$this->isAssociativeArray($value)); } if ('string' === $type) {