Le 04/11/2010 15:59, Bernhard Schussek a écrit : > 2010/11/4 Jordi Boggiano <[email protected]>: >> And what about using a new stdClass object, putting all the data on it, >> and validating that? How would the validator distinguish it from an >> Entity object? If all the properties are public I assume it doesn't try >> and use getters/setters? > > Sounds like a hack to me. Apart from that, the validator can't know > about the constraints in this case, because constraints are defined on > your class (not stdClass). Even if you propose to pass the desired > constraints manually, this does not work for cascaded constraints > (Author->Emails etc.), because the validator discovers the constraints > of these cascaded objects automatically by introspecting the class > name.
And what about allowing Validator to get the object's class name
directly as an argument ? So, with the form providing a "wrong"
argument, it would allow Validator to validate the stdClass as if it was
the Entity object, isn't it ?
Or, better, allow the class name to be a property, and if not set, fall
back to getClass($data). So the form framework would be able to set a
class name, using things such as (I didn't have a look to Form
framework's code) (not tested, and there are probably better ways of
doing it) :
{{{
// $data is the data got from the user
$data = array(
'name' => 'Mr. N',
'parents' => array(
array(
'name' => 'Mr. X',
'parents' => array(null, null)
),
array(
'name' => 'Mrs. Y',
'parents' => array(null, null)
)
)
);
validate(transformToStdClass($data));
function transformToStdClass(&$data) {
if (!is_array($data)) {
return $data;
}
// Is it a collection of objects ?
$isCollection = true;
foreach ($data as $k => $v) {
if (!is_int($k)) {
$isCollection = false;
break;
}
}
// If not collection, transform each future entity to stdClass,
// otherwise simply transform to stdClass
$ret = stdClass();
if ($isCollection) {
$ret->___class_name = 'Collection';
for ($data as $o) {
$ret->data[] = transformToStdClass($o);
}
} else {
$ret->___class_name = 'Person'; // How to determine this, I don't
// know, but I'm sure Form framework
// knows it to be able to bind data.
foreach ($data as $k => $v) {
$ret->$k = transformToStdClass($v);
}
}
return $ret;
}
}}}
So the example upper would become, after transformToStdClass :
{{{
$o->___class_name = 'Person';
$o->name = 'Mr. N';
$o->parents->___class_name = 'Collection';
$o->parents->data[0]->___class_name = 'Person';
$o->parents->data[0]->name = 'Mr. X';
$o->parents->data[0]->parents->___class_name = 'Collection';
$o->parents->data[0]->parents->data[0] = null;
$o->parents->data[0]->parents->data[1] = null;
$o->parents->data[1]->___class_name = 'Person';
$o->parents->data[1]->name = 'Mrs. Y';
$o->parents->data[1]->parents->___class_name = 'Collection';
$o->parents->data[1]->parents->data[0] = null;
$o->parents->data[1]->parents->data[1] = null;
}}}
What do you think about it ?
> Bernhard
> --
> Software Architect & Engineer
> Blog: http://webmozarts.com
> Twitter: http://twitter.com/webmozart
>
signature.asc
Description: OpenPGP digital signature
