PHP code example of formapro / yadm

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

    

formapro / yadm example snippets




namespace Acme;

use function Formapro\Values\set_value;
use function Formapro\Values\get_value;

class Price
{
    private $values = [];
    
    public function setCurrency(string $value): void
    {
        set_value($this, 'currency', $value);
    }
    
    public function getCurrency(): string 
    {
        return get_value($this, 'currency');
    }
    
    public function setAmount(int $value): void
    {
        set_value($this, 'amount', $value);
    }
    
    public function getAmount(): string 
    {
        return get_value($this, 'amount');
    }
}


namespace Acme;

use function Formapro\Values\set_value;
use function Formapro\Values\get_value;
use function Formapro\Values\set_object;
use function Formapro\Values\get_object;

class Order
{
    private $values = [];
    
    public function setNumber(string $number): void
    {
        set_value($this, 'number', $number);
    }
    
    public function getNumber(): string 
    {
        return get_value($this, 'number');
    }
    
    public function setPrice(Price $price): void
    {
        set_object($this, 'price', $price);
    }
    
    public function getPrice(): Price
    {
        return get_object($this, 'price', Price::class);
    }
}


namespace Acme;

use MongoDB\Client;
use Formapro\Yadm\Hydrator;
use Formapro\Yadm\Storage;

$collection = (new Client())->selectCollection('acme_demo', 'orders');
$hydrator = new Hydrator(Order::class);
$storage = new Storage($collection, $hydrator);

$price = new Price();
$price->setAmount(123); # 1.23 USD
$price->setCurrency('USD');

$order = new Order();
$order->setNumber(1234);
$order->setPrice($price);

$storage->insert($order);

$foundOrder = $storage->find(['_id' => get_object_id($order)]);
$foundOrder->setNumber(4321);
$storage->update($foundOrder);

$storage->delete($foundOrder);


namespace Acme;

use MongoDB\Client;
use Formapro\Yadm\Hydrator;
use Formapro\Yadm\Storage;
use Formapro\Yadm\ConvertValues;
use Formapro\Yadm\Type\UuidType;
use Formapro\Yadm\Type\UTCDatetimeType;
use Formapro\Yadm\Uuid;
use function Formapro\Values\set_value;
use function Formapro\Values\get_value;

$convertValues = new ConvertValues([
    'id' => new UuidType(),
    'createdAt' => new UTCDatetimeType(),
]);

$collection = (new Client())->selectCollection('acme_demo', 'orders');
$hydrator = new Hydrator(Order::class);
$storage = new Storage($collection, $hydrator, null, null, $convertValues);
 

$order = new Order();
set_value($order, 'id', Uuid::generate()->toString());
set_value($order, 'createdAt', (new \DateTime())->format('U'));

$storage->insert($order);

$id = get_value($order, 'id');

// find by uuid
$anotherOrder = $storage->findOne(['id' => new Uuid($id)]);

// do not update id if not changed
$storage->update($anotherOrder);

// update on change
set_value($anotherOrder, 'id', Uuid::generate()->toString());
$storage->update($anotherOrder);
bash
$ composer