PHP code example of kingson-de / marshal-serializer

1. Go to this page and download the library: Download kingson-de/marshal-serializer 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/ */

    

kingson-de / marshal-serializer example snippets




use KingsonDe\Marshal\AbstractMapper;

class UserMapper extends AbstractMapper {

    public function map(User $user) {
        return [
            'username'  => $user->getUsername(),
            'email'     => $user->getEmail(),
            'birthday'  => $user->getBirthday()->format('Y-m-d'),
            'followers' => count($user->getFollowers()),
        ];
    }
}



use KingsonDe\Marshal\Data\Item;

$item = new Item(new UserMapper(), $user);



use KingsonDe\Marshal\Data\Collection;

$userCollection = [$user1, $user2, $user3];
$item           = new Collection(new UserMapper(), $userCollection);



use KingsonDe\Marshal\Marshal;

$data = Marshal::serialize($item);



use KingsonDe\Marshal\Marshal;

$data = Marshal::serializeItem($mapper, $model);
// or
$data = Marshal::serializeCollection($mapper, $modelCollection);
// or 
$data = Marshal::serializeCollectionCallable(function (User $user) {
    return [
        'username'  => $user->getUsername(),
        'email'     => $user->getEmail(),
        'birthday'  => $user->getBirthday()->format('Y-m-d'),
        'followers' => count($user->getFollowers()),
    ];
}, $modelCollection);



use KingsonDe\Marshal\Data\Item;
use KingsonDe\Marshal\Marshal;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;

class UserController extends Controller {

    public function indexAction(User $user) {
        $item = new Item(new UserMapper(), $user);
        $data = Marshal::serialize($item);
        
        return new JsonResponse($data);
    }
}



use KingsonDe\Marshal\AbstractMapper;

class UserMapper extends AbstractMapper {

    public function map(User $user) {
        return [
            'username'  => $user->getUsername(),
            'email'     => $user->getEmail(),
            'birthday'  => $user->getBirthday()->format('Y-m-d'),
            'followers' => $this->collection(new FollowerMapper(), $user->getFollowers),
            'location'  => $this->item(new LocationMapper(), $user->getLocation()),
        ];
    }
}



use KingsonDe\Marshal\Data\Item;
use KingsonDe\Marshal\Marshal;

$item = new Item(new UserMapper(), $user, $followers, $location);
$data = Marshal::serialize($item);



use KingsonDe\Marshal\AbstractMapper;

class UserMapper extends AbstractMapper {

    public function map(User $user, FollowerCollection $followers, Location $location) {
        return [
            'username'  => $user->getUsername(),
            'email'     => $user->getEmail(),
            'birthday'  => $user->getBirthday()->format('Y-m-d'),
            'followers' => $this->collection(new FollowerMapper(), $followers),
            'location'  => $this->item(new LocationMapper(), $location),
        ];
    }
}



use KingsonDe\Marshal\AbstractMapper;

class UserMapper extends AbstractMapper {

    public function map(User $user) {
        if ($user->isPrivate()) {
            return null;
        }
    
        return [
            'username' => $user->getUsername(),
        ];
    }
}



use KingsonDe\Marshal\AbstractObjectMapper;
use KingsonDe\Marshal\Data\FlexibleData;
use KingsonDe\Marshal\Example\Model\User;

class UserObjectMapper extends AbstractObjectMapper {

    /**
     * @inheritdoc
     *
     * @return User
     */
    public function map(FlexibleData $flexibleData, ...$additionalData) {
        return new User(
            $flexibleData['id'] ?? 0,
            $flexibleData['email'] ?? '',
            $flexibleData->find('username', '')
        );
    }
}



use KingsonDe\Marshal\Marshal;

$data = Marshal::serializeItem(new UserMapper(), $user);

$user = Marshal::deserialize(new UserObjectMapper(), $data);



use KingsonDe\Marshal\Marshal;

$data = Marshal::serializeItem(new UserMapper(), $user);

$user = Marshal::deserializeCallable(function (FlexibleData $flexibleData) {
    return new User(
        $flexibleData->get('id'),
        $flexibleData->get('email'),
        $flexibleData->get('username')
    );
}, $data);