Skip to content

Repository files navigation

Panic - A simple php validation and sanitation

Build StatusLicenseUsagecodecovCodacy Badge

Panic validator is adobted for simple lovely and easy like common validation in php

There are no hardcoded for simply usage, no weight and pretty useful

Preparement

Quick Start

Installations

composer require "moviet/panic-validator"

Features

  • Simply Validation
  • Custom Warning
  • Custom Verification
  • Simple Filtering
  • Modify Pattern
  • Validate Password
  • Sanitize Html
  • Plus Bonus

Common Usage

Simply Validation

  • You can validate with fast direct resolve example

    require__DIR__ . '/vendor/autoload.php';
    useMoviet\Validator\Panic;
    useMoviet\Validator\Ival;
    $panic = newPanic;
    $nom = 'Smile'; $getnom = $panic->case($nom)
    ->rule(':alpha')
    ->get(); // var_dump : Smile

    The above will return value, if doesn't match will return false

  • Or maybe you enjoy quietly with something like this

    $data = $panic->match(':doc','mydocument.xls');
    // Output : mydocument.xls

    The above will return value, if doesn't match will return false

Custom Warning

  • Validate using your own message

    $request = $_POST['String']; $post = $panic->case($request)
    ->rule(':alNumSpace')
    ->throw(['Do not burn your finger, invalid !!']);
  • Validate using limit and custom language

    $somePost = $panic->case($_POST['String'])
    ->lang('En') // Optional
    ->min(3)
    ->max(100)
    ->rule(':alphaSpace')
    ->throw(['Please follow wakanda alphabets']);
  • Validate using empty case

    $anyPost = $panic->case(/*..Empty..*/)
    ->rule(':alpha')
    ->throw(['~ Gunakan angka atau spasi']);
    // Output : 'Please Fill Out The Form'

    Notes :

    You can't validate an empty value, by default the field does not allow empty chunks

Custom Verification

  • You can validate for example html form like simply below

    $validUser = $panic->case($_POST['username'])
    ->rule(':email')
    ->throw(['Your username may error !!']);
    $validPass = $panic->case($_POST['password'])
    ->auth(8)
    ->throw(['Password minimum 8 characters']);
    $tokenLog = $panic->case($_POST['csrf'])
    ->rule(':alphaNum')
    ->get();
    $data = [$validUser, $validPass, $tokenLog];
    if ($panic->confirm($data) !== false) {
    // $_POST['username']// $_POST['password']// ...
    }
  • Render on your own template html form example

    <formmethod="POST" action="login.php" name="login"><table><tbody><tr><td><?php if (!$validUser) ?><divclass="title_form">Username *
    <labelfor='username' class='error'><?= $validUser; ?></label></div><inputtype="text" placeholder="Enter username" name="username" class="form_login" value="" required></td></tr><tr><td><?php if (!$validPass) ?><divclass="title_form">Password *
    <labelfor='password' class='error'><?= $validPass; ?></label></div><inputtype="text" placeholder="Enter password" name="password" class="form_login" value="" required></td></tr></tbody></table></form>
  • If the above doesn't look so nice, you can trust with new endorse

    $product = $panic->case($_GET['product'])
    ->rule(':alNumSpace')
    ->throw(['Do not only look, Please bo bo boy !!']);
    $tokenId = $panic->case($_GET['token_id'])
    ->rule(':int')
    ->throw(['Your token of course, invalid !!']);
    $validate = [$product, $tokenId]; $entry = $panic->trust($_GET, $validate);
    // to retrieve data, please use arrayif ($entry !== false)
    $entry[0] // Equivalent $product$entry[1] // Equivalent $tokenId
    Next Next...

Simple Filtering

  • We use native filter functions look like below

    $url = 'https://github.com/moviet/panic-validator';
    $data = $panic->filter(':url', $url); // Output : https://github.com/moviet/panic-validator

Modify Pattern

  • You can modify with modify like modify your own rules

    $myrule = $panic->case('My name is yoyo')
    ->min(2)
    ->max(20)
    ->modify('/^[a-zA-Z 0-9]*$/')
    ->throw(['What the hell something wrong ??']);
    // Return Boolen
  • You may want to draft and match for non messenger

    $string = 'Thanks you';
    $data = $panic->draft('/^[a-zA-Z@0-9_1-`-02-^"?>/+\//./]*$/', $string); // Output : Thanks you$string = 'Thanks Match';
    $data = $panic->match(':message', $string); // Output : Thanks Match

Validate Password

  • We put a simply for native password hash validation

    $data = $panic->verify($_POST['password'], 'DataPassword')
    ->warn('Do not type password, please use Abcde !!'); if ($panic->catch($data)) {
    // $_POST['password']
    }
    // Print $data => Do not type password, please use Abcde !!

Sanitize Html

  • You can sanitize with our little pony for simply safe html as one packet

    $stringHtml = '<script> If this is XSS </script>';
    $filterHtml = $panic->htmlSafe($stringHtml); $html = $panic->htmlRaw($filterHtml); // Output : <script> If this is XSS </script>

Plus Bonus

  • Whatever usage a custom clean base64 encode may interesting

    $string = 'And thanos will go on';
    $encode = $panic->base64($string); // Output : aOAIOAIHJSDH837287ksasjka983jsdhdsfsJHJAdsfd34dfSfb$decode = $panic->pure64($encode); // Output : And thanos will go on

Patterns:

AttributesFormat
:num0 - 9
:phone+081991988xx (plus +)
:intInteger/Number
:alphaAlphabets
:alphaNumAlphabets Number
:alphaSpaceAlphabets Plus Spaces
:alNumSpaceAlphabets Number Spaces
:queryHttp Url Query
:urlUrl Address
:imagejpg, jpeg, png, bmp, gif
:docpdf,xls,doc,rtf,txt,ppt,pptx
:addressNormal Address
:subjectEmail Subject
:emailEmail Address
:messageSimple Message Characters

Filters:

AttributesFormat
:intFilter Validate Integer
:floatFilter Validate Float
:urlFilter Validate Url
:domainFilter Validate Domain
:ip4Filter Validate IP4
:ip6Filter Validate IP6
:emailFilter Validate Email

License

Moviet/panic-validator is released under the MIT public license.

About

A dead simple php validation and sanitation library

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages