pushoperations/decorators

This package is abandoned and no longer maintained. No replacement package was suggested.

A library of decorators.

v1.0.0 2015-06-19 23:12 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:00:49 UTC


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, ...];