Skip to content

Commit

Permalink
Use TypeCheck interface for object manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
erayd committed Jan 18, 2017
1 parent 1cab31d commit 9bbf292
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/JsonSchema/Constraints/TypeCheck/LooseTypeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public static function propertyGet($value, $property)
return $value[$property];
}

public static function propertySet($value, $property, $data)
{
if (is_object($value)) {
$value->{$property} = $data;
}

$value[$property] = $data;
}


public static function propertyExists($value, $property)
{
if (is_object($value)) {
Expand Down
5 changes: 5 additions & 0 deletions src/JsonSchema/Constraints/TypeCheck/StrictTypeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public static function propertyGet($value, $property)
return $value->{$property};
}

public static function propertySet($value, $property, $data)
{
$value->{$property} = $data;
}

public static function propertyExists($value, $property)
{
return property_exists($value, $property);
Expand Down
2 changes: 2 additions & 0 deletions src/JsonSchema/Constraints/TypeCheck/TypeCheckInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public static function isArray($value);

public static function propertyGet($value, $property);

public static function propertySet($value, $property, $data);

public static function propertyExists($value, $property);

public static function propertyCount($value);
Expand Down
6 changes: 3 additions & 3 deletions src/JsonSchema/Constraints/UndefinedConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ protected function validateCommonProperties(&$value, $schema = null, JsonPointer
if ($coerce && $this->factory->getCheckMode() & self::CHECK_MODE_APPLY_DEFAULTS) {
if ($this->getTypeCheck()->isObject($value) && isset($schema->properties)) {
foreach ($schema->properties as $i => $propertyDefinition) {
if (!isset($value->$i) && isset($propertyDefinition->default)) {
$value->$i = $propertyDefinition->default;
if (!$this->getTypeCheck()->propertyExists($value, $i) && isset($propertyDefinition->default)) {
$this->getTypeCheck()->propertySet($value, $i, $propertyDefinition->default);
}
}
} elseif (is_array($value)) {
} elseif ($this->getTypeCheck()->isArray($value)) {
if (isset($schema->properties)) {
foreach ($schema->properties as $i => $propertyDefinition) {
if (!isset($value[$i]) && isset($propertyDefinition->default)) {
Expand Down

0 comments on commit 9bbf292

Please sign in to comment.