1. Go to this page and download the library: Download seredoff/evolve-orm 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/ */
seredoff / evolve-orm example snippets
use EvolveORM\Hydrator;
class User {
public int $id;
public string $name;
public array $roles;
}
$data = [
'id' => 1,
'name' => 'Иван Иванов',
'roles' => ['admin', 'editor']
];
$hydrator = Hydrator::create();
$user = $hydrator->hydrate(User::class, $data);
echo $user->id; // Выведет: 1
echo $user->name; // Выведет: Иван Иванов
print_r($user->roles); // Выведет: Array ( [0] => admin [1] => editor )
// Для гидрации нескольких объектов можно использовать метод `hydrateAll`
$dataSet = [
['id' => 1, 'name' => 'Иван Иванов', 'roles' => ['admin']],
['id' => 2, 'name' => 'Петр Петров', 'roles' => ['editor']]
];
$users = $hydrator->hydrateAll(User::class, $dataSet);
// $users теперь содержит массив объектов User
final readonly class Trip
{
/** @var Passenger[] */
#[OrmMapper(ToPassengerCollectionMapper::class)]
public array $passengers;
public function __construct(
public Id $id,
public DateTimeImmutable $date,
public Route $route,
public Bus $bus,
public Driver $driver,
Passenger ...$passengers
) {
$this->passengers = $passengers;
}
}
final readonly class Route
{
/** @var Point[] */
#[OrmMapper(ToPointsCollectionMapper::class)]
public array $points;
public function __construct(
public Id $id,
public string $title,
public Duration $duration,
Point ...$point
) {
$this->points = $point;
}
}
// ... другие классы (Bus, Driver, Passenger, Luggage и т.д.)
use EvolveORM\Bundle\Orm\Mapper;
class UcfirstMapper implements Mapper
{
public function __invoke(mixed $value, Hydrator $hydrator): mixed
{
// логика объектно-реляционного отображения...
return ucfirst($value);
}
}
class UpperMapper implements Mapper
{
public function __invoke(mixed $value, Hydrator $hydrator): mixed
{
// логика объектно-реляционного отображения...
if (!is_array($value)) {
throw new RuntimeException('Unexpected value type');
}
return array_map(
static fn(string $role): string => mb_strtoupper($role),
$value,
);
}
}
use EvolveORM\Hydrator;
use EvolveORM\Bundle\Attribute\OrmMapper;
use EvolveORM\Bundle\Cache\WeakRefReflectionCache;
class User {
public int $id;
public string $name;
#[OrmMapper(UpperMapper::class)]
public array $roles;
}
$data = [
'id' => 1,
'name' => 'иван',
'roles' => ['admin', 'editor']
];
$cache = new WeakRefReflectionCache();
$hydrator = new Hydrator(
$cache,
[
User::class => [
'name' => UcfirstMapper::class,
],
],
);
$user = $hydrator->hydrate(User::class, $data);
echo $user->id; // Выведет: 1
echo $user->name; // Выведет: Иван
print_r($user->roles); // Выведет: Array ( [0] => ADMIN [1] => EDITOR )
class MyCustomStrategy implements HydrationStrategy
{
public function canHydrate(ReflectionProperty $property, mixed $value): bool
{
// Логика определения, может ли стратегия обработать свойство
}
public function hydrate(object $object, ReflectionProperty $property, mixed $value): void
{
// Логика гидрации свойства
}
}
$customStrategies = [
new MyCustomStrategy(),
new AnotherCustomStrategy(),
];
$hydrator = new Hydrator($reflectionCache, $ormConfig, $customStrategies);
$cache->clear();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.