PHP code example of mistralys / application-utils-result-handling

1. Go to this page and download the library: Download mistralys/application-utils-result-handling 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/ */

    

mistralys / application-utils-result-handling example snippets


use function AppUtils\operationResult

const ERROR_FILE_NOT_FOUND = 1;

function doSomething() : OperationResult 
{
    // Create a new result object using the global function
    $result = operationResult();
    
    if(!file_exists('waldo.txt')) {
        return $result->makeError(
            'The waldo file could not be found :(',
            ERROR_FILE_NOT_FOUND
        );
    }
    
    return $result;
}

$result = doSomething();

if(!$result->isValid()) {
    echo $result;
}

use function AppUtils\operationCollection;
use AppUtils\OperationResult_Collection;

const VALIDATION_ERROR_NAME_TOO_SHORT = 1;
const VALIDATION_WARNING_NOT_RECOMMENDED_LENGTH = 2;

function validateSomething() : OperationResult_Collection 
{
    $collection = operationCollection();
    
    $collection->makeError(
        'The name must be at least 5 characters long',
        VALIDATION_ERROR_NAME_TOO_SHORT
    );
    
    $collection->makeWarning(
        'The name must be at most 50 characters long',
        VALIDATION_WARNING_NOT_RECOMMENDED_LENGTH
    )
    
    return $collection;
}

$collection = validateSomething();

if(!$collection->isValid()) {
     $results = $collection->getResults();
     // do something with the results
}

use function AppUtils\operationCollection;

$collection = operationCollection();

// Get all results in the order they were added
$results = $collection->getResults(); 

// Get all errors
$errors = $collection->getErrors();

// Get all warnings
$warnings = $collection->getWarnings();

// Get all successes
$successes = $collection->getSuccesses();

// Get all notices
$notices = $collection->getNotices();

// Check if the collection contains any result of a specific type
if($collection->isError()) {}
if($collection->isWarning()) {}
if($collection->isSuccess()) {}
if($collection->isNotice()) {}

// Check if the collection contains a specific result code
if($collection->containsCode(1)) {
    // do something
}

// Get all unique result codes
$codes = $collection->getCodes();

use function AppUtils\operationCollection;

$collection = operationCollection();

// Count the total number of results
$all = $collection->countResults();
$errors = $collection->countErrors();
$warnings = $collection->countWarnings();   
$successes = $collection->countSuccesses();
$notices = $collection->countNotices();
    


use AppUtils\OperationResult;

/**
 * @method MyOperation getSubject() 
 */
class MyOperationResult extends OperationResult
{
    public function __construct(MyOperation $subject)
    {
        parent::__construct($subject);
    }
}