1. Go to this page and download the library: Download dql/valueobjects 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/ */
dql / valueobjects example snippets
use EventSourced\ValueObject\ValueObject;
class Integer extends ValueObject\AbstractSingleValue
{
protected function validator()
{
return parent::validator()->intVal();
}
}
$integer = new Integer(1);
echo $integer->value();
use EventSourced\ValueObject\ValueObject\Type\AbstractSingleValue;
class Coordinate extends AbstractSingleValue
{
protected function validator()
{
return parent::validator()->floatVal()->between(-90, 90);
}
}
use EventSourced\ValueObject\ValueObject\Type\AbstractComposite;
class GPSCoordinates extends AbstractComposite
{
protected $latitude;
protected $longitude;
public function __construct(Coordinate $latitude, Coordinate $longitude)
{
$this->latitude = $latitude;
$this->longitude = $longitude;
}
public function latitude()
{
return $this->latitude;
}
public function longitude()
{
return $this->longitude;
}
}
namespace EventSourced\ValueObject\ValueObject;
class NullableGPSCoordinates extends GPSCoordinates
{
public function __construct(Coordinate $latitude=null, Coordinate $longitude=null)
{
$this->latitude = $latitude;
$this->longitude = $longitude;
}
}
use EventSourced\ValueObject\ValueObject\Type\AbstractCollection;
class IntegerCollection extends AbstractCollection
{
public function collection_of()
{
return Integer::class;
}
}
use EventSourced\ValueObject\ValueObject\Type\AbstractEntity;
class SampleEntity extends AbstractEntity
{
public $date;
//Uuid and Date are base types that comes with the library
public function __construct(Uuid $id, Date $date)
{
$this->date = $date;
parent::__construct($id);
}
}
$entity = new SampleEntity(new Uuid("153111a5-2d77-48b7-a88d-ee1d626c1d5d"), new Date('2013-10-12');
//Accessing the id property, part of the base class
echo $entity->id()->value();
use EventSourced\ValueObject\ValueObject\Type\AbstractIndex;
class SampleEntityIndex extends Type\AbstractIndex
{
public function collection_of()
{
return SampleEntity::class;
}
}
$index = new SampleEntityIndex([]);
$id = new Uuid("153111a5-2d77-48b7-a88d-ee1d626c1d5d");
$index = $index->add(new SampleEntity($id, new Date('2013-10-12')));
$index->count(); //1
$index->exists($id); //true
$index->get($id)->date()->value(); //'2013-10-12'
$index = $index->replace(new SampleEntity($id, new Date('2014-10-12'));
$index->get($id)->date()->value(); //'2014-10-12'
$index = $index->remove($id);
$index->exists($id); //false
$float_a = new Float(0.121);
$float_b = new Float(0.121);
$float_a->equals($float_b); //true
use EventSourced\ValueObject\Serializer\Serializer;
$float = new Float(0.121);
$serializer = new Serializer();
$serialized = $serializer->serialize($float);
use EventSourced\ValueObject\Serializer\Serializer;
use EventSourced\ValueObject\Deserializer\Deserializer;
$float = new Float(0.121);
$serializer = new Serializer();
$serialized = $serializer->serialize($float);
$deserializer = new Deserializer();
$float_again = $deserializer->deserialize(Float:class, $serialized);