adityasetiono / php-functions

PHP函数助手

5.0.0 2018-03-12 23:55 UTC

This package is auto-updated.

Last update: 2024-09-09 13:43:06 UTC


README

一个用于将数组序列化/反序列化为对象或相反的PHP库。

安装

使用composer安装

composer require adityasetiono/php-functions

用法

导入所需函数,例如

use function adityasetiono\util\{deserialize, serialize, generate_alphanumeric};

随机字符串生成器

使用方法

$string = generate_alphanumeric(5);

// => "aR4z0"

序列化/反序列化

这是一个将数组序列化/反序列化为对象及其相反过程的函数。因为库作为外部函数构建,所以类属性需要具有gettersetter

一个类示例

// User.php
class User {
    protected $firstName;
    protected $lastName;
    protected $email;
    protected $username;
    
    public function setFirstName(string $firstName): User
    {
        $this->firstName = $firstName;
        
        return $this;
    }
    
    public function getFirstName(): string
    {
        return $this->firstName;
    }
    
    public function setLastName(string $lastName): User
    {
        $this->lastName = $lastName;
        
        return $this;
    }
    
    public function getLastName(): string
    {
        return $this->lastName;
    }
    
    public function setEmail(string $email): User
    {
        $this->email = $email;
        
        return $this;
    }
    
    public function getEmail(): string
    {
        return $this->email;
    }
    
    public function setUsername(string $username): User
    {
        $this->username = $username;
        
        return $this;
    }
    
    public function getUsername(): string
    {
        return $this->username;
    }
}

序列化器

$user = serialize([
    'firstName' => 'John',
    'lastName' => 'Doe',
    'email' => 'john.doe@example.com',
    'username' => 'johndoe'
], User::class);

结果

echo $user->getFirstName(); // John
echo $user->getEmail(); // john.doe@example.com

序列化的对象与DoctrineORM实体兼容,可以直接持久化到数据库中(目前仅测试了Doctrine,其他ORM实体需要单独测试)。这是一个将数组序列化为对象的通用序列化器。

以下是DoctrineORM实体序列化的示例

// User.php
use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User {
    
    /**
     * @ORM\Id
     * @ORM\Column(type="guid")
     * @ORM\GeneratedValue(strategy="UUID")
     */
    protected $uuid;
    
    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $firstName;
    
    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $lastName;
    
    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $email;
    
    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $username;
    
    public function getUuid(): string
    {
        return $this->uuid;
    }
    
    public function setFirstName(string $firstName): User
    {
        $this->firstName = $firstName;
        
        return $this;
    }
    
    public function getFirstName(): string
    {
        return $this->firstName;
    }
    
    public function setLastName(string $lastName): User
    {
        $this->lastName = $lastName;
        
        return $this;
    }
    
    public function getLastName(): string
    {
        return $this->lastName;
    }
    
    public function setEmail(string $email): User
    {
        $this->email = $email;
        
        return $this;
    }
    
    public function getEmail(): string
    {
        return $this->email;
    }
    
    public function setUsername(string $username): User
    {
        $this->username = $username;
        
        return $this;
    }
    
    public function getUsername(): string
    {
        return $this->username;
    }
}

序列化器

Uuid是主键,由数据库自动生成。

$user = serialize([
    'firstName' => 'John',
    'lastName' => 'Doe',
    'email' => 'john.doe@example.com',
    'username' => 'johndoe'
], User::class);

持久化到数据库

// obtaining the entity manager
$entityManager = EntityManager::create($conn, $config);
$entityManager->persist($user);
$entityManager->flush();

反序列化器

从数据库获取对象/实体

$uuid = 'e48a0ba1-1504-11e8-ae97-0649b2727d37'; // Hardcoded for display purposes
$user = $entityManager->find(User::class, $uuid);

使用反序列化

$array = deserialise($user); // empty option means complete deserialization by default.
echo json_encode($array);

$array的内容将是

{
  "uuid": "e48a0ba1-1504-11e8-ae97-0649b2727d37",
  "firstName": "John",
  "lastName": "Doe",
  "email": "john.doe@example.com",
  "username": "johndoe"
}

另一个带有自定义字段的示例。第二个参数接受一个关联数组,键是结果的关键,值是属性名。反序列化器期望值与getter的camelCase名称完全相同,但不包含get

$customArray = deserialize($user,[
    'first_name' => 'firstName',
    'surname' => 'lastName',
    'email' => 'email',
]);
echo json_encode($customArray);

$customArray的内容将是

{
  "first_name": "John",
  "surname": "Doe",
  "email": "john.doe@example.com"
}

反序列化器应与嵌套对象和对象数组一起工作。更详细的示例将在文档中提供。(它可在测试文件中找到)。

问题

请随时使用GitHub问题或直接发送电子邮件到winged.zach@gmail.com,因为我个人开始了这个库。欢迎合作者。