PHP code example of wernerdweight / ra

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

    

wernerdweight / ra example snippets


use WernerDweight\RA\RA;
 
// helper methods (extracted here to simplify the code below and emphasize the difference)

function filterFunction(string $god): bool {
    return false !== strpos($god, 's');
}
 
function mapFunction(string $godContainingTheLetterS): string {
    return strtoupper($godContainingTheLetterS);
}
 
function reduceFunction(string $carry, string $god): string {
    return $carry .= ($carry[-1] === ' ' ? '' : ', ') . $god;
}
 
// create new RA
$egyptianGods = new RA(['Ra', 'Osiris', 'Anubis', 'Horus']);
 
// use as object
$godsContainingTheLetterSInUppercase = $egyptianGods
    ->filter('filterFunction')
    ->map('mapFunction')
    ->reverse()
    ->reduce('reduceFunction', 'My favourite Egyptian Gods are ');
 
echo $godsContainingTheLetterSInUppercase . "\n";
 
// use as normal array
$godsContainingTheLetterSInUppercase = array_reduce(
    array_reverse(
        array_map(
            'mapFunction',
            array_filter(
                $egyptianGods->toArray(),
                'filterFunction'
            )
        )
    ),
    'reduceFunction',
    'My favourite Egyptian Gods are '
);
 
echo $godsContainingTheLetterSInUppercase . "\n";
 
// RA extends Iterator, ArrayAccess and Countable
foreach ($egyptianGods as $god) {
    echo sprintf("My favourite Egyptian God is %s\n", $god);
}