PHP code example of stopsopa / jms-serializer-lite
1. Go to this page and download the library: Download stopsopa/jms-serializer-lite library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
stopsopa / jms-serializer-lite example snippets
namespace MyProject;
use Stopsopa\LiteSerializer\Dumper;
class NewDumper extends Dumper
{
}
class NewDumper extends Dumper
{
public function dumpMyProject_Article($entity) {
$data = $this->toArray($entity, array(
'id' => 'id',
'name' => array('title', null),
# if path 'title' is wrong then
# return null instead of throw AbstractEntityException
'body' => 'content',
'comments' => 'comments'
));
if (!$data['name']) {
$data['name'] = 'default value if here is something false';
}
return $data;
}
}
use Stopsopa\LiteSerializer\Exceptions\DumperContinueException;
class NewDumper extends Dumper
{
public function dumpMyProject_Comment($entity) {
if (!$entity->isModerated()) {
throw new DumperContinueException();
}
return $this->toArray($entity, array(
'id' => 'id',
'body' => 'content'
));
}
}
namespace MyProject;
use Stopsopa\LiteSerializer\Dumper;
use Stopsopa\LiteSerializer\Exceptions\DumperContinueException;
class Group {
protected $id;
protected $name;
public static function getInstance() { return new self(); }
public function getId() { return $this->id; }
public function setId($id) { $this->id = $id; return $this; }
public function getName() { return $this->name; }
public function setName($name) { $this->name = $name; return $this; }
}
# extend just to make this example shorter
# the case is that we can build one-to-many relation using this classes
class User extends Group {
protected $groups = array();
public function getGroups() { return $this->groups; }
public function setGroups($groups) { $this->groups = $groups; return $this; }
}
$user = User::getInstance()->setId(50)->setName('user')->setGroups(array(
'group-1' => Group::getInstance()->setId(10)->setName('gr 1'),
'group-2' => Group::getInstance()->setId(11)->setName('gr 2'),
'group-3' => Group::getInstance()->setId(12)->setName('gr 3'),
));
class NewDumper extends Dumper
{
public function dumpMyProject_User($entity) {
return $this->toArray($entity, array(
'id' => 'id',
'name' => 'name',
'groups' => 'groups'
));
}
public function dumpMyProject_Group($entity) {
if ($entity->getName() === 'gr 2') {
throw new DumperContinueException();
}
return $this->toArray($entity, array(
'id' => 'id',
'name' => 'name'
));
}
}
echo json_encode(NewDumper::getInstance()->dump($user), JSON_PRETTY_PRINT);
...
public function dumpMyProject_User($entity) {
$map = array(
'id' => 'id',
'name' => 'name'
);
if (count($this->stack) < 1) {
# dump groups of user only if this is feed where
# user is on higher level of hierarchy
$map['groups'] = 'groups';
}
# stack is array and contain more information about where we
# are in execution stack, inspect this later if You need.
return $this->toArray($entity, $map);
}
...
class NewDumper extends Dumper
{
...
public function dumpMyProject_User($entity) {
$map = array(
'id' => 'id',
'name' => 'name'
);
if ($this->scope === 'dumpalsogroups') {
$map['groups'] = 'groups';
}
return $this->toArray($entity, $map);
}
}
NewDumper::getInstance()->dumpScope($user, 'dumpalsogroups');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.