1. Go to this page and download the library: Download scn/hydrator 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/ */
scn / hydrator example snippets
ExtractorConfig implements Configuration\ExtractorConfigInterface
{
public function getExtractorProperties(): array
{
return [
'property' => function (string $propertyName): string {
return $this->privateProperty; // `$this` will be the entity to extract
}
];
}
}
class Entity
{
private $privateProperty = 'private value';
}
$hydrator = new \Hydrator();
$result = $hydrator->extract(
new ExtractorConfig(),
new Entity()
);
var_dump(assert($result === ['property' => 'private value'])); // -> bool(true)
HydratorConfig implements Configuration\HydratorConfigInterface
{
public function getHydratorProperties(): array
{
return [
'property' => function (string $value, string $propertyName): void {
$this->privateProperty = $value; // $this will be the entity to hydrate
}
];
}
}
class Entity
{
private $privateProperty = 'private value';
public function getPropertyValue(): string
{
return $this->privateProperty;
}
}
$hydrator = new \Hydrator();
$data = [
'property' => 'hydrated private value',
];
$entity = new Entity();
$hydrator->hydrate(
new HydratorConfig(),
$entity,
$data
);
var_dump(assert('hydrated private value' === $entity->getPropertyValue())); // -> bool(true)
HydratorConfig implements Configuration\HydratorConfigInterface
{
public function getHydratorProperties(): array
{
return [
'property_a' => function (string $value, string $propertyName): void {
$this->privatePropertyA = $value;
},
'property_b' => function (string $value, string $propertyName): void {
$this->privatePropertyB = $value;
},
'property_c' => function (string $value, string $propertyName): void {
$this->privatePropertyC = $value;
},
];
}
}
class Entity
{
private $privatePropertyA;
private $privatePropertyB;
private $privatePropertyC;
public function getPropertyValues(): array
{
return [
$this->privatePropertyA,
$this->privatePropertyB,
$this->privatePropertyC,
];
}
}
$hydrator = new \Hydrator();
$data = ['value a', 'value_b', 'value_c'];
$entity = new Entity();
$hydrator->hydrate(
new HydratorConfig(),
$entity,
$data,
Hydrator::IGNORE_KEYS
);
var_dump(assert($data === $entity->getPropertyValues())); // -> bool(true)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.