PHP code example of mantas-done / short-closures

1. Go to this page and download the library: Download mantas-done/short-closures 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/ */

    

mantas-done / short-closures example snippets


// instead of this code:
$totalRevenue = $employees
    ->filter(function ($employee) {
        return $employee->district == 'Amsterdam';
    })->flatMap(function ($employee) {
        return $employee->customers;
    })->map(function ($customer) {
        return $customer->orders->sum('total');
    })->filter(function ($customerTotal) {
        return $customerTotal >= 75000;
    })->sum();

// you can do:
$totalRevenue = $employees
    ->filter('$employee->district == "Amsterdam"')
    ->flatMap('$employee->customers')
    ->map('$customer->orders->sum("total")')
    ->filter('$customerTotal >= 75000')
    ->sum();

// instead of this code:
$anonymous_function = function ($x) {
   return $x * 2;
}

// you can do:
$anonymous_function = c('$x ~> $x * 2');

// or even shorter:
$anonymous_function = c('$x * 2');


// usage 
echo $anonymous_function(3); // output: 6

// all this syntax is valid
c('$x * 2');
c('$x ~> $x * 2');
c('($x) ~> $x * 2');
c('$x ~> {return $x * 2;}');
c('($x) ~> {return $x * 2;}');
c('($v, $k) ~> $v == 2 && $k == 1');
c('($v, $k) ~> {return $v == 2 && $k == 1;}');
c('($v, $k) ~> {$v2 = $v * 2; $k2 = $k * 2; return $v2 == 4 && $k2 == 2;}');

$totalRevenue = $employees
    ->filter(c('$employee->district == "Amsterdam"'))
    ->flatMap(c('$employee->customers'))
    ->map(c('$customer->orders->sum("total")'))
    ->filter(c('$customerTotal >= 75000'))
    ->sum();

$totalRevenue = $employees
    ->filter('$employee->district == "Amsterdam"')
    ->flatMap('$employee->customers')
    ->map('$customer->orders->sum("total")')
    ->filter('$customerTotal >= 75000')
    ->sum();

// instead of this code:
$users = User::whereHas('comments', function ($q) {
    $q->published();
})->get();

// it could be replaced with:
$users = User::whereHas('comments', 'published()')->get();

// ofcouse you can use it without tight integration, but it doesn't look nice
$users = User::whereHas('comments', c('$q->published()'))->get();