2017 © Pedro Peláez
 

library guardsman

Guard clause assertion library to enforce parameter preconditions

image

guardsman/guardsman

Guard clause assertion library to enforce parameter preconditions

  • Friday, July 15, 2016
  • by le6o
  • Repository
  • 2 Watchers
  • 4 Stars
  • 2,411 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Guardsman :guardsman:

travis, (*1)

A guard clause assertion library to enforce parameter preconditions., (*2)

This provides a framework free, simple, chainable api for method parameter validation that throws exceptions on failure., (*3)

It is not intended as a validation library for end user input. If this is your use case then you should try one of these validation libraries instead., (*4)

Installation

composer require guardsman/guardsman

Example Usage

public function rename($name) {
    \Guardsman\check($name)
        ->isString()
        ->isNotEmpty();

    …
}
public function setSeconds($seconds) {
    \Guardsman\check($seconds)
        ->isInteger()
        ->isGreaterThanOrEqualTo(0)
        ->isLessThan(60);

    …
}
public function changeStatus($status) {
    \Guardsman\check($status)
        ->isValueOf(self::validStatuses);

    …
}

Preconditions

Array

\Guardsman\check($subject)->isValueOf(array $array);
\Guardsman\check($subject)->isNotValueOf(array $array);
\Guardsman\check($subject)->isKeyOf(array $array);
\Guardsman\check($subject)->isNotKeyOf(array $array);

DateTime

Methods will first check that the subject is an instance of \DateTimeInterface, (*5)

\Guardsman\check($subject)->isBefore(\DateTimeInterface $limit);
\Guardsman\check($subject)->isBeforeOrEqualTo(\DateTimeInterface $limit);
\Guardsman\check($subject)->isAfter(\DateTimeInterface $limit);
\Guardsman\check($subject)->isAfterOrEqualTo(\DateTimeInterface $limit);

Empty

\Guardsman\check($subject)->isNotEmpty();

Number

Methods that accept a limit will first check that the subject is numeric. Limits will then be checked to ensure they are numeric and positive., (*6)

\Guardsman\check($subject)->isNumeric();
\Guardsman\check($subject)->isInteger();
\Guardsman\check($subject)->isFloat();
\Guardsman\check($subject)->isGreaterThan($limit);
\Guardsman\check($subject)->isGreaterThanOrEqualTo($limit);
\Guardsman\check($subject)->isLessThan($limit);
\Guardsman\check($subject)->isLessThanOrEqualTo($limit);
\Guardsman\check($subject)->isPositive();
\Guardsman\check($subject)->isNegative();

String

Methods that accept a limit will first check that the subject is a string and that the encoding matches mb_internal_encoding Limits will then be checked to ensure they are numeric and positive., (*7)

\Guardsman\check($subject)->isString();
\Guardsman\check($subject)->isShorterThan($limit);
\Guardsman\check($subject)->isShorterThanOrEqualTo($limit);
\Guardsman\check($subject)->isLongerThan($limit);
\Guardsman\check($subject)->isLongerThanOrEqualTo($limit);
\Guardsman\check($subject)->matchesRegex($pattern);

Extending Guardsman

Simply extend the Guardsman class with your own methods and create a check function under your namespace., (*8)

src\Your\Namespace\SuperGuard.php, (*9)

namespace Your\Namespace;

class SuperGuard extends \Guardsman\Guardsman
{
    public function isYourPreconditionMethod()
    {
        …
    }
}

src\Your\Namespace\check.php, (*10)

namespace Your\Namespace;

function check($subject)
{
    return new SuperGuard($subject);
}

composer.json, (*11)

    "autoload": {
        "files": ["src/Your/Namespace/check.php"]
    }

Usage:, (*12)

\Your\Namespace\check($subject)->isYourPreconditionMethod();

Show Thanks

If you find this useful then please show your thanks with a small donation., (*13)

The Versions

15/07 2016

dev-master

9999999-dev

Guard clause assertion library to enforce parameter preconditions

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • php >=5.5

 

The Development Requires

by Martin Ledgard

validation assert validate parameter assertion guard precondition constraint argument ascertain

15/07 2016

v1.2.0

1.2.0.0

Guard clause assertion library to enforce parameter preconditions

  Sources   Download

MIT

The Requires

  • php >=5.5
  • ext-mbstring *

 

The Development Requires

by Martin Ledgard

validation assert validate parameter assertion guard precondition constraint argument ascertain

28/03 2016

v1.1.0

1.1.0.0

Guard clause assertion library to enforce parameter preconditions

  Sources   Download

MIT

The Requires

  • php >=5.5
  • ext-mbstring *

 

The Development Requires

by Martin Ledgard

validation assert validate parameter assertion guard precondition constraint argument ascertain

31/01 2016

v1.0.0

1.0.0.0

Guard clause assertion library to enforce parameter preconditions

  Sources   Download

MIT

The Requires

  • php >=5.5
  • ext-mbstring *

 

The Development Requires

by Martin Ledgard

validation assert validate parameter assertion guard precondition constraint argument ascertain