PHP code example of pushoperations / decorators

1. Go to this page and download the library: Download pushoperations/decorators library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pushoperations / decorators example snippets




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;
            }
        });
    }
}

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