1. Go to this page and download the library: Download kylekatarnls/morph 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/ */
kylekatarnls / morph example snippets
class User
{
public int $id;
public string $name;
public string $email;
public string $label;
}
$user = new User();
$user->id = 42;
$user->name = 'Katherine Anderson';
$user->email = '[email protected]';
$user->label = '';
$transformer = new Morph\Sequence([
new Morph\PublicPropertiesToArray(),
'array_filter', // or static fn (array $value) => array_filter($value),
new Morph\UpperKeysFirstLetter(['id' => 'ID']),
// a transformer can be composed with any kind of callable
]);
$info = $transformer->transform($user);
// or simply:
$info = $transformer($user);
class UserTransformer extends Morph\Merge
{
protected function getTransformers(): array
{
return [
static fn ($value) => ['username' => $value->name],
new HashUserId(),
];
}
}
$transformer = new UserTransformer();
var_dump($transformer->transform($user, 'sha1'));
class HashUserId extends Morph\MorphBase
{
private string $hashAlgo;
public function __construct(string $hashAlgo)
{
$this->hashAlgo = $hashAlgo;
}
public function __invoke($value = null): array
{
if ($value) {
return ['userid' => hash($this->hashAlgo, (string) $value->id)];
}
return [];
}
}
class UserTransformer extends Morph\Merge
{
private string $idHashAlgo;
public function __construct(string $idHashAlgo)
{
$this->idHashAlgo = $idHashAlgo;
}
protected function getTransformers(): array
{
return [
static fn ($value) => ['username' => $value->name],
new HashUserId($this->idHashAlgo),
];
}
}
$transformer = new UserTransformer('sha1');
var_dump($transformer->transform($user));
class User implements JsonSerializable
{
public int $id;
public string $name;
public string $email;
public string $label;
public function jsonSerialize(): array
{
return ModelTransformer::get()->transform($this);
}
}
class ModelTransformer extends Morph\Sequence
{
private static self $singleton;
public static function get(): self
{
// We can cache the transformer instance
// for better performances.
// This can be done via a simple singleton
// as below.
// Or using a container (see Psr\Container\ContainerInterface)
// such as the Symfony container.
return self::$singleton ??= new self();
}
protected function getTransformers(): array
{
return [
new Morph\PublicPropertiesToArray(),
'array_filter',
new Morph\UpperKeysFirstLetter(['id' => 'ID']),
];
}
}
$user = new User();
$user->id = 42;
$user->name = 'Katherine Anderson';
$user->email = '[email protected]';
$user->label = '';
echo json_encode($user, JSON_PRETTY_PRINT);
class User
{
public function getName(): string { return 'Bob'; }
public function isAdmin(): bool { return false; }
public function update(): void {}
}
$getGetters = new \Morph\Getters(['get', 'is']);
$getGetters(User::class);
[
'Name' => new \Morph\Reflection\Method(new \ReflectionMethod(
User::class, 'getName',
)),
'Admin' => new \Morph\Reflection\Method(new \ReflectionMethod(
User::class, 'isAdmin',
)),
]
class User
{
public string $id = 'abc';
public function getName(): string { return 'Bob'; }
public function isAdmin(): bool { return false; }
public function update(): void {}
}
$bob = new User();
$getGetters = new \Morph\GettersToArray(['get', 'is']);
$getGetters($bob);
[
'Name' => 'Bob',
'Admin' => false,
]
$lowerFirstLetter = new \Morph\LowerFirstLetter([
'Special' => '***special***',
]);
$lowerFirstLetter(5); // 5, non-string input are returned as is
$lowerFirstLetter('FooBar'); // "fooBar"
$lowerFirstLetter('Special'); // "***special***"
$upperFirstLetter = new \Morph\UpperFirstLetter([
'***special***' => 'Special',
]);
$upperFirstLetter(5); // 5, non-string input are returned as is
$upperFirstLetter('fooBar'); // "FooBar"
$upperFirstLetter('***special***'); // "Special"
class User
{
public string $id = 'abc';
public function getName(): string { return 'Bob'; }
public function isAdmin(): bool { return false; }
public function update(): void {}
}
$bob = new User();
$itemsWithTotal = new \Morph\Merge([
new \Morph\PublicPropertiesToArray(),
new \Morph\GettersToArray(['get', 'is']),
]);
$itemsWithTotal(['A', 'B']);
$info = [
'firstName' => 'Georgia',
'lastName' => 'Dobbins',
'group' => 'The Marvelettes',
];
$select = new \Morph\Only('firstName');
$select($info);
[
'firstName' => 'Georgia',
]
$info = [
'firstName' => 'Georgia',
'lastName' => 'Dobbins',
'group' => 'The Marvelettes',
];
$select = new \Morph\Pick('firstName');
$select($info);
'Georgia'
class User
{
public string $name;
protected int $id;
private array $cache;
}
$getProperties = new \Morph\Properties();
$getProperties(User::class);
[
'name' => new \Morph\Reflection\Property(new \ReflectionProperty(
User::class, 'name',
)),
'id' => new \Morph\Reflection\Property(new \ReflectionProperty(
User::class, 'id',
)),
'cache' => new \Morph\Reflection\Property(new \ReflectionProperty(
User::class, 'cache',
)),
]
class User
{
public string $name;
protected int $id;
private array $cache;
}
$getPublicProperties = new \Morph\PublicProperties();
$getPublicProperties(User::class);
[
'name' => new \Morph\Reflection\Property(new \ReflectionProperty(
User::class, 'name',
)),
]
class User
{
public string $name;
protected int $id;
private array $cache;
public function __construct(string $name, int $id)
{
$this->name = $name;
$this->id = $id;
$this->cache = ['foo' => 'bar'];
}
}
$getPublicValues = new \Morph\PublicPropertiesToArray();
$getPublicValues(new User('Juanita Cowart'));