PHP code example of touki / populator

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

    

touki / populator example snippets




use Touki\Populator\Populator;

$populator = new Populator;




$loader = mmon\Annotations\AnnotationRegistry;

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));




use Touki\Populator\Populator;
use Touki\Populator\Hydrator;
use Touki\Populator\HydratorContextFactory;
use Doctrine\Common\Annotations\AnnotationReader;

/**
 * You can instanciate your own as long as it implements HydratorInterface
 */
$hydrator = new Hydrator;

/**
 * You can instanciate your own as long as it implements HydratorContextFactoryInterface
 * The default one accepts Any Doctrine's Annotation Reader (Like FileCacheReader)
 *
 * @see https://github.com/doctrine/annotations/tree/master/lib/Doctrine/Common/Annotations
 */
$factory = new HydratorContextFactory(new AnnotationReader);

$populator = new Populator($hydrator, $factory);




namespace Acme\Model\Foo;

class Foo
{
    protected $bar;
    public $public;
    public $publicWithSetter;

    public function setBar($bar)
    {
        $this->bar = $bar;
    }

    public function getBar()
    {
        return $this->bar;
    }

    public function setPublicWithSetter($var)
    {
        $this->publicWithSetter = $var;
    }
}

$data = array(
    'bar' => 'Foobaz!',
    'public' => 'Public!'
    'publicWithSetter' => 'BySetter'
);

/**
 * You can give either classname or an instance
 */
$foo = new Acme\Model\Foo;
$foo = 'Acme\Model\Foo';

$newFoo = $populator->populate($data, $foo);

echo $newFoo->getBar();         // Foobaz!
echo $newFoo->public;           // Public!
echo $newFoo->publicWithSetter; // BySetter




use Touki\Populator\Annotation as Populator;

class Foo
{
    /**
     * @Populator\Ignore
     */
    protected $bar;
}

$data = array(
    'bar' => 'Foobaz!'
);

$newFoo = $populator->populate($data, $foo);

echo $newFoo->getBar(); // NULL




use Touki\Populator\Annotation as Populator;

class Foo
{
    /**
     * @Populator\Setter("mySetter")
     */
    protected $bar;

    public function mySetter($value)
    {
        $this->bar = $value;
    }
}

$data = array(
    'bar' => 'Foobaz!'
);

$newFoo = $populator->populate($data, $foo);

echo $newFoo->getBar(); // Foobaz!




use Touki\Populator\Annotation as Populator;

class Foo
{
    /**
     * @Populator\Alias("bar")
     * @Populator\Alias("another")
     */
    protected $foo;

    public function setFoo($value)
    {
        $this->foo = $value;
    }
}

$data = array(
    'bar' => 'Foobaz!'
);

$newFoo = $populator->populate($data, $foo);

echo $newFoo->getFoo(); // Foobaz!

$data = array(
    'another' => 'Foobaz!'
);

$newFoo = $populator->populate($data, $foo);

echo $newFoo->getFoo(); // Foobaz!




use Touki\Populator\Annotation as Populator;

class Foo
{
    /**
     * @Populator\Aliases({"bar", "another"})
     */
    protected $foo;

    public function setFoo($value)
    {
        $this->foo = $value;
    }
}

$data = array(
    'bar' => 'Foobaz!'
);

$newFoo = $populator->populate($data, $foo);

echo $newFoo->getFoo(); // Foobaz!




use Touki\Populator\Annotation as Populator;

class Foo
{
    /**
     * @Populator\Deep("Bar")
     */
    protected $bar;

    public function setBar(Bar $bar)
    {
        $this->bar = $bar;
    }
}

class Bar
{
    protected $baz;

    public function setBaz($bar)
    {
        $this->baz = $baz;
    }
}

$data = array(
    'bar' => array(
        'baz' => 'DeepBaz!'
    )
);

$newFoo = $populator->populate($data, $foo);

echo $newFoo->getBar()->getBaz(); // DeepBaz!