PHP code example of yalesov / arg-validator

1. Go to this page and download the library: Download yalesov/arg-validator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yalesov / arg-validator example snippets


function foo(string $foo) {}

function foo(array|string|null $foo, int/*between 3-10*/ $bar) {}

function foo(Array_of_strings $foo) {}

/**
 * @param array $params
 *  - 'foo' => string
 *  - 'bar' => int
 */
function foo(array $params) {}

class Foo {
  /* const BAR 
}

use Yalesov\ArgValidator\ArgValidator;

function foo($foo)
{
  ArgValidator::assert($foo, 'int');
  // throw InvalidArgumentException if $foo is not int
  // do something
}

use Yalesov\ArgValidator\ArgValidator;

function foo($foo)
{
  $result = ArgValidator::assert($foo, 'int', false);
  // $result = false if invalid
  // do something
}

public static function assert($arg, $checks, $exception = true)

use Yalesov\ArgValidator\ArgValidator;

function foo(array $params)
{
  ArgValidator::arrayAssert($params, array(
    'foo' => 'float',
    'bar' => array('string', 'notSet'),
    'baz' => array('int', 'string', 'min' => 2),
  ));
  // $params['foo'] should be a float
  // $params['bar'] should be a string, if set, or not set at all
  // $params['baz'] can be either an int (>=2), or a string (strlen >= 2)
}

public static function arrayAssert(array $arg, array $checks, $exception = true)

namespace Foo;

use Yalesov\ArgValidator\ArgValidator;

class FooClass {
  const FOO = 'foo';
  const BAR = 2;
}

ArgValidator::assertClassConstant('Foo\FooClass', array('FOO', 'BAR'));
// \Foo\FooClass must have the constants 'FOO' and 'BAR' set

public static function assertClassConstant($className, $constants, $exception = true)

public static function assertClass($className, $exception = true)

public static function throwIssetException($argName)