PHPackages                             tconway1/mattyrad-support - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. tconway1/mattyrad-support

ActiveLibrary

tconway1/mattyrad-support
=========================

A collection of PHP support classes and traits

v1.0.0(2y ago)0183MITPHPPHP &gt;=5.6

Since Feb 7Pushed 2y ago1 watchersCompare

[ Source](https://github.com/tconway1/mattyrad-support)[ Packagist](https://packagist.org/packages/tconway1/mattyrad-support)[ RSS](/packages/tconway1-mattyrad-support/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (3)Used By (0)

MattyRad Support
================

[](#mattyrad-support)

[![Build Status](https://camo.githubusercontent.com/9f14cec41d515245fc8bdcf3cc402063dd97b67be166dca61ba08d46450d0b40/68747470733a2f2f6170692e7472617669732d63692e6f72672f4d617474795261642f737570706f72742e706e673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/9f14cec41d515245fc8bdcf3cc402063dd97b67be166dca61ba08d46450d0b40/68747470733a2f2f6170692e7472617669732d63692e6f72672f4d617474795261642f737570706f72742e706e673f6272616e63683d6d6173746572) [![Code Coverage](https://camo.githubusercontent.com/98f7087e7db3b88f7d8388f660a57beb46c764fd5fe71fb6d17e5355ed0ff6f9/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6d617474797261642f737570706f72742e737667)](https://camo.githubusercontent.com/98f7087e7db3b88f7d8388f660a57beb46c764fd5fe71fb6d17e5355ed0ff6f9/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6d617474797261642f737570706f72742e737667)

Installation
------------

[](#installation)

`composer require mattyrad/support`

Table of Contents
-----------------

[](#table-of-contents)

- [Conformation Trait](#conformation-trait)
- [Result Objects](#result-objects)

### Conformation Trait

[](#conformation-trait)

#### Instantiate objects from an unsorted array

[](#instantiate-objects-from-an-unsorted-array)

```
use MattyRad\Support\Conformation;

class Sample {
    use Conformation;

    private $arg1;
    private $arg2;
    private $arg3;
    private $arg4;
    private $optional1;
    private $optional2;

    private function __construct(
        string $arg1,
        int $arg2,
        bool $arg3,
        float $arg4,
        string $optional1 = '',
        int $optional2 = 1
    ) {
        $this->arg1 = $arg1;
        $this->arg2 = $arg2;
        $this->arg3 = $arg3;
        $this->arg4 = $arg4;
        $this->optional1 = $optional1;
        $this->optional2 = $optional2;
    }
}
```

```
$sample = Sample::fromArray([
    'optional2' => 777,
    'arg2' => 1,
    'arg3' => false,
    'arg1' => 'example',
    'arg4' => 2.0,
]);
```

Failing to provide all the required arguments will throw an Exception

```
$sample = Sample::fromArray([
    'arg1' => 'example',
    'arg2' => 1,
]);
```

`PHP Fatal error:  Uncaught InvalidArgumentException: Sample missing key(s): arg3, arg4`

### Result Objects

[](#result-objects)

It's very common to require extensible result objects for success and failures, particularly for APIs.

#### Defining Results

[](#defining-results)

You can hit the ground running with generic success results

```
$json_response_data = ['user' => ['name' => 'John', 'email' => 'user@example.com', 'posts' => [['name' => 'A'], ['name' => 'B']]]];

$result = new \MattyRad\Support\Result\Success($json_response_data);

$result->isSuccess(); // true
$result->isFailure(); // false
$result->get('user.email'); // dot syntax enabled
$result->get('user.posts.*.name'); // wildcard enabled, ['A', 'B']
```

For more precision, you can extend the Success result

```
class WidgetPurchased extends Result\Success
{
    public function __construct(Widget $widget)
    {
        $this->widget = $widget;
    }

    public function getWidget(): Widget
    {
        return $this->widget;
    }
}

$result = new Result\Success\WidgetPurchased($widget);

$result->getWidget(); // Widget object
$result->isSuccess(); // true
$result->isFailure(); // false
```

Failure results are required to be a bit more specific

```
namespace MattyRad\Support\Result\Stripe;

use MattyRad\Support\Result;

class ChargeFailed extends Result\Failure
{
    protected static $message = 'Stripe charge failed, delinquent card';

    public function __construct($last_four_digits)
    {
        $this->last_four_digits = $last_four_digits;
    }

    public function getContext()
    {
        return ['last_four_digits' => $this->last_four_digits];
    }
}

$result->isSuccess(); // false
$result->isFailure(); // true
$result->get('widget.name'); // throws exception with message 'Stripe charge failed, delinquent card'
$result->getWidget(); // also throws exception with message 'Stripe charge failed, delinquent card'
$result->getMessage(); // 'Stripe charge failed, delinquent card'
$result->getContext(); // ['last_four_digits' => '1234']
$result->getReason(); // 'Stripe charge failed, delinquent card; {"last_four_digits":"1234"}'
```

#### Instantiating and Returning Results

[](#instantiating-and-returning-results)

```
use MattyRad\Support\Result;

function purchaseWidget($user, string $widget_name): Result
{
    if ($existing_widget = $this->db->getWidgetByName($widget_name)) {
        return new Result\Failure\WidgetExists($existing_widget);
    }

    try {
        $user->charge(100); // API call, this could be any interface to stripe
    } catch (\Stripe\Error\Card $e) {
        return new Result\Failure\Stripe\ChargeFailed($e->getLastFour()); // pretend that getLastFour exists
    }

    $widget = new Widget($widget_name);

    return new Result\Success\WidgetPurchased($widget);
}
```

#### Consuming Results

[](#consuming-results)

You can check for a failure manually

```
$result = purchaseWidget(Auth::user(), 'my_cool_new_widget');

if ($result->isFailure()) {
    return new JsonResponse(['error' => $result->getReason()], 422);
}

return new JsonResponse($result->getWidget());
```

Alternatively, you can leverage Exceptions and cut out the success/failure checks. Attempting to access data from a Failure Result will cause it to throw an Exception

```
$result = purchaseWidget(Auth::user(), 'my_cool_new_widget'); // returns ChargeFailed

return new Response($result->getWidget()); // Throws exception
```

Laravel's Exception Handler handles a number of exceptions by default (like HttpResponseException), which we can use to our advantage by overriding the default toException function of the Failure Result

```
class ChargeFailed extends Result\Failure
{
    // ...

    public function toException()
    {
        $response = new JsonResponse(['error' => static::$message], 422);

        return new HttpResponseException($response);
    }
```

And error handling will be built in, we can focus on the success path. Any non-built in exceptions can get caught in the render() function of the Exception Handler.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

822d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/87d695e5f5b9cbba3551742b2b31963759c9072d5a5164899ace69972d56fb30?d=identicon)[tconway1](/maintainers/tconway1)

---

Top Contributors

[![tconway1](https://avatars.githubusercontent.com/u/56689274?v=4)](https://github.com/tconway1 "tconway1 (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/tconway1-mattyrad-support/health.svg)

```
[![Health](https://phpackages.com/badges/tconway1-mattyrad-support/health.svg)](https://phpackages.com/packages/tconway1-mattyrad-support)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
