PHPackages                             hylianshield/validator - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. hylianshield/validator

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

hylianshield/validator
======================

Data validation

2.1.0(9y ago)532531Apache-2.0PHPPHP ^7.0

Since Jun 7Pushed 9y ago2 watchersCompare

[ Source](https://github.com/HylianShield/validator)[ Packagist](https://packagist.org/packages/hylianshield/validator)[ Docs](https://github.com/HylianShield/validator)[ RSS](/packages/hylianshield-validator/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (10)Dependencies (1)Versions (13)Used By (1)

HylianShield Validator
======================

[](#hylianshield-validator)

The HylianShield validator is a validation package, built to create a common validator interface.

Its current release is explicitly built with PHP7 in mind. When compared to its predecessor, the current version of the HylianShield validator package is stripped down to the bare essential needs for validation.

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

[](#installation)

`composer require hylianshield/validator:^2.1.0`

Configuration
-------------

[](#configuration)

For this package, there is no configuration to consider.

Validator
---------

[](#validator)

A validator consists of at least the following two methods:

### getIdentifier

[](#getidentifier)

```
public function getIdentifier(): string
```

This method is used to create a unique identifier for the validator. It is of use when debugging behavior of validators or identifying validators that have blocked out certain validation subjects.

### validate

[](#validate)

```
public function validate($subject): bool
```

The validate method accepts a validation subject and returns whether the subject is or is not valid.

Validator Collection
--------------------

[](#validator-collection)

Besides an interface for validators to implement, a validator collection interface is exposed.

This collection itself implements an interface that extends the validator interface. Therefore, a validator collection itself is a validator.

Besides the shared functionality of a validator, it has a method to add a validator, as well as a method to remove a validator.

If the `validate` method is called on a collection, it will validate against the validators inside the collection.

Depending on which of the following collections is used, the validation result will differ:

CollectionResultMatchAllCollectionReturns true when all internal validators return true.MatchNoneCollectionReturns true when all internal validators return false.MatchAnyCollectionReturns true when any internal validator returns true.Because a collection is a validator itself, it can accept another collection as a registered validator.

**Note:** To not negatively impact performance, there is no test against registering collections recursively. Because of this, when a collection is poorly built, it may render function nesting overflows.

Additionally, the collections keep nesting the formatting of identifiers. Given that a `MatchAnyCollection` holds validators with identifiers *Foo*and *Bar*, respectively, the identifier of the collection will be:

```
any(, )

```

If one would nest a level deeper, combined with `MatchAllCollection`, one can get the following:

```
any(, all(, ))

```

This particular validator will pass if the *Foo* validator passes, if both the *Bar* and *Baz* validators pass or if all three validators pass.

NotValidator
------------

[](#notvalidator)

When one wants to invert the validation of any given validator or collection, one can wrap it around a `NotValidator`.

```
/** ValidatorInterface $validator */
$notValidator = new NotValidator($validator);

$validator->validate('something'); // true
$notValidator->validate('something'); // false

$validator->validate('somethingElse'); // false
$notValidator->validate('somethingElse'); // true

echo $validator->getIdentifier(); // something
echo $notValidator->getIdentifier(); // not()
```

Anonymous validator
-------------------

[](#anonymous-validator)

As of PHP 7, PHP supports anonymous classes. One can easily create a custom validator without having to introduce a fully qualified class name for its validator.

```
use HylianShield\Validator\ValidatorInterface;
use Acme\User\UserManagerInterface;

$userValidator = new class($userManager) implements ValidatorInterface {
    /**
     * @var UserManagerInterface
     */
    protected $userManager;

    /**
     * Initialize a new user validator.
     *
     * @param UserManagerInterface $userManager
     */*
    public function __construct(UserManagerInterface $userManager)
    {
        $this->userManager = $userManager;
    }

    /**
     * Get the identifier for the validator.
     *
     * @return string
     */
    public function getIdentifier(): string
    {
        return 'user';
    }

    /**
     * Validate the given subject.
     *
     * @param mixed $subject
     * @return bool
     */
    public function validate($subject): bool
    {
        return $this->userManager->contains($subject);
    }
};
```

And with this you have created a validator that can assert if a given user exists.

You could even combine it with existing validators.

```
use HylianShield\Validator\Collection\MatchAllCollection;
use Acme\User\Role\RoleValidator;
use Acme\User\Role\AdminRole;

$adminValidator = new MatchAllCollection();
$adminValidator->addValidator($userValidator);
$adminValidator->addValidator(
    new RoleValidator(AdminRole::IDENTIFIER)
);

$adminValidator->validate($nonexistentUser); // false
$adminValidator->validate($normalUser);      // false
$adminValidator->validate($adminUser);       // true
echo $adminValidator->getIdentifier();       // all(, )
```

Invoker
-------

[](#invoker)

In previous versions of this package, one could pass a validator on to functions like `array_filter` and `array_map`.

To properly separate concerns in code and to keep the validator interface clean for implementations, this is now solved by a separate `Invoker` object.

One simply wraps the validator like so:

```
use HylianShield\Validator\Invoker;

$filtered = array_filter($input, new Invoker($validator));
```

This also works for validator collections.

See invoker code example under `examples/` for an implementation of the invoker.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 73.3% 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

Every ~79 days

Recently: every ~72 days

Total

11

Last Release

3573d ago

Major Versions

0.9.0 → 2.0.02016-07-25

PHP version history (2 changes)0.6.0PHP &gt;=5.3

2.0.0PHP ^7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/4f105dc2b106ecb7ac1819cf18ea0276730a95d04e11918e23a2bef60efc3bbd?d=identicon)[janmarten\_jongerius](/maintainers/janmarten_jongerius)

![](https://avatars.githubusercontent.com/u/843685?v=4)[CyberSecutor](/maintainers/CyberSecutor)[@CyberSecutor](https://github.com/CyberSecutor)

---

Top Contributors

[![CyberSecutor](https://avatars.githubusercontent.com/u/843685?v=4)](https://github.com/CyberSecutor "CyberSecutor (33 commits)")[![noesnaterse](https://avatars.githubusercontent.com/u/1178741?v=4)](https://github.com/noesnaterse "noesnaterse (9 commits)")[![a-schuurman](https://avatars.githubusercontent.com/u/7899502?v=4)](https://github.com/a-schuurman "a-schuurman (1 commits)")[![gitter-badger](https://avatars.githubusercontent.com/u/8518239?v=4)](https://github.com/gitter-badger "gitter-badger (1 commits)")[![MarkRedeman](https://avatars.githubusercontent.com/u/1114829?v=4)](https://github.com/MarkRedeman "MarkRedeman (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/hylianshield-validator/health.svg)

```
[![Health](https://phpackages.com/badges/hylianshield-validator/health.svg)](https://phpackages.com/packages/hylianshield-validator)
```

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

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