-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
aa72e28
add support for type coercion
shmax 1e0942e
add tests
shmax 0eded5b
move coerce tests out of base
shmax bfd0611
use flags for mode
shmax aa771b9
update readme
shmax 1ddebe7
fix tests
shmax b7afe7a
remove ws
shmax 75a200b
use binary literals, explicit cast
shmax ff07d71
back to hex
shmax File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,17 +121,83 @@ 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_COERCE){ | ||
if(!($property instanceof Constraint)) { | ||
$element->{$i} = $property = $this->coerce($property, $definition); | ||
} | ||
} | ||
|
||
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; | ||
} | ||
|
||
/** | ||
* 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; | ||
|
||
case "number": | ||
$value = $this->toNumber($value); | ||
break; | ||
} | ||
} | ||
return $value; | ||
} | ||
|
||
/** | ||
* retrieves a property from an object or array | ||
* | ||
|
@@ -146,6 +212,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; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Dead space
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.
removed