PHP code example of happyr / serializer-bundle

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

    

happyr / serializer-bundle example snippets



use \Happyr\SerializerBundle\Annotation as Serializer;

/**
 * @Serializer\ExclusionPolicy("all")
 */
class Car
{
    /**
     * @Serializer\Expose
     */
    private $size = 'Small';
    
    /**
     * @Serializer\Expose
     * @Serializer\Accessor({"getter":"getModel"})
     */
    private $model = 'Volvo';

    private $color = 'Red';
    
    public function getModel()
    {
        return 'This is model: '.$this->model;
    }
}

class Owner
{
    private $name;

    /**
     * @Serializer\Type("Car")
     */
    private $car;

    /**
     * @Serializer\ReadOnly
     */
    private $birthday;

    public function __construct()
    {
        $this->name = 'Tobias';
        $this->car = new Car(true);
        $this->birthday = new \DateTime('1989-04-30');
    }
}

$json = $this->container->get('serializer')->serialize(new Owner(), 'json');
var_dump($json);



use \Happyr\SerializerBundle\Annotation as Serializer;

class User
{
    private $id;

    /** 
     * @Serializer\Accessor(getter="getTrimmedName",setter="setName") 
     */
    private $name;

    // ...
    public function getTrimmedName()
    {
        return trim($this->name);
    }

    public function setName($name)
    {
        $this->name = $name;
    }
}
 php

// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Happyr\SerializerBundle\HappyrSerializerBundle(),
    );
}