1. Go to this page and download the library: Download rammewerk/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/ */
rammewerk / hydrator example snippets
final class Product {
public string $name = '';
public ?string $sku = null;
public float $price = 0;
public StatusEnum $status = StatusEnum::draft;
public ?\DateTime $created_at = null;
public ?ProductDetails $details = null;
}
class OrderItems {
public ?Product $product = null;
}
$hydrator = new Hydrator(OrderItems::class)->hydrate([
'order_id' => 100,
'product' => [
'name' => 'Some product',
'sku' => '1020',
],
]);
class OrderItem {
/** @var Product[] */
public array $products = [];
}
$hydrator = new Hydrator(OrderItem::class);
// Notice: the mapArray is an immutable method, so we need to assign the result
$hydrator = $hydrator->mapArray('products', Product::class);
$orderItem = $hydrator->hydrate([
'order_id' => 100,
'products' => [
['name' => 'Some product', 'sku' => '1020'],
['name' => 'Some product', 'sku' => '1020'],
],
]);
$orderItem->products[0] instanceof Product; // true
use Rammewerk\Component\Hydrator\Hydrator;
use Rammewerk\Component\Hydrator\HydratorCollection;
$collection = new HydratorCollection(new Hydrator(Product::class), $data);
foreach( $collection as $entity ) {
echo $entity->name;
}
// Get all as an array of entities: Product[]
$entities = $collection->toArray();
public function __construct(
// Promoted parameters:
public readonly string $firstName, // Must be in dataset
public int $age, // Must be in dataset
public ?string $email, // If not in dataset, defaults to null
public bool $active = true, // Optional, uses default if missing
// Non-promoted parameters:
int $
class Entity {
public function __construct(
public readonly int $id
) {
}
}
// Get all users, returns as an array
$user = $this->userRepository->getAll();
foreach($users as $user) {
if( isset($user['email']) && is_string($user['email']) && ! empty($user['email']) ) {
$this->sendEmail($user['email']);
}
}
$users = $this->userRepository->getAll();
foreach ($users as $user) {
if (!$user->email) continue;
$this->sendEmail($user->email);
}
class User {
public string $email = '';
public string $name = '';
public int $id = 0;
}
use Rammewerk\Component\Hydrator\Hydrator;
use Rammewerk\Component\Hydrator\HydratorCollection;
class UserRepository {
/**
* @return HydratorCollection<User>
*/
public function getAll(): HydratorCollection {
$data = $this->fetchAll("SELECT * FROM `users`");
return new HydratorCollection(new Hydrator(User::class), $data);
}
}
public ?string $name = '';
public ?string $name = null;
public int $id;
public string $name = ''; // Optional property with a default value
public function __construct(
public string $id, // Required property
public readonly string $uid, // Required and read-only property
) {}
$entity = new Entity('John Doe', 100);
$hydrator = new \Rammewerk\Component\Hydrator($entity);
class Entity {
public int $age = 0;
public function __construct(int $age = 18) {
$this->age = $age;
}
}
$data = ['name' => 'Johnny'];
$postData = $_POST;
unset($postData['_token']);
$entity = $hydrator->hydrate($data, static fn(PropertyHandler $prop) => match ($prop->name) {
'email' => $postData['email_address'] ?? null,
// 'name' will be ignored because it is set in the first parameter.
'name' => $postData['first_name'] ?? null,
default => null,
});