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
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']);