Skip to content

Conversation

@shmax
Copy link
Collaborator

@shmax shmax commented Oct 30, 2016

Adds support for coercion of items for schemas with a type of array:

$value = (object)[
    "foo"=>["7","9.1","true"]
];
$schema = (object)[
    "type"=>"object",
    "properties"=>(object)[
        "foo"=>(object)[
            "type"=>"array",
            "additionalItems"=>[
                "type"=>"boolean"
            ],
            "items"=>[
                (object)[
                    "type"=>"integer"
                ],
                (object)[
                    "type"=>"number"
                ]
            ]
        ]
    ]
];

echo gettype($value->foo[0]); // "string"
echo gettype($value->foo[1]); // "string"
echo gettype($value->foo[2]); // "string"

$validator->check($value, $schema);

echo gettype($value->foo[0]); // "integer"
echo gettype($value->foo[1]); // "double"
echo gettype($value->foo[2]); // "boolean"
  • moved the coercion helpers out of ObjectConstraint and down into Constraint, because there are non-objects that need to coerce
  • Got rid of CollectionConstraint. Because we need to pass arrays by reference to coerce them, and because CollectionConstraint was concerned exclusively with arrays of things, its check method was rendered useless. My choices seemed to be to factor it into the base Constraint class (which already had a checkArray method) or change all checkimplementations to take a reference; I chose the former because the latter would mean breaking backwards compatibility (only variables can be passed by reference, so any inline array literals being used out there in the wild would cause a run-time exception). Hope that's not a problem.

@bighappyface @digitalkaoz

@shmax
Copy link
Collaborator Author

shmax commented Nov 2, 2016

Would love some feedback, guys

@shmax
Copy link
Collaborator Author

shmax commented Nov 10, 2016

@bighappyface Is there any chance we can get this merged before #331 ? I'm not looking forward to resolving those conflicts...

@bighappyface
Copy link
Collaborator

@shmax I can work with you on that. Is this PR rebased on the latest?

@shmax
Copy link
Collaborator Author

shmax commented Nov 10, 2016

@bighappyface Yep, good to go. No conflicts with the base branch.

@shmax
Copy link
Collaborator Author

shmax commented Nov 26, 2016

I'm working on a complete redesign of the coercion functionality that makes this PR unnecessary.

@shmax shmax closed this Nov 26, 2016
@shmax shmax mentioned this pull request Nov 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants