1. Go to this page and download the library: Download samleybrize/valoa 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/ */
samleybrize / valoa example snippets
use Samleybrize\Valoa\ValueObject\ValueObjectTrait;
class EntityTest
{
use ValueObjectTrait;
/**
* @var int
*/
private $var1;
/**
* @var string
*/
private $var2;
}
$test = new EntityTest();
$test->var1 = 23;
$test->var2 = "message";
echo $test->var2;
use Samleybrize\Valoa\ValueObject\ValueObjectTrait;
class EntityTest
{
use ValueObjectTrait;
/**
* @var int
* @strict
*/
private $var1;
/**
* @var int
*/
private $var2;
}
$test = new EntityTest();
$test->var2 = "23text"; // will be converted to the integer "23"
$test->var1 = "23text"; // will raise an exception
use Samleybrize\Valoa\ValueObject\ValueObjectTrait;
class EntityTest
{
use ValueObjectTrait;
/**
* @var some text
* @validator string
*/
private $var1;
/**
* @var some text
* @validator any
*/
private $var2;
}
$test = new EntityTest();
$test->var1 = "text"; // validate as string
$test->var2 = "whatever you want"; // will not be validated, any value may be setted
use Samleybrize\Valoa\ValueObject\ValueObjectTrait;
class EntityTest
{
use ValueObjectTrait;
/**
* @var string
* @immutable
*/
private $var1;
}
$test = new EntityTest();
$test->var1 = "text"; // will raise an exception
/**
* @immutable
*/
class EntityTest
{
use ValueObjectTrait;
/**
* @var string
*/
private $var1;
/**
* @var boolean
*/
private $var2;
}
$test = new EntityTest();
$test->var1 = "text"; // will raise an exception
$test->var2 = true; // will raise an exception
class EntityTest
{
use ValueObjectTrait;
/**
* @var string
*/
private $var1;
/**
* @var string
* @nullable
*/
private $var2;
}
$test = new EntityTest();
$test->var1 = "text"; // ok
$test->var1 = null; // will raise an exception
$test->var2 = "text"; // ok
$test->var2 = null; // ok
/**
* Array of string
* @var string[]
*/
private $var1;
/**
* Array of DateTime objects
* @var \DateTime[]
*/
private $var2;
/**
* Array of array of string
* @var string[][]
*/
private $var1;
/**
* This var accept integer 1, integer 5 and string "text".
* @var ...
* @validator enum
* @enum [1, 5, "text"]
*/
private $var;
class Test
{
const TEST_NAME1 = 1;
const TEST_NAME2_P = 2;
const X_AZERTY3 = 3;
const X_AZERTY4_P = 3;
/**
* Allow all class constants of the class 'Test'
* @var int
* @validator ClassConstants
* @classname Test
*/
private $var1;
/**
* Allow TEST_NAME1 and TEST_NAME2_P
* @var int
* @validator ClassConstants
* @classname Test2
* @beginWith TEST_
*/
private $var1;
/**
* Allow TEST_NAME2_P and X_NAME4_P
* @var int
* @validator ClassConstants
* @classname Test2
* @endWith _P
*/
private $var1;
/**
* Allow TEST_NAME1 and TEST_NAME2_P
* @var int
* @validator ClassConstants
* @classname Test2
* @contain NAME
*/
private $var1;
}
namespace Your\Namespace;
use Samleybrize\Valoa\ValueObject\Validator\ValidatorInterface;
class CustomValidator implements ValidatorInterface
{
// $tags contains all doc comment tags
public function __construct(array $tags = array())
{
// ...
}
public function isValid(&$value)
{
// ...
}
}
use Samleybrize\Valoa\ValueObject\ValueObjectTrait;
class EntityTest
{
use ValueObjectTrait;
/**
* @var string
* @validator \Your\Namespace\CustomValidator
*/
private $var1;
}
use Samleybrize\Valoa\ValueObject\ValueObjectLazyLoaderInterface;
use Samleybrize\Valoa\ValueObject\ValueObjectTrait;
class Test
{
/**
* @var string
*/
private $var;
}
class LazyLoaderString implements ValueObjectLazyLoaderInterface
{
public function load()
{
// retrieve the value from a database for example
return "text";
}
}
class LazyLoaderInteger implements ValueObjectLazyLoaderInterface
{
public function load()
{
// retrieve the value from a database for example
return 12;
}
}
$test = new EntityTest();
$test->var = new LazyLoaderString();
echo $test->var; // outputs "text"
$test->var = new LazyLoaderInteger();
echo $test->var; // raise an exception
// will not work
$object->array[] = 8;
// workaround 1
$array = $object->array;
$array[] = 8;
$object->array = $array;
// workaround 2
$object->addToArray(8);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.