PHP code example of mehradsadeghi / laravel-decorator

1. Go to this page and download the library: Download mehradsadeghi/laravel-decorator 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/ */

    

mehradsadeghi / laravel-decorator example snippets


class Person {

    public function makeFullName($firstName, $lastName)
    {
        return "$firstName $lastName";
    }
}

$person = new Person();
$person->makeFullName('mehrad', 'sadeghi'); // mehrad sadeghi


decorate([Person::class, 'makeFullName'], ['mehrad', 'sadeghi']); // mehrad sadeghi

$decorator = function ($callable) {
    return function (...$params) use ($callable) {
    
        // decorating the inputs
        foreach($params as $key => $param) {
            $params[$key] = trim($param);
        }

        // real call to makeFullName method
        $output = app()->call($callable, $params);

        // decorating the output
        $output = strtoupper($output);

        return $output;
    };
};

decorator([Person::class, 'makeFullName'])->set($decorator);


class PersonDecorator {

    public function decorateFullName($callable)
    {
        return function (...$params) use ($callable) {
        
            // decorating the inputs
            foreach($params as $key => $param) {
                $params[$key] = trim($param);
            }

            // real call to makeFullName method
            $output = app()->call($callable, $params);

            // decorating the output
            $output = strtoupper($output);

            return $output;
        };
    }
}

decorator([Person::class, 'makeFullName'])->set([PersonDecorator::class, 'decorateFullName']);


decorate([Person::class, 'makeFullName'], ['  mehrad ', '     sadeghi ']); // MEHRAD SADEGHI


decorator([Person::class, 'makeFullName'])
        ->set(function($callable) {
            // decoration
        })
        ->set(function($callable) {
            // decoration
        });

decorator([Person::class, 'makeFullName'])
    ->set([PersonDecorator::class, 'secondDecorator'])
    ->set([PersonDecorator::class, 'firstDecorator']);

class PersonDecorator {

    public function decorateInput($callable)
    {
        return function (...$params) use ($callable) {

            // decorating the inputs
            foreach($params as $key => $param) {
                $params[$key] = trim($param);
            }

            // real call to makeFullName method
            $output = app()->call($callable, $params);

            return $output;
        };
    }

    public function decorateOutput($callable)
    {
        return function (...$params) use ($callable) {

            // real call to makeFullName method
            $output = app()->call($callable, $params);

            // decorating the output
            $output = strtoupper($output);

            return $output;
        };
    }
}

decorator([Person::class, 'makeFullName'])
    ->set([PersonDecorator::class, 'decorateInput'])
    ->set([PersonDecorator::class, 'decorateOutput']);

decorate([Person::class, 'makeFullName'], ['  mehrad ', '     sadeghi ']); // MEHRAD SADEGHI

decorator([Person::class, 'makeFullName'])
    ->forget([PersonDecorator::class, 'decorateOutput']);

decorate([Person::class, 'makeFullName'], ['  mehrad ', '     sadeghi ']); // mehrad sadeghi

decorator([Person::class, 'makeFullName'])->forget();