PHP code example of fi1a / hydrator
1. Go to this page and download the library: Download fi1a/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/ */
fi1a / hydrator example snippets
use Fi1a\Hydrator\Hydrator;
class Foo {
/**
* @var string
*/
public $propertyBar;
/**
* @var int
*/
protected $propertyBaz;
/**
* @var bool
*/
private $propertyQux;
}
$hydrator = new Hydrator();
/**
* @var Foo $model
*/
$model = $hydrator->hydrate([
'propertyBar' => 'value',
'propertyBaz' => 1,
'propertyQux' => true,
], Foo::class);
$model->propertyBar; // 'value'
use Fi1a\Hydrator\Hydrator;
use Fi1a\Hydrator\Hydrates\Hydrate;
class Foo {
/**
* @var string
*/
public $propertyBar;
/**
* @var int
*/
protected $propertyBaz;
/**
* @var bool
*/
private $propertyQux;
}
$hydrator = new Hydrator();
$model = new Foo();
$hydrator->hydrateModel([
'propertyBar' => 'value',
'propertyBaz' => 1,
'propertyQux' => true,
], $model);
$model->propertyBar; // 'value'
use Fi1a\Hydrator\Hydrator;
class Foo {
/**
* @var string
*/
public $propertyBar;
/**
* @var int
*/
protected $propertyBaz;
/**
* @var bool
*/
private $propertyQux;
}
$hydrator = new Hydrator();
/**
* @var Foo $model
*/
$model = $hydrator->hydrate([
'propertyBar' => 'value',
'propertyBaz' => 1,
'propertyQux' => true,
], Foo::class);
$model->propertyBar; // 'value'
use Fi1a\Hydrator\Hydrator;
use Fi1a\Hydrator\Hydrates\HydrateCallSetters;
class Foo {
public $propertyBar;
protected $propertyBaz;
private $propertyQux;
protected function setPropertyBaz(int $propertyBaz): void
{
$this->propertyBaz = $propertyBaz + 1;
}
public function getPropertyBaz(): int
{
return $this->propertyBaz;
}
}
$hydrator = new Hydrator(new HydrateCallSetters());
/**
* @var Foo $model
*/
$model = $hydrator->hydrate([
'propertyBar' => 'value',
'propertyBaz' => 1,
'propertyQux' => true,
], Foo::class);
$model->getPropertyBaz(); // 2
use Fi1a\Hydrator\Hydrator;
use Fi1a\Hydrator\Hydrates\HydratePublicCallSetters;
class Foo {
public $propertyBar;
protected $propertyBaz;
private $propertyQux;
public function setPropertyBaz(int $propertyBaz): void
{
$this->propertyBaz = $propertyBaz + 2;
}
public function getPropertyBaz(): int
{
return $this->propertyBaz;
}
}
$hydrator = new Hydrator(new HydratePublicCallSetters());
/**
* @var Foo $model
*/
$model = $hydrator->hydrate([
'propertyBar' => 'value',
'propertyBaz' => 1,
'propertyQux' => true,
], Foo::class);
$model->getPropertyBaz(); // 3
use Fi1a\Hydrator\Extractor;
class Foo {
/**
* @var string
*/
public $propertyBar = 'value';
/**
* @var int
*/
protected $propertyBaz = 1;
/**
* @var bool
*/
private $propertyQux = true;
}
$model = new Foo();
$hydrator = new Extractor();
$data = $hydrator->extract($model); // ['propertyBar' => 'value',]
use Fi1a\Hydrator\Extractor;
class Foo {
/**
* @var string
*/
public $propertyBar = 'value';
/**
* @var int
*/
protected $propertyBaz = 1;
/**
* @var bool
*/
private $propertyQux = true;
}
$model = new Foo();
$hydrator = new Extractor();
$data = $hydrator->extract($model, ['propertyBar', 'propertyBaz']); // ['propertyBar' => 'value', 'propertyBaz' => 1,]
use Fi1a\Hydrator\Extractor;
use Fi1a\Hydrator\Extracts\ExtractPublicCallGetters;
class Foo {
/**
* @var string
*/
public $propertyBar = 'value';
/**
* @var int
*/
protected $propertyBaz = 1;
/**
* @var bool
*/
private $propertyQux = true;
public function getPropertyBaz(): int
{
return $this->propertyBaz;
}
}
$model = new Foo();
$hydrator = new Extractor(new ExtractPublicCallGetters());
$data = $hydrator->extract($model); // ['propertyBar' => 'value', 'propertyBaz' => 1,]
use Fi1a\Hydrator\Extractor;
use Fi1a\Hydrator\Extracts\ExtractCallGetters;
class Foo {
/**
* @var string
*/
public $propertyBar = 'value';
/**
* @var int
*/
protected $propertyBaz = 1;
/**
* @var bool
*/
private $propertyQux = true;
public function getPropertyBaz(): int
{
return $this->propertyBaz;
}
}
$model = new Foo();
$hydrator = new Extractor(new ExtractCallGetters());
$data = $hydrator->extract($model); // ['propertyBar' => 'value', 'propertyBaz' => 1, 'propertyQux' => true]
use Fi1a\Hydrator\Extractor;
use Fi1a\Hydrator\Extracts\ExtractPublic;
class Foo {
/**
* @var string
*/
public $propertyBar = 'value';
/**
* @var int
*/
protected $propertyBaz = 1;
/**
* @var bool
*/
private $propertyQux = true;
public function getPropertyBaz(): int
{
return $this->propertyBaz;
}
}
$model = new Foo();
$hydrator = new Extractor(new ExtractPublic());
$data = $hydrator->extract($model); // ['propertyBar' => 'value',]
use Fi1a\Hydrator\Extractor;
use Fi1a\Hydrator\Extracts\Extract;
class Foo {
/**
* @var string
*/
public $propertyBar = 'value';
/**
* @var int
*/
protected $propertyBaz = 1;
/**
* @var bool
*/
private $propertyQux = true;
public function getPropertyBaz(): int
{
return $this->propertyBaz;
}
}
$model = new Foo();
$hydrator = new Extractor(new Extract());
$data = $hydrator->extract($model); // ['propertyBar' => 'value', 'propertyBaz' => 1, 'propertyQux' => true]
use Fi1a\Hydrator\Hydrator;
use Fi1a\Hydrator\Hydrates\Hydrate;
use Fi1a\Hydrator\KeyName\Humanize;
class Foo {
/**
* @var string
*/
public $propertyBar;
/**
* @var int
*/
protected $propertyBaz;
/**
* @var bool
*/
private $propertyQux;
}
$hydrator = new Hydrator(new Hydrate(new Humanize()));
$model = new Foo();
$hydrator->hydrateModel([
'property_bar' => 'value',
'property_baz' => 1,
'property_qux' => true,
], $model);
$model->propertyBar; // 'value'
use Fi1a\Hydrator\Extractor;
use Fi1a\Hydrator\Extracts\Extract;
use Fi1a\Hydrator\KeyName\Humanize;
class Foo {
/**
* @var string
*/
public $propertyBar = 'value';
/**
* @var int
*/
protected $propertyBaz = 1;
/**
* @var bool
*/
private $propertyQux = true;
public function getPropertyBaz(): int
{
return $this->propertyBaz;
}
}
$model = new Foo();
$hydrator = new Extractor(new Extract(new Humanize()));
$data = $hydrator->extract($model); // ['property_bar' => 'value', 'property_baz' => 1, 'property_qux' => true]