keep-update
, (*1)
Keep update resolve the problem of remaps data from jsonSerialize, (*2)
Example, (*3)
I have this dto representation of my model, (*4)
class News implements \JsonSerializable
{
/**
* @var string
*/
private $title;
/**
* @var Author
*/
private $author;
// setters, getters
public function jsonSerialize()
{
return array(
'title' => $this->title,
'author' => $this->author
);
}
}
class Author implements \JsonSerializable
{
private $name;
// setters, getters
public function jsonSerialize()
{
return array(
'name' => $this->name
);
}
}
If i do, (*5)
$author = new Author();
$author->setName('Stéphane');
$news = new News();
$news->setTitle('My very news');
$news->setAuthor($author);
file_put_content('tmp', json_encode($news));
I will have a json representation in tmp file. Now i want to wake the representation., (*6)
$newsArray = json_decode(file_get_content('tmp'));
How to be sure that's the array i have is a correct representation of my dto's ? You have to create a validator class, that's very annoying..., (*7)
With keepUpdate you only have to add Annotation in your attribute, resuming the previous sample, (*8)
use KeepUpdate\Annotation;
class News implements \JsonSerializable
{
/**
* @var string
*/
private $title;
/**
* @Annotation\Chain(class="Author")
* @var Author
*/
private $author;
// setters, getters
public function jsonSerialize()
{
return array(
'title' => $this->title,
'author' => $this->author
);
}
}
class Author implements \JsonSerializable
{
private $name;
// setters, getters
public function jsonSerialize()
{
return array(
'name' => $this->name
);
}
}
$author = new Author();
$author->setName('Stéphane');
$news = new News();
$news->setTitle('My very news');
$news->setAuthor($author);
file_put_content('tmp', json_encode($news));
$newsArray = json_decode(file_get_content('tmp'));
$arrayValidatorFactory = \KeepUpdate\ArrayValidatorFactory::getInstance();
try {
$arrayValidatorFactory->isValid($news, $newsArray);
} catch(\KeepUpdate\ValidationException $e) {
echo 'Not valid !';
}
// or
try {
$arrayValidatorFactory->isValid('News', $newsArray);
} catch(\KeepUpdate\ValidationException $e) {
echo 'Not valid !';
}
Validate an array representation of a dto is now more simple, (*9)