Giter VIP home page Giter VIP logo

inspector's Introduction

Inspector

inspector [inˈspektər] - noun - an official employed to ensure that official regulations are obeyed.

Inspector is your validations handler for PHP applications. Inspector is easy to use and easy to learn and imposes rules on the payload supplied.

Validaton examples

Valid payload

<?php
// Example payload that is
$data = array(
  "username" => "simon",
  "email" => "[email protected]",
  "password" => "secret",
  "password_confirmation" => "secret"
);

// Create a new instance of inspector
$inspector = new Inspector($data);

// Ensure that the data is valid
$inspector->ensure("username")
  ->isAlpha("is not only alpha characters")
  ->isMin(3, "is to short")
  ->isMax(10, "is to long");

$inspector->ensure("email")
  ->isValidEmail("is not valid")
  ->notNull("is empty");

$inspector->ensure("password")
  ->isSame("password_confirmation", "don't correspond")
  ->isMin(6, "is to short");

// Check if any errors exist
echo $inspector->hasErrors(); // Returns false

// Throw a exception if there are errors
$inspector->validate(); // Returns false

Invalid payload

<?php
// Example payload that is valid
$data = array(
  "username" => "s",
  "email" => "simon@--",
  "password" => "s",
  "password_confirmation" => "b"
);

// Create a new instance of inspector
$inspector = new Inspector($data);

// Ensure that the data is valid
$inspector->ensure("username")
  ->isAlpha("is not only alpha characters")
  ->isMin(3, "is to short")
  ->isMax(10, "is to long");

$inspector->ensure("email")
  ->isValidEmail("is not valid")
  ->notNull("can not be empty");

$inspector->ensure("password")
  ->isSame("password_confirmation", "don't correspond")
  ->isMin(6, "is to short");

echo $inspector->hasErrors(); // Returns true

print_r($inspector->errors());

/*
Array
(
  [username] => Array
    (
      [0] => is to short
    )
  [email] => Array
    (
      [0] => is not valid
    )
  [password] => Array
    (
      [0] => don't correspond
      [1] => is to short
    )
)
*/

// If you like you can throw an InspectorException

$inspector->validate(); // InspectorException

Available validations

Inspector comes loaded with a few default validators.

isNull() / notNull()                     - Check if payload is null
isMax($int) / notMax($int)               - Check length of string
isMin($int) / notMin($int)               - Check length of string
isFloat() / notFloat()                   - Check if payload is a float
isInt() / notInt()                       - Check if payload is a int
isUrl() / notUrl()                       - Check if payload is a URL
isEmail() / notEmail()                   - Check if payload is a e-mail
isIp() / notIp()                         - Check if payload is a valid ip
isAlnum() / notAlnum()                   - Check if payload is alphanumeric
contains($needle) / notContains($needle) - Check if $needle exists in payload
isSameAs($needle) / notSameAs($needle)   - Check if $needle is exactly the same as payload
isRegex($regex) / notRegex($regex)       - Check if $regex matches payload
isChars($chars) / notChars($chars)       - Check if $chars exist inside string

Add your own validator

It's easy to add your own validators to Inspector. Below I added a validator to check if the payload contains ninja. You don't need to specify is/not when you add validators, they gets added magically. Also notice the number of arguments, $str is the string getting tested. The error message argument is added automagically. There is a small ceavat when doing it this way. You can't create validators with optional arguments.

<?php

Inspector::addValidator("ninja", function($str) {
  return strpos($str, "ninja") !== false;
});

$inspector = new Inspector(array(
  "name" => "Inspector 'ninja' Gadget",
  "email" => "inspector.gadget@localhost"
));

// Check that "ninja" is present
$inspector->ensure("name")->isNinja("ninja is missing");

// Check that "ninja" is present (same as isNinja)
$inspector->ensure("name")->ninja("ninja is missing");

// Check that "ninja" is not present
$inspector->ensure("email")->notNinja("ninja is present");
$inspector->hasErrors(); // False

License

Copyright (c) 2011 Simon Gate [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

inspector's People

Contributors

smgt avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.