2017 © Pedro Peláez
 

library type-enforcement

Enforce types and throw InvalidArgumentExceptions upon invalid types.

image

joshbrw/type-enforcement

Enforce types and throw InvalidArgumentExceptions upon invalid types.

  • Thursday, February 1, 2018
  • by joshbrw
  • Repository
  • 1 Watchers
  • 0 Stars
  • 26 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 4 % Grown

The README.md

Type Enforcement

If you've ever come across a time where you're writing loosely-coupled code but want to check a parameters type at the start of a method, then this library is for you., (*1)

How you're probably doing it

class UserRegistrar {
    public function register(array $usersDetails, $author)
    {
        if (!$author instanceof SomeAuthor) {
            throw new \InvalidArgumentException('The author must be an instance of SomeAuthor');
        }
    }
}

Let's refactor!

use Joshbrw\TypeEnforcement\Type;

class UserRegistrar {
    public function register(array $usersDetails, $author)
    {
        /* Throws \InvalidArgumentException on invalid input */
        Type::enforce($author, SomeAuthor::class);
    }
}

Custom Exception Messages

By default the package will provide a useful exception message, stating which type is expected and which type has been provided, i.e:, (*2)

Expected [Tests\NonExistentClass], [array '["array"]'] provided.

This exception message can be switched out by providing a custom message as the third parameter, i.e.:, (*3)

Type::enforce($variable, Type::class, 'The variable must be a Type!');

Not a fan of static method calls?

This package also includes a helper method that accepts the same parameters:, (*4)

enforce_type($variable, Type::class, 'The variable must be a Type!');

The Versions

01/02 2018

dev-master

9999999-dev

Enforce types and throw InvalidArgumentExceptions upon invalid types.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Josh Brown

01/02 2018

v0.1.2

0.1.2.0

Enforce types and throw InvalidArgumentExceptions upon invalid types.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Josh Brown

01/02 2018

v0.1.1

0.1.1.0

Throw InvalidArgumentExceptions when a Type isn't provided

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Josh Brown

01/02 2018

v0.1

0.1.0.0

Throw InvalidArgumentExceptions when a Type isn't provided

  Sources   Download

The Requires

  • php >=7.0

 

The Development Requires

by Josh Brown