1. Go to this page and download the library: Download saschati/yii2-value-object 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/ */
saschati / yii2-value-object example snippets
use Saschati\ValueObject\Behaviors\ORMBehavior;
use Saschati\ValueObject\Types\ValueObjects\EmailType;
use Saschati\ValueObject\Types\ValueObjects\UuidType;
use Saschati\ValueObject\Types\Flats\BooleanType;
use Saschati\ValueObject\Types\Flats\TimestampType;
use Saschati\ValueObject\Helpers\TypeScope;
...
class User extends ActiveRecord
{
private Name $name;
...
public function behaviors(): array
{
return [
'vo' => [
'class' => ORMBehavior::class,
'attributes' => [
'id' => UuidType::class,
'email' => EmailType::class,
'active' => 'boolean', // BooleanType::class
'created_at' => 'timestamp', // TimestampType::class,
'name' => [
'scope' => TypeScope::EMBEDDED,
'type' => Name::class,
'map' => [
'firstname' => 'firstname',
'lastname' => 'lastname',
'middlename' => 'middlename',
],
],
],
],
];
}
...
}
use Saschati\ValueObject\Helpers\TypeScope;
use Saschati\ValueObject\Behaviors\ORMBehavior;
...
'vo' => [
'class' => ORMBehavior::class,
'attributes' => [
// Step: 1
// Cast the attribute in the Value Object
'@id' => Id::class,
// Step: 2
// Create a virtual property into which we transfer other properties.
'#build' => [
'scope' => TypeScope::EMBEDDED,
'type' => TypeOne::class,
'map' => [
'property1' => '@attribute1',
'property2' => '@attribute2',
],
],
// Step: 3
// We create a class property in which the object with our virtual
// property and the act record attribute will be embedded
'propertyInClass' => [
'scope' => TypeScope::EMBEDDED,
'type' => TypeTwo::class,
'map' => [
// #build the virtual property from our step 2
'property1' => '#build',
'property2' => '@attribute3',
],
],
// Step: 4
'mapper' => [
'scope' => TypeScope::MAPPER,
'map' => [
// We map the value from the attribute to the real property of the class,
// now we can safely interact with this property, and the library itself
// will transfer the value to the attribute when saved
'id' => '@id',
],
],
],
],
use Saschati\ValueObject\Behaviors\ORMBehavior;
use Saschati\ValueObject\Types\Flats\JsonType;
use Saschati\ValueObject\Helpers\TypeScope;
class User extends ActiveRecord
{
/**
* @var Contact
*/
private Contact $contact;
/**
* @var string
*/
private string $region;
...
public function behaviors(): array
{
return [
'vo' => [
'class' => ORMBehavior::class,
'attributes' => [
// STEP: 1
// We convert the contact attribute into an array
'@contact' => JsonType::class,
'#address' => [
'scope' => TypeScope::EMBEDDED,
'type' => Address::class,
'map' => [
// STEP: 2
// Assign the nested address from the contact attribute
// to the properties of the Address class
'country' => '@contact.address.country',
'region' => '@contact.address.region',
'city' => '@contact.address.city',
'street' => '@contact.address.street',
'house' => '@contact.address.house',
]
],
'contact' => [
'scope' => TypeScope::EMBEDDED,
'type' => Contact::class,
'map' => [
// STEP: 3
// We put other keys in properties of the Contact class
'phone' => '@contact.phone',
'email' => '@contact.email',
'address' => '#address',
]
]
'mapper' => [
'scope' => TypeScope::MAPPER,
'map' => [
// STEP: 4
// We take the property from the build and assign it to the class property.
// ===================
// It is better not to do this:)
// Try to change the properties through one VO, and not spread it throughout the model
'region' => '#address.region',
],
],
],
],
];
}
...
}
class Address
{
/**
* @var string
*/
private string $country;
/**
* @var string
*/
private string $region;
/**
* @var string
*/
private string $city;
/**
* @var string
*/
private string $street;
/**
* @var string
*/
private string $house;
...
}
class Contact
{
/**
* @var string
*/
private string $phone;
/**
* @var string
*/
private string $email;
/**
* @var Address
*/
private Address $address;
...
}