odan/hydrator

此包已被弃用,不再维护。作者建议使用zendframework/zend-hydrator包。

PHP的高性能hydrator。

1.4.1 2018-03-10 08:38 UTC

This package is auto-updated.

Last update: 2019-04-27 12:39:50 UTC


README

PHP的高性能hydrator。

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

需求

  • PHP >= 7.1

安装

composer require odan/hydrator

功能

  • 数组转对象
  • 对象转数组

对象属性

任何与公开访问属性匹配的数据键都将进行hydrating;任何公开属性都将用于提取。

类方法

任何与setter方法匹配的数据键将按顺序调用以进行hydrating;任何与getter方法匹配的方法将用于提取。

使用

// User entity
class User
{
    public $id;
    public $username;
    public $firstName;
    public $email;
}

// A row from the database
$userRow = [
    'id' => 1,
    'username' => 'admin',
    'first_name' => 'John Doe',
    'email' => 'john@example.com'
];

// Create the hydrator
$hydrator = new \Odan\Hydrator\ObjectProperty();

// Convert array to a new User object (with lower camel case properties)
$user = $hydrator->hydrate($userRow, new User());

print_r($user);

/*
User Object
(
    [id] => 1
    [username] => admin
    [firstName] => John Doe
    [email] => john@example.com
)
*/

// Convert User object to an array with lower camel case keys
$array = $hydrator->extract($user);

print_r($array);

/*
Array
(
    [id] => 1
    [username] => admin
    [first_name] => John Doe
    [email] => john@example.com
)
*/

替代方案

许可证

MIT许可证(MIT)。请参阅许可证文件获取更多信息。