承接 digiaonline/instance-factory 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

digiaonline/instance-factory

最新稳定版本:1.1.0

Composer 安装命令:

composer require digiaonline/instance-factory

包简介

A factory for constructing objects from arrays of properties

README 文档

README

Build Status Coverage Status

A factory for constructing objects from arrays of properties

Installation

composer require digiaonline/instance-factory

Usage

<?php

require_once(__DIR__ . '/vendor/autoload.php');

/**
 * A class we want to be able to instantiate. Phpdoc has been left out for brevity.
 */
class Person
{
    private $name;

    private $age;

    private $optionalInformation;

    public function __construct(string $name, int $age, ?string $optionalInformation = null)
    {
        $this->name                = $name;
        $this->age                 = $age;
        $this->optionalInformation = $optionalInformation;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getAge(): int
    {
        return $this->age;
    }

    public function getOptionalInformation(): ?string
    {
        return $this->optionalInformation;
    }
}

// Create one instance where we skip the optional information
/** @var Person $personOne */
$personOne = \Digia\InstanceFactory\InstanceFactory::fromProperties(Person::class, [
    'name' => 'John Smith',
    'age'  => 34,
]);

// Create another instance where we do supply optional information
/** @var Person $personOne */
$personTwo = \Digia\InstanceFactory\InstanceFactory::fromProperties(Person::class, [
    'name'                => 'Alice Smith',
    'age'                 => 33,
    'optionalInformation' => 'Not related to John Smith',
]);

This will print the following:

class Person#5 (3) {
  private $name =>
  string(10) "John Smith"
  private $age =>
  int(34)
  private $optionalInformation =>
  NULL
}

class Person#2 (3) {
  private $name =>
  string(11) "Alice Smith"
  private $age =>
  int(33)
  private $optionalInformation =>
  string(25) "Not related to John Smith"
}

If instantiation fails for whatever reason, \RuntimeException is thrown.

License

MIT

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 5
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-08-24