The Simple Result Type simply returns the processing result as an object.

Overview

The Simple Result Type

Testing PHPStan Validate Composer

The Simple Result Type simply returns the processing result as an object.
Enjoy!

Example

This is a basic usage example.

use Takemo101\SimpleResultType\ {
    Resulter,
    Error,
    Success,
};
use Exception;

// Create a Success object with the Success method of the Resulter class.
$data = Resulter::success(10)
    // Create a Result object with a new value by the map method.
    ->map(fn(int $result) => $result * 2)
    // Get the success result by the success method.
    ->success();

var_dump($data); // int(20)


// Create an Error object with the error method of the Resulter class.
$data = Resulter::error(10)
    // Create an Error object with a new value by the mapError method.
    ->mapError(fn(int $result) => $result * 2)
    // Get the error result by the error method.
    ->error();

var_dump($data); // int(20)


// If you generate an Error with a value that implements Throwable, 
// an exception will be raised.
Resulter::error(new Exception('error'))
    ->exception();


// You can get the output according to the result by the output method.
$data = Resulter::success(10)
    ->map(fn(int $result) => $result * 2)
    ->output(
        success: fn(int $result) => $result * 100,
        error: fn(int $result) => $result * 1,
    );

var_dump($data); // int(2000)

This is an example of using to determine whether the result is success or error.

use Takemo101\SimpleResultType\ {
    Error,
    Success,
};

// You can also create objects from the Error and Success classes.
$result = Error::create('error');

var_dump($result->isError()); // bool(true)
var_dump($result->isSuccess()); // bool(false)


$result = Success::create('success');

// You can also judge by Type enum.
$data = match ($result->type()) {
    Type::Success => $result->success(),
    Type::Error => $result->error(),
};

var_dump($data); // string(7) "success"

Try operations that may fail and produce results.

use Takemo101\SimpleResultType\Resulter;
use Takemo101\SimpleResultType\Support\ {
    CatchType,
    NotCatchType,
};
use Exception;
use LogicException;
use RuntimeException;
use InvalidArgumentException;

// If an exception occurs, the result will be returned as Error.
$result = Resulter::trial(function() {
    throw new Exception('error');
}); // Error<Exception>

// By returning the success value, the result will be returned as Success.
$result = Resulter::trial(function() {
    return 10;
}); // Success<integer>

// No error is output except for the exception specified in the CatchType Attribute class.
$result = Resulter::trial(
    #[CatchType(
        RuntimeException::class,
        InvalidArgumentException::class,
    )]
    function() {
        throw new RuntimeException('error');
    }
); // Error<RuntimeException>

var_dump($result->isError()); // bool(true)

// No error is output for the exception specified in the NotCatchType Attribute class.
Resulter::trial(
    #[NotCatchType(
        RuntimeException::class,
        InvalidArgumentException::class,
    )]
    function() {
        throw new RuntimeException('error');
    }
); // Exception occurs.
You might also like...
PHP Text Analysis is a library for performing Information Retrieval (IR) and Natural Language Processing (NLP) tasks using the PHP language

php-text-analysis PHP Text Analysis is a library for performing Information Retrieval (IR) and Natural Language Processing (NLP) tasks using the PHP l

iOrder is a light weight prototype for a order processing MIS.

Order Processing MIS. iOrder is a light weight prototype for a order processing MIS. Features Centralized order management Merchants definitely benefi

PHP 7+ Payment processing library. It offers everything you need to work with payments: Credit card & offsite purchasing, subscriptions, payouts etc. - provided by Forma-Pro

Supporting Payum Payum is an MIT-licensed open source project with its ongoing development made possible entirely by the support of community and our

Alipay driver for the Omnipay PHP payment processing library

Omnipay: Alipay Alipay driver for the Omnipay PHP payment processing library Omnipay is a framework agnostic, multi-gateway payment processing library

UnionPay driver for the Omnipay PHP payment processing library

Omnipay: UnionPay UnionPay driver for the Omnipay PHP payment processing library Omnipay is a framework agnostic, multi-gateway payment processing lib

First Data driver for the Omnipay PHP payment processing library

Omnipay: First Data First Data driver for the Omnipay PHP payment processing library Omnipay is a framework agnostic, multi-gateway payment processing

Melek Berita Backend is a service for crawling data from various websites and processing the data to be used for news data needs.

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

A collection of type-safe functional data structures

lamphpda A collection of type-safe functional data structures Aim The aim of this library is to provide a collection of functional data structures in

A package for adding more type safety to your PHP projects.
A package for adding more type safety to your PHP projects.

Table of Contents Overview Installation Usage Simple Checks Advanced Checks Custom Checks Skipping Checks Testing Security Contribution Credits Change

Comments
  • Develop

    Develop

    changes

    1. added flatMapError method to Result interface.
    2. added successOr and errorOr method to Result interface.
    3. Added CatchType attribute class to Resulter::trial method
    opened by takemo101 0
Releases(v0.1.4)
Owner
null
This Validate Class is for those who are looking for a validator that returns a code for every each error (Laravel/Api)

Validator-Class This Validate Class is for those who are looking for a validator that returns a code for every each error (Laravel/Api) Requirements A

Barbod 3 Jul 18, 2022
The main scope of this extension is to help phpstan to detect the type of object after the Assert\Assertion validation.

PHPStan beberlei/assert extension PHPStan beberlei/assert Description The main scope of this extension is to help phpstan to detect the type of object

PHPStan 33 Jan 2, 2023
Simply removes the applcation's front-end and redirects it to the admin area.

Simply removes the application's front-end and redirects it to the admin area.

Albright Labs 1 Mar 28, 2022
Result of our code-along meetup writing PHP 8.1 code

PHP 8.1 Demo Code This code demonstrates various PHP 8.0 and 8.1 features in a realistic, functional (but incomplete) codebase. The code is part of so

azPHP 2 Nov 14, 2021
MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query and get result in a fastest way

Mysql Optimizer mysql optimizer also known as MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query

null 2 Nov 20, 2021
An article about alternative solution for convert object into a JSON Object for your api.

Do we really need a serializer for our JSON API? The last years I did build a lot of JSON APIs but personally was never happy about the magic of using

Alexander Schranz 1 Feb 1, 2022
Your alter ego object. Takes the best of object and array worlds.

Supporting Opensource formapro\values is an MIT-licensed open source project with its ongoing development made possible entirely by the support of com

FormaPro 31 Jun 25, 2021
A simple, type-safe, zero dependency port of the javascript fetch WebApi for PHP.

A simple, type-safe, zero dependency port of the javascript fetch WebApi for PHP.

Matias Navarro Carter 105 Jan 4, 2023
JsonQ is a simple, elegant PHP package to Query over any type of JSON Data

php-jsonq JsonQ is a simple, elegant PHP package to Query over any type of JSON Data. It'll make your life easier by giving the flavour of an ORM-like

Nahid Bin Azhar 834 Dec 25, 2022
A framework agnostic, multi-gateway payment processing library for PHP 5.6+

Omnipay An easy to use, consistent payment processing library for PHP Omnipay is a payment processing library for PHP. It has been designed based on i

The League of Extraordinary Packages 5.7k Dec 30, 2022