lc5/from-array 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lc5/from-array

Composer 安装命令:

composer require lc5/from-array

包简介

Create objects from arrays with type checks

README 文档

README

Build Status Latest Stable Version Total Downloads PHP Version Require License PHPStan Enabled

Create objects from arrays with type checks.

Installation

$ composer require lc5/from-array

Usage

Add FromArrayTrait to the class you wish to be instantiated with the values from the array. It provides fromArray method, which will validate the data and create the object if the data is valid. Otherwise, either PHP TypeError or Lc5\FromArray\Exception\InvalidArgumentException will be thrown.

The validation consists of the following steps:

  • check if all the required properties are present
  • check if there are no redundant properties
  • check if all the properties have correct types according to the doc blocks. See Supported Annotations

Aforementioned behaviour can be configured. See Options

Basic example

use Lc5\FromArray\FromArrayTrait;

class ExampleClass
{
    use FromArrayTrait;

    private bool $bool;
    private int $int;
    private float $float;
    private string $string;
    private array $array;
    private object $object;
}

$properties = [
    'bool' => true,
    'int' => 2,
    'float' => 3.5,
    'string' => 'example string',
    'array' => ['example array'],
    'object' => new stdClass()
];

$exampleObject = ExampleClass::fromArray($properties);

Advanced example

use Lc5\FromArray\FromArrayTrait;

class ExampleClass
{
    use FromArrayTrait;
    
    /** @var callable */
    private $callable;
    /** @var mixed[] */
    private iterable $iterable;
    /** @var stdClass[] */
    private array $typedArray;
    /** @var stdClass[] */
    private iterable $typedIterable;
    /** @var mixed */
    private $mixed;
    /** @var int|float */
    public $intOrFloat;
}

$properties = [
    'callable' => function (): void {},
    'iterable' => new ArrayObject(),
    'typedArray' => [new stdClass(), new stdClass()],
    'typedIterable' => new ArrayObject([new stdClass(), new stdClass()]),
    'mixed' => 'mixed',
    'intOrFloat' => 1.5
];

$exampleObject = ExampleClass::fromArray($properties);

Docs

Options

The following options are available:

  • DEFAULT - check for missing and redundant properties and check types
  • VALIDATE_MISSING - check for missing properties
  • VALIDATE_REDUNDANT - check for redundant properties
  • VALIDATE_TYPES - check types of properties

Options can be combined using bitwise operators. To disable validation of redundant properties in order to be able to pass an array with more properties you can use the following code:

ExampleClass::fromArray($properties, Options::DEFAULT & ~Options::VALIDATE_REDUNDANT);

More info: https://www.php.net/manual/en/language.operators.bitwise.php

Supported Annotations

The following doc block annotations are supported:

  • callable - standard PHP callable type
  • mixed - represents PHP mixed typed, which basically means any type
  • T[] - represents typed iterable of items of a given type e.g. int[], stdClass[] etc.
  • union types - e.g. int|float - representing union of types

Standard PHP types are supported by native Typed Properties

统计信息

  • 总下载量: 2.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固