PHP code example of someniatko / result-type

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

    

someniatko / result-type example snippets




use Someniatko\ResultType\Success;
use Someniatko\ResultType\Error;

$value = (new Success('Let it be'))
    ->map(fn (string $s) => substr_count($s, ' ')) // Success<2>
    ->chain(fn (int $wordsCount) => $wordsCount > 3 
        ? new Success('Long text') 
        : new Error('short text')
    ) // Error<'short text'>
    ->map(fn (string $s) => str_replace(' ', '', $s)) // will not be called because we're in Error result
    ->mapError(fn (string $s) => strtoupper($s)) // Error<'SHORT TEXT'>
    ->get(); // 'SHORT TEXT'