pushoperations/decorators 问题修复 & 功能扩展

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

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

pushoperations/decorators

最新稳定版本:v1.0.0

Composer 安装命令:

composer require pushoperations/decorators

包简介

A library of decorators.

README 文档

README

Build Status Coverage Status Scrutinizer Code Quality

Total Downloads Latest Stable Version Latest Unstable Version License

A library to decorate arrays (especially Laravel's Input::) for manipulation and usage as a service to return data for object construction.

Note: this library may contain other patterns in the future.

Contents

Install

The recommended way to install is through Composer.

Update your project's composer.json file to include Decorators:

{
    "require": {
        "pushoperations/decorators": "1.*"
    }
}

Then update the project dependencies to include this library:

composer update pushoperations/decorators

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

Usage

Create your own decorator by:

  • Extending from the DataDecorator abstract class
  • Creating a constructor that accepts an array
  • Optional: add methods that are specific to generating the array of data you need

One use case is to split input data into different arrays for different factories to use for the construction of new objects.

A handy effect is that you can perform sanitization within the decorator's methods before returning the data array.

Examples

use Push\Decorators\DataDecorator;
use Push\Decorators\DataDecoratorInterface;

class BasicDecorator extends DataDecorator implements DataDecoratorInterface
{
    public function __construct(array $input)
    {
        $this->data = $input;
    }
}

class ComplexDecorator extends DataDecorator implements DataDecoratorInterface
{
    public function __construct(array $input)
    {
        $this->data = $input;
    }

    public function complicate()
    {
        return array_map($this->data, function($value) {
            if (is_int($value)) {
                return $value * 2;
            }
        });
    }
}

Common usage would be to filter and pick apart the user input for create/update:

$input = [
    'name' => 'Push Operations',
    'desks' => 50,
    'employees' => [
        'John', 'Jane',
    ],
];

$basic = new BasicDecorator($input);

// Check if value for key exists
echo $basic->has('desks');                      // true
echo $basic->has('chairs');                     // false

// Provide a default value if it doesn't exist
echo $basic->get('name');                       // 'Push Operations'
echo $basic->get('chairs', 10);                 // 10

// Get some of the data
var_dump($basic->only('name', 'desks'));        // ['name' => 'Push Operations', 'desks' => 50]
var_dump($basic->only(['name', 'desks']));      // ['name' => 'Push Operations', 'desks' => 50]
var_dump($basic->except('name'));               // ['desks' => 50, 'employees' => ['John', 'Jane']]

// Get all of the data
var_dump($basic->all());                        // The $input array

// Add data
$add = [
    'interns' => [
        'Billy', 'Derrick'
    ],
];
$basic->merge($add);
var_dump($basic->get('interns'));               // ['Billy', 'Derrick']

// You can redecorate the results of the decorator (with itself or another decorator) to do more manipulation.

$complex = new ComplexDecorator($basic->all());
var_dump($complex->complicate());               // [..., 'desks' => 100, ...];

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-01-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固