PHP code example of kenny1911 / populate

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

    

kenny1911 / populate example snippets


use Kenny1911\Populate\PopulateBuilder;

$populate = PopulateBuilder::create()->build(); // Create new instance

class Src
{
    public $foo;
    public $bar;
    public $baz;
}

class Dest
{
    public $foo;
    public $bar;
    public $baz;
}

$src = new Src();
$src->foo = 'Foo';

$dest = new Dest();

$populate->populate(
    $src,               // Source object
    $dest,              // Destination object
    ['foo', 'bar'],     // Only properties `foo` and `bar` will be populated
    ['bar'],            // Property `bar` won't bw populated
    ['foo' => 'bar']    // Value of $src->foo will be set to $dest->bar
);

// $dest->bar === 'Foo';

use Kenny1911\Populate\PopulateBuilder;

$settings = [
    [
        'src' => 'Src',                 // Required
        'dest' => 'Dest',               // Required
        'properties' => ['foo', 'bar'], // Optional
        'ignore_properties' => ['bar'], // Optional
        'mapping' => ['foo' => 'bar']   // Optional
    ]
];

$populate = PopulateBuilder::create()->setSettings($settings)->build();

class Src
{
    public $foo;
    public $bar;
    public $baz;
}

class Dest
{
    public $foo;
    public $bar;
    public $baz;
}

$src = new Src();
$src->foo = 'Foo';

$dest = new Dest();

$populate->populate($src, $dest);

// $dest->bar === 'Foo';

    return [
        // ...
        Kenny1911\Populate\Bridge\Symfony\PopulateBundle::class => ['all' => true]
        // ...
    ];
    

use Kenny1911\Populate\PopulateInterface;

class Service
{
    /** @var PopulateInterface */
    private $populate;

    public function __construct(PopulateInterface $populate)
    {
        $this->populate = $populate;
    }

    public function action($src, $dest)
    {
        $this->populate->populate($src, $dest);
    }
}

use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;

class Service implements ContainerAwareInterface
{
    use ContainerAwareTrait;

    public function action($src, $dest)
    {
        $populate = $this->container->get('populate');

        $populate->populate($src, $dest);
    }
}