PHP code example of ommua / solid-domain

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

    

ommua / solid-domain example snippets


EXAMPLE: 

use Ommua\FailureResponse;
use Ommua\SuccessResponse;
use Ommua\SolidDomain;
use Ommua\Interfaces\EitherFailureOrSuccess;



class ConvertStringToInteger extends SolidDomain
{
    /**
     *  
     * @param String $stringNumber
     * @return EitherFailureOrSuccess
     */
    final protected function invokeDomain(String $stringNumber)
    {
        if (is_numeric($stringNumber))
            return new SuccessResponse(intval($stringNumber));
        else
            return new FailureResponse(["Error cast string {$stringNumber}"]);
    }
}

$convertStringToInteger = new ConvertStringToInteger();
$failureOrSuccess = $convertStringToInteger("5");

$result= $failureOrSuccess->fold(function($error){
   return $error;
}, function ($success){
    return $success;
});

// Print: 5
echo $result;

$failureOrSuccess = $convertStringToInteger("five");
$result= $failureOrSuccess->fold(function($error){
   return $error;
}, function ($success){
    return $success;
});


// Print:  ["Error cast string five"]
echo $result;