PHP code example of asokol1981 / abstract-mold
1. Go to this page and download the library: Download asokol1981/abstract-mold 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/ */
asokol1981 / abstract-mold example snippets
use ASokol1981\AbstractMold\AbstractMutableMold;
final class UserMutableMold extends AbstractMutableMold
{
protected function publicFields(): array
{
return ['name', 'email', 'age'];
}
protected function validatedData(): array
{
$age = $this->getRawData('age');
return [
'name' => (string) $this->getRawData('name', ''),
'email' => strtolower((string) $this->getRawData('email', '')),
'age' => $age !== null ? (int) $age : null,
];
}
}
// Usage
$mold = new UserMutableMold(['name' => 'John', 'email' => '[email protected] ']);
$mold->change('name', 'Johnny');
$mold->changes(['email' => '[email protected] ', 'age' => '25']);
$data = $mold->validated();
// ['name' => 'Johnny', 'email' => '[email protected] ', 'age' => 25]
$changes = $mold->changesValidated();
// ['name' => 'Johnny', 'email' => '[email protected] ', 'age' => 25]
use ASokol1981\AbstractMold\AbstractImmutableMold;
final class UserImmutableMold extends AbstractImmutableMold
{
protected function publicFields(): array
{
return ['name', 'email', 'age'];
}
protected function validatedData(): array
{
$age = $this->getRawData('age');
return [
'name' => (string) $this->getRawData('name', ''),
'email' => strtolower((string) $this->getRawData('email', '')),
'age' => $age !== null ? (int) $age : null,
];
}
}
// Usage
$mold = new UserImmutableMold(
['name' => 'John', 'email' => '[email protected] '],
['name' => 'Johnny', 'age' => '25']
);
$data = $mold->validated();
// ['name' => 'Johnny', 'email' => '[email protected] ', 'age' => 25]
$changes = $mold->changesValidated();
// ['name' => 'Johnny', 'age' => 25]