-
Notifications
You must be signed in to change notification settings - Fork 348
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
Add support for type coercion #308
Changes from 6 commits
aa72e28
1e0942e
0eded5b
bfd0611
aa771b9
1ddebe7
b7afe7a
75a200b
ff07d71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,17 +121,103 @@ public function validateElement($element, $matches, $objectDefinition = null, Js | |
*/ | ||
public function validateDefinition($element, $objectDefinition = null, JsonPointer $path = null) | ||
{ | ||
$default = $this->getFactory()->createInstanceFor('undefined'); | ||
|
||
foreach ($objectDefinition as $i => $value) { | ||
$property = $this->getProperty($element, $i, $this->getFactory()->createInstanceFor('undefined')); | ||
$property = $this->getProperty($element, $i, $default); | ||
$definition = $this->getProperty($objectDefinition, $i); | ||
|
||
if($this->checkMode & Constraint::CHECK_MODE_TYPE_CAST){ | ||
if(!($property instanceof Constraint)) { | ||
$property = $this->coerce($property, $definition); | ||
|
||
if($this->checkMode & Constraint::CHECK_MODE_COERCE) { | ||
if (is_object($element)) { | ||
$element->{$i} = $property; | ||
} else { | ||
$element[$i] = $property; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (is_object($definition)) { | ||
// Undefined constraint will check for is_object() and quit if is not - so why pass it? | ||
$this->checkUndefined($property, $definition, $path, $i); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Converts a value to boolean. For example, "true" becomes true. | ||
* @param $value The value to convert to boolean | ||
* @return bool|mixed | ||
*/ | ||
protected function toBoolean($value) | ||
{ | ||
if($value === "true"){ | ||
return true; | ||
} | ||
|
||
if($value === "false"){ | ||
return false; | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* Converts a numeric string to a number. For example, "4" becomes 4. | ||
* | ||
* @param mixed $value The value to convert to a number. | ||
* @return int|float|mixed | ||
*/ | ||
protected function toNumber($value) | ||
{ | ||
if(is_numeric($value)) { | ||
return $value + 0; // cast to number | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
protected function toInteger($value) | ||
{ | ||
if(ctype_digit ($value)) { | ||
return $value + 0; // cast to number | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not cast instead of coerce here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain what it is you have in mind in a bit more detail, please? This is a cast (in the case that a string is input). The actual coercing happens (optionally) in lines 136 and 138 of this file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am referring to an explicit cast instead of an operation that results in an implicit cast: return (int) $value; For the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see no problem with that. Changed. |
||
} | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* Given a value and a definition, attempts to coerce the value into the | ||
* type specified by the definition's 'type' property. | ||
* | ||
* @param mixed $value Value to coerce. | ||
* @param \stdClass $definition A definition with information about the expected type. | ||
* @return bool|int|string | ||
*/ | ||
protected function coerce($value, $definition) | ||
{ | ||
$type = isset($definition->type)?$definition->type:null; | ||
if($type){ | ||
switch($type){ | ||
case "boolean": | ||
$value = $this->toBoolean($value); | ||
break; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dead space There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed |
||
case "integer": | ||
$value = $this->toInteger($value); | ||
break; | ||
case "number": | ||
$value = $this->toNumber($value); | ||
break; | ||
} | ||
} | ||
return $value; | ||
} | ||
|
||
/** | ||
* retrieves a property from an object or array | ||
* | ||
|
@@ -146,6 +232,7 @@ protected function getProperty($element, $property, $fallback = null) | |
if (is_array($element) /*$this->checkMode == self::CHECK_MODE_TYPE_CAST*/) { | ||
return array_key_exists($property, $element) ? $element[$property] : $fallback; | ||
} elseif (is_object($element)) { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dead space There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed |
||
return property_exists($element, $property) ? $element->$property : $fallback; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why hex literals?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a style thing I learned decades ago. I guess the idea is that this way it's a little more clear to the reader that we're creating a sequence for the purpose of bit-masking (and you don't have to get out a calculator to figure out what the higher numbers are, though it's not an issue in this simple case).
vs
But I could go either way if you feel strongly about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with it but I would caution that web developers, in my experience, are not usually accustomed to bitwise operations or thinking.
would go with a boolean literal if that is the clarity you intend to convey:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Went with binary literals.