PHPackages                             aegisora/in-array-rule-guardian - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. aegisora/in-array-rule-guardian

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

aegisora/in-array-rule-guardian
===============================

A simple shortcut for in-array value check validation, built on top of aegisora/guardian and aegisora/in-array-rule.

v1.0.0(yesterday)01↑2900%MITPHP &gt;=7.4

Since Jul 22Compare

[ Source](https://github.com/Aegisora/in-array-rule-guardian)[ Packagist](https://packagist.org/packages/aegisora/in-array-rule-guardian)[ Docs](https://github.com/Aegisora/in-array-rule-guardian)[ RSS](/packages/aegisora-in-array-rule-guardian/feed)WikiDiscussions Synced today

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

Aegisora In-Array Rule Guardian
===============================

[](#aegisora-in-array-rule-guardian)

[![Latest Version](https://camo.githubusercontent.com/e04d619f546eafd8bdafc17e35936c102256650603c3d63390685816bd946dc2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f61656769736f72612f696e2d61727261792d72756c652d677561726469616e3f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aegisora/in-array-rule-guardian)[![Total Downloads](https://camo.githubusercontent.com/bc15b4a46196bb7f227e9d9d9c4c3f862f03acc69e04f7076f2953640990e7e1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f61656769736f72612f696e2d61727261792d72756c652d677561726469616e3f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aegisora/in-array-rule-guardian)[![Code Coverage Badge](./badge.svg)](./badge.svg)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![PHPStan Badge](https://camo.githubusercontent.com/83dd3d35cebed0eab9ee97ff1a5849c1344cda6a8ee9cac2cda20f5aa55b67bd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230392d627269676874677265656e2e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/83dd3d35cebed0eab9ee97ff1a5849c1344cda6a8ee9cac2cda20f5aa55b67bd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230392d627269676874677265656e2e7376673f7374796c653d666c6174)

In-Array Rule Guardian provides a simple shortcut for in-array value validation using `aegisora/guardian` and `aegisora/in-array-rule`.

It is designed for cases where you want to quickly check whether a value is contained in a given array without manually creating validation pipelines.

This package is built on top of:

- [aegisora/guardian](https://github.com/Aegisora/guardian)
- [aegisora/in-array-rule](https://github.com/Aegisora/in-array-rule)

---

✨ Features
----------

[](#-features)

- 🔹 Simple shortcut API for `InArrayRule`
- 🔹 Validates whether a value is present in an array
- 🔹 Supports both strict and soft (loose) comparison
- 🔹 Uses `aegisora/guardian` internally
- 🔹 Uses `aegisora/in-array-rule` internally
- 🔹 Supports custom validation exceptions
- 🔹 Keeps rule execution errors separated from validation errors
- 🔹 Fully compatible with the Aegisora ecosystem
- 🔹 Ready to use out of the box

---

📦 Installation
--------------

[](#-installation)

```
composer require aegisora/in-array-rule-guardian
```

---

🚀 Core Concept
--------------

[](#-core-concept)

This package wraps the common validation flow:

```
$guardian->check($value, InArrayRule::createStrict($actualArray), new NotInArrayException());
```

into a dedicated shortcut class:

```
$inArrayRuleGuardian->checkStrict($value, $actualArray, new NotInArrayException());
```

Instead of manually creating `InArrayRule` and passing it to `Guardian`, you can use `InArrayRuleGuardian` directly.

---

🏗️ Basic Usage
--------------

[](#️-basic-usage)

```
use Aegisora\Guardian\Guardian;
use Aegisora\Guardian\Exceptions\GuardianValidationException;
use Aegisora\RuleGuardians\InArrayRule\InArrayRuleGuardian;

$guardian = new Guardian();

$inArrayRuleGuardian = new InArrayRuleGuardian($guardian);

try {
    $inArrayRuleGuardian->checkStrict(2, [1, 2, 3]);
    // value is present in the array
} catch (GuardianValidationException $exception) {
    // value is not present in the array
}
```

---

⚖️ Strict vs Soft Comparison
----------------------------

[](#️-strict-vs-soft-comparison)

The package exposes two methods that differ only in how values are compared.

### `checkStrict()`

[](#checkstrict)

Uses strict comparison (`===`). Both the type and the value must match.

```
$inArrayRuleGuardian->checkStrict('2', [1, 2, 3]); // fails: '2' !== 2
$inArrayRuleGuardian->checkStrict(2, [1, 2, 3]);   // passes
```

### `checkSoft()`

[](#checksoft)

Uses loose comparison (`==`). Only the value must match after type juggling.

```
$inArrayRuleGuardian->checkSoft('2', [1, 2, 3]); // passes: '2' == 2
$inArrayRuleGuardian->checkSoft(0, ['0']);       // passes: 0 == '0'
```

Both methods share the same signature and behaviour regarding exceptions — only the comparison mode changes.

---

🧩 Usage with Custom Exception
-----------------------------

[](#-usage-with-custom-exception)

You may provide your own exception for validation failure.

```
use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\InArrayRule\InArrayRuleGuardian;
use App\Exceptions\NotInArrayException;

$guardian = new Guardian();

$inArrayRuleGuardian = new InArrayRuleGuardian($guardian);

$inArrayRuleGuardian->checkStrict(4, [1, 2, 3], new NotInArrayException());
```

If the value is not present in the array, the provided exception will be thrown.

This is useful when validation errors should have domain-specific meaning.

---

🧪 Example in Application Service
--------------------------------

[](#-example-in-application-service)

```
use Aegisora\RuleGuardians\InArrayRule\InArrayRuleGuardian;
use App\Exceptions\InvalidStatusException;

final class OrderService
{
    private const ALLOWED_STATUSES = ['pending', 'paid', 'shipped', 'cancelled'];

    private InArrayRuleGuardian $inArrayRuleGuardian;

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

    /**
     * @param mixed $status
     */
    public function updateStatus($status): void
    {
        $this->inArrayRuleGuardian->checkStrict($status, self::ALLOWED_STATUSES, new InvalidStatusException());

        // business logic for a valid status
    }
}
```

---

🚨 Exceptions
------------

[](#-exceptions)

This package does not define its own exception types. It delegates execution to `Guardian` and re-throws the exceptions raised by the underlying pipeline.

### `GuardianValidationException`

[](#guardianvalidationexception)

Thrown when validation fails and no custom exception is provided.

The rule code for failed in-array validation is `in_array_rule`.

```
use Aegisora\Guardian\Exceptions\GuardianValidationException;

try {
    $inArrayRuleGuardian->checkStrict(4, [1, 2, 3]);
} catch (GuardianValidationException $exception) {
    echo $exception->getRuleCode(); // "in_array_rule"
}
```

### Custom exception

[](#custom-exception)

When a custom exception is passed as the last argument, it is thrown instead of `GuardianValidationException` on validation failure.

```
use App\Exceptions\NotInArrayException;

try {
    $inArrayRuleGuardian->checkStrict(4, [1, 2, 3], new NotInArrayException());
} catch (NotInArrayException $exception) {
    // domain-specific handling
}
```

### `GuardianExecutingRuleException`

[](#guardianexecutingruleexception)

Thrown when the underlying rule execution fails.

```
use Aegisora\Guardian\Exceptions\GuardianExecutingRuleException;

try {
    $inArrayRuleGuardian->checkStrict($value, $actualArray);
} catch (GuardianExecutingRuleException $exception) {
    // the rule could not be executed
}
```

---

🧩 API
-----

[](#-api)

### `InArrayRuleGuardian::checkStrict()`

[](#inarrayruleguardiancheckstrict)

```
/**
 * @param mixed $value
 * @param mixed[] $actualArray
 * @throws GuardianExecutingRuleException
 * @throws GuardianValidationException
 * @throws \Throwable
 */
public function checkStrict(
    $value,
    array $actualArray,
    ?\Throwable $exception = null
): void
```

Validates that `$value` is present in `$actualArray` using strict comparison (`===`).

### `InArrayRuleGuardian::checkSoft()`

[](#inarrayruleguardianchecksoft)

```
/**
 * @param mixed $value
 * @param mixed[] $actualArray
 * @throws GuardianExecutingRuleException
 * @throws GuardianValidationException
 * @throws \Throwable
 */
public function checkSoft(
    $value,
    array $actualArray,
    ?\Throwable $exception = null
): void
```

Validates that `$value` is present in `$actualArray` using loose comparison (`==`).

Parameters (both methods):

- `$value` *(mixed)* — value to look for in the array
- `$actualArray` *(mixed\[\])* — array of allowed values (the haystack)
- `$exception` *(?\\Throwable, default `null`)* — optional custom exception thrown on validation failure

Both methods return `void`. They communicate results through exceptions only — they return nothing on success and throw on failure:

- `GuardianValidationException` — validation failed and no custom exception was provided
- the provided custom exception — validation failed and a custom exception was passed
- `GuardianExecutingRuleException` — the underlying rule failed to execute

Example:

```
$inArrayRuleGuardian->checkStrict(2, [1, 2, 3]);
```

With custom exception:

```
$inArrayRuleGuardian->checkSoft('2', [1, 2, 3], new NotInArrayException());
```

---

🏛️ Architecture
---------------

[](#️-architecture)

This package is a small shortcut layer over the Aegisora validation pipeline.

Flow:

1. `InArrayRuleGuardian::checkStrict()` / `checkSoft()` is called
2. `InArrayRule::createStrict()` / `InArrayRule::createSoft()` is created
3. `Guardian` executes the rule
4. If validation succeeds, execution continues normally
5. If validation fails, the custom exception or `GuardianValidationException` is thrown
6. If rule execution fails, `GuardianExecutingRuleException` is thrown

Internal flow:

```
Value → InArrayRuleGuardian → Guardian → InArrayRule → Result → Exception

```

---

🔗 Related Packages
------------------

[](#-related-packages)

- [aegisora/guardian](https://github.com/Aegisora/guardian) — validation execution orchestrator
- [aegisora/in-array-rule](https://github.com/Aegisora/in-array-rule) — rule-based in-array validation
- [aegisora/rule-contract](https://github.com/Aegisora/rule-contract) — base rule contract and validation result architecture

---

⚖️ License
----------

[](#️-license)

This package is open-source and licensed under the MIT License. See the LICENSE for details.

---

🌱 Contributing
--------------

[](#-contributing)

Contributions are welcome and greatly appreciated!. See the CONTRIBUTING for details.

---

🌟 Support
---------

[](#-support)

If you find this project useful, please consider giving it a star on GitHub!

It helps the project grow and motivates further development.

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance100

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

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

1d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/81402dcd0a07ad550b7f80f5871e7c302770b29d4c73a52fc35ba697f702d56e?d=identicon)[arslanim](/maintainers/arslanim)

---

Tags

phpvalidationarrayruleguardianaegisoraaegisora-ecosystemin-array-validationin-array-rulerule-guardian

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/aegisora-in-array-rule-guardian/health.svg)

```
[![Health](https://phpackages.com/badges/aegisora-in-array-rule-guardian/health.svg)](https://phpackages.com/packages/aegisora-in-array-rule-guardian)
```

PHPackages © 2026

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