PHPackages                             yalesov/arg-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. yalesov/arg-validator

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

yalesov/arg-validator
=====================

Validate function arguments, extending typehinting capabilities of PHP.

v2.1.1(9y ago)826.3k320ISCPHPPHP &gt;=5.3.3

Since Sep 6Pushed 9y ago3 watchersCompare

[ Source](https://github.com/yalesov/php-arg-validator)[ Packagist](https://packagist.org/packages/yalesov/arg-validator)[ Docs](https://github.com/yalesov/php-arg-validator)[ RSS](/packages/yalesov-arg-validator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (12)Used By (20)

Yalesov\\ArgValidator
=====================

[](#yalesovargvalidator)

[![Build Status](https://camo.githubusercontent.com/b05bdc11d56198f379a748f981d8e32e99e90280e166387bc7ad0fb51b1702c3/68747470733a2f2f7472617669732d63692e6f72672f79616c65736f762f7068702d6172672d76616c696461746f722e737667)](https://travis-ci.org/yalesov/php-arg-validator)

Solving several function argument validation/typehinting issues in PHP:

Primitive typehints:

```
function foo(string $foo) {}
```

Mixed / advanced typehints:

```
function foo(array|string|null $foo, int/*between 3-10*/ $bar) {}
```

"array of X" typehints:

```
function foo(Array_of_strings $foo) {}
```

Validating / typehinting "pseudo named function paramters" through array members:

```
/**
 * @param array $params
 *  - 'foo' => string
 *  - 'bar' => int
 */
function foo(array $params) {}
```

Declaring required constants in classes (interface-style function):

```
class Foo {
  /* const BAR required */
  /* const BAZ required */
}
```

Installation
============

[](#installation)

[Composer](http://getcomposer.org/):

```
{
  "require": {
    "yalesov/arg-validator": "2.*"
  }
}
```

Usage
=====

[](#usage)

Simple argument validation
--------------------------

[](#simple-argument-validation)

Validates that `$foo` is an integer, throwing an `InvalidArgumentException` if validation failed.

```
use Yalesov\ArgValidator\ArgValidator;

function foo($foo)
{
  ArgValidator::assert($foo, 'int');
  // throw InvalidArgumentException if $foo is not int
  // do something
}
```

Validates that `$foo` is an integer, return boolean.

```
use Yalesov\ArgValidator\ArgValidator;

function foo($foo)
{
  $result = ArgValidator::assert($foo, 'int', false);
  // $result = false if invalid
  // do something
}
```

Full function signature:

```
public static function assert($arg, $checks, $exception = true)
```

Valid argument types are specified through the `$checks` parameter. A string, or an array of the following accepted. `$arg` will be considered valid if it satisfies *any one* of the specified checks.

- Flags
    - `arrayof`: will check for an array of remaining specified types, instead of plain types, e.g. `array('arrayOf', 'string', 'int')` = an array of string, or an array of int. *Note: Empty array will be considered valid*
    - `min`, `max`
        - combine with `int`, `float`: min and max value
        - combine with `string`: min and max length
        - combine with `array`: min and max count
- Types
    - `int`
    - `float`
    - `numeric`
    - `string`
    - `array`
    - `null`
    - `callable`
    - `bool`
    - `resource`
    - `notEmpty`: equivalent to `!empty()`
    - (an array of scalars for in\_array check), e.g. `array('foo', 'bar')` will check for `in_array($arg, array('foo', 'bar'))`. *Note: `ArgValidator::assert($arg, array('foo', 'bar'))` will be interpreted as instanceof checks against `foo` and `bar`. To specify an in\_array check, wrap it in another array: `ArgValidator::assert($arg, array(array('foo', 'bar')))`.*
    - (a string): assumed to be an instanceof check, should be a fully-qualified name of Class/Interface

"Named parameters" validation
-----------------------------

[](#named-parameters-validation)

```
use Yalesov\ArgValidator\ArgValidator;

function foo(array $params)
{
  ArgValidator::arrayAssert($params, array(
    'foo' => 'float',
    'bar' => array('string', 'notSet'),
    'baz' => array('int', 'string', 'min' => 2),
  ));
  // $params['foo'] should be a float
  // $params['bar'] should be a string, if set, or not set at all
  // $params['baz'] can be either an int (>=2), or a string (strlen >= 2)
}
```

Full function signature:

```
public static function arrayAssert(array $arg, array $checks, $exception = true)
```

Valid argument types are same as above, except with the addition of a `notSet` type.

Check class constants
---------------------

[](#check-class-constants)

```
namespace Foo;

use Yalesov\ArgValidator\ArgValidator;

class FooClass {
  const FOO = 'foo';
  const BAR = 2;
}

ArgValidator::assertClassConstant('Foo\FooClass', array('FOO', 'BAR'));
// \Foo\FooClass must have the constants 'FOO' and 'BAR' set
```

Full function signature:

```
public static function assertClassConstant($className, $constants, $exception = true)
```

`$className` should be a fully-qualified class name; `$constants` should be an array of strings, each member being the constant name.

`ArgValidator::assertClassConstant()` will check for:

1. the class `$className` exists
2. the class has declared the required constants specified in `$constants`

Misc
----

[](#misc)

To centralize exception handling on argument validations, ArgValidator also provides two helper functions:

Assert that a class exists, throw `InvalidArgumentException` otherwise:

```
public static function assertClass($className, $exception = true)
```

Throw an `InvalidArgumentException` about the given variable name is not set:

```
public static function throwIssetException($argName)
```

*Note: this function doesn't actually perform the `isset` check. I can't find a way to abstract the `isset` check away, without the variable being set in the first place (in order to act as argument to call this function with).*

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community24

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 90% 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 ~140 days

Recently: every ~0 days

Total

11

Last Release

3594d ago

Major Versions

1.1.3 → v2.0.02016-07-05

### Community

Maintainers

![](https://www.gravatar.com/avatar/9c284282a506ac3c908eda03eb11cc263a4457f2e3cc23cbe3e9b97830589e31?d=identicon)[yalesov](/maintainers/yalesov)

---

Top Contributors

[![yalesov](https://avatars.githubusercontent.com/u/20264293?v=4)](https://github.com/yalesov "yalesov (9 commits)")[![EugeniyPetrov](https://avatars.githubusercontent.com/u/2155621?v=4)](https://github.com/EugeniyPetrov "EugeniyPetrov (1 commits)")

---

Tags

validatortypehintvalidateargumenttypehinting

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[wixel/gump

A fast, extensible &amp; stand-alone PHP input validation class that allows you to validate any data.

1.2k1.3M29](/packages/wixel-gump)[sadegh19b/laravel-persian-validation

A comprehensive Laravel validation package for Persian text, numbers, dates, and Iranian national identifiers

18293.8k1](/packages/sadegh19b-laravel-persian-validation)[awurth/slim-validation

A wrapper around the respect/validation PHP validation library for easier error handling and display

65378.4k9](/packages/awurth-slim-validation)[romeoz/rock-validate

Flexible validator for PHP with I18N.

251.7k6](/packages/romeoz-rock-validate)

PHPackages © 2026

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