moviet/panic-validator

A simple php validation library for php

v1.0.0 2018-12-14 11:19 UTC

This package is auto-updated.

Last update: 2024-04-26 07:40:09 UTC


README

Build Status License Usage codecov Codacy 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';
    
    use Moviet\Validator\Panic;
    use Moviet\Validator\Ival;
    
    $panic = new Panic;
    
    $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

    <form method="POST" action="login.php" name="login">
    <table>
    <tbody>
    
    <tr><td>
    <?php if (!$validUser) ?>
    <div class="title_form">Username *
    <label for='username' class='error'><?= $validUser; ?></label></div>
    <input type="text" placeholder="Enter username" name="username" class="form_login" value="" required>
    </td></tr>
    
    <tr><td>
    <?php if (!$validPass) ?>
    <div class="title_form">Password *
    <label for='password' class='error'><?= $validPass; ?></label></div>
    <input type="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 array
    
    if ($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:

Attributes Format
:num 0 - 9
:phone +081991988xx (plus +)
:int Integer/Number
:alpha Alphabets
:alphaNum Alphabets Number
:alphaSpace Alphabets Plus Spaces
:alNumSpace Alphabets Number Spaces
:query Http Url Query
:url Url Address
:image jpg, jpeg, png, bmp, gif
:doc pdf,xls,doc,rtf,txt,ppt,pptx
:address Normal Address
:subject Email Subject
:email Email Address
:message Simple Message Characters

Filters:

Attributes Format
:int Filter Validate Integer
:float Filter Validate Float
:url Filter Validate Url
:domain Filter Validate Domain
:ip4 Filter Validate IP4
:ip6 Filter Validate IP6
:email Filter Validate Email

License

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