PHP code example of immutablephp / immutable
1. Go to this page and download the library: Download immutablephp/immutable 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/ */
immutablephp / immutable example snippets
use Immutable\ValueObject\ValueObject;
class Address extends ValueObject
{
protected $street;
protected $city;
protected $region;
protected $postcode;
}
class Address extends ValueObject
{
protected $street;
protected $city;
protected $region;
protected $postcode;
public function withChanged(
string $street,
string $city,
string $region,
string $postcode
) {
return $this->with([
'street' => $street,
'city' => $city,
'region' => $region,
'postcode' => $postcode
]);
}
}
class Address extends ValueObject
{
protected $street;
protected $city;
protected $region;
protected $postcode;
public function __construct(
string $street,
string $city,
string $region,
string $postcode
) {
$this->withChanged($street, $city, $region, $postcode);
parent::__construct();
}
public function withChanged(
string $street,
string $city,
string $region,
string $postcode
) : self
{
return $this->with([
'street' => $street,
'city' => $city,
'region' => $region,
'postcode' => $postcode
]);
}
}
public function withChanged(
string $street,
string $city,
string $region,
string $postcode
) {
$valid = AddressValidator::validate($street, $city, $region, $postcode);
if (! $valid) {
throw new \RuntimeException('address is not valid');
}
return $this->with([
'street' => $street,
'city' => $city,
'region' => $region,
'postcode' => $postcode
]);
}
use Immutable\ValueObject\CreditCard;
$creditCard = new CreditCard('5555-5555-5555-4444');
// reading
$creditCard->getNumber(); // '5555555555554444'
$creditCard->getBrand(); // 'VISA'
// changing
$newCreditCard = $creditCard->withNumber('4111-1111-1111-1111');
$newCreditCard->getNumber(); // '4111111111111111'
$newCreditCard->getBrand(); // 'MASTERCARD'
use Immutable\ValueObject\Email;
$email = new Email('[email protected] ');
// reading
$email->get(); // '[email protected] '
// changing
$newEmail = $email->withAddress('[email protected] ');
$newEmail->get(); // '[email protected] '
use Immutable\ValueObject\Ip;
$ip = new Ip('127.0.0.1');
// reading
$ip->get(); // '127.0.0.1'
// changing
$newIp = $ip->withAddress('192.168.0.1');
$newIp->get(); // '192.168.0.1'
use Immutable\ValueObject\Isbn;
$isbn = new Isbn('960-425-059-0');
// reading
$isbn->get(); // '960-425-059-0'
// changing
$newIsbn = $ip->withAddress('0-8044-2957-X');
$newIsbn->get(); // '0-8044-2957-X'
use Immutable\ValueObject\Uri\HttpUri;
$httpUri = new HttpUri(
'http://boshag:[email protected] :8080/foo?bar=baz#dib'
);
// reading
$httpUri->getScheme(); // 'http'
$httpUri->getHost(); // 'example.com'
$httpUri->getPort(); // 8080
$httpUri->getUser(); // 'boshag'
$httpUri->getPass(); // 'bopass'
$httpUri->getPath(); // /'foo'
$httpUri->getQuery(); // 'bar=baz'
$httpUri->getFragment(); // 'dib'
// changing
$newHttpUri = $httpUri
->withScheme('https')
->withHost('example.net')
->withPort('8888')
->withUser('newuser')
->withPass('newpass')
->withPath('/foo2')
->withQuery('zim=gir')
->withFragment('irk');
$newHttpUri->get(); // 'https://newuser:[email protected] :8888/foo2/?zim=gir#irk'
use Immutable\ValueObject\Uuid;
$uuid = new Uuid('12345678-90ab-cdef-1234-567890123456');
// reading
$uuid->get(); // '12345678-90ab-cdef-1234-567890123456'
// changing
$newUuid = $uuid->withIdentifier('11111111-1111-1111-1111-111111111111');
$newUuid->get(); // '11111111-1111-1111-1111-111111111111'
// create a new random UUIDv4 identifier
$uuidv4 = Uuid::newVersion4();
use Immutable\Bag;
$bag = new Bag(['foo' => 'bar']);
echo $bag->foo; // bar
echo $bag['foo']; // bar
$bag->foo = 'baz'; // ImmutableObjectException
$bag = $bag->with('foo', 'baz');
echo $bag->foo; // baz
echo $bag['foo']; // baz
unset($bag->foo); // ImmutableObjectException
$bag = $bag->without('foo');
$bag->dib; // Notice: $dib not defined
$bag = $bag->with('dib', ['zim', 'gir']);
foreach ($bag->dib as $key => $value) {
echo "$key:$value,"; // 0:zim,1:gir,
}