1. Go to this page and download the library: Download 3nr1c/structure 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/ */
3nr1c / structure example snippets
if (!is_array($c)) {
throw new \Exception();
} else if (!isset($c["profile"])) {
throw new \Exception();
} else if (!is_array($c["profile"])) {
throw new \Exception();
} //...
$arrayCheck = new \Structure\ArrayS();
$arrayCheck->setFormat(array("profile" => "array");
if (!$arrayCheck->check($c)) {
throw new \Exception();
} //...
public function __construct($data = null, $null = false);
public function setData($data);
public function getData();
// to avoid the data to be null:
public function setNull($null);
public function getNull();
// to check everything (type and range/format)
public function check($data = null, &$failed = null);
// to format things (specially powerfull in ArrayS)
public function format($data = null);
public static function getLastFail();
public static function clearLastFail();
public static function ArrayS($format = null, $data = null, $countStrict = true, $null = false);
public static function NumericS($range = null, $data = null, $null = false);
public static function IntegerS($range = null, $data = null, $null = false);
public static function FloatS($range = null, $data = null, $null = false);
public static function StringS($data = null, $null = false);
$scalar = new \Structure\ScalarS();
if (!$scalar->check($var, $failed)) {
print "Error: expected _scalar_, got " . $failed;
}
$scalar->setFormat("value1", "value2", 10, 11, 12);
// or
$scalar->setFormat("{value1, value2, 10, 11, 12, commas and brackets can be escaped: \{\,}");
$numeric = new \Structure\NumericS();
$numeric->check("3.2");// true
$numeric->check("5");// true
$float = new \Structure\FloatS();
$float->check("3.2");// false
$integer = new \Structure\IntegerS();
$integer->check("5");// false
$float = new \Structure\FloatS();
$integer = new \Structure\IntegerS();
$float->setNumeric(true);
$integer->setNumeric(true);
$float->check("3.2");// true
$integer->check("5");// true
$string = new \Structure\StringS();
$string->check($var);
$string->setLength(4); // 4 characters or more
$string->setLength(4, 10); // 4 to 10 characters
$string->setLength(0, 10); // up to 10 characters
$string->setLength("(4..10)"); // 4 to 10 characters
public function setFormat($format);
public function getFormat();
public function setCountStrict();
public function isCountStrict();
public static function isAssociative($data);
$array->setFormat("string[*]"); // checks for 0 or more strings
$array->setFormat("integer[+]"); // checks for 1 or more integers
$array->setFormat("scalar[5+]"); // checks for 5 or more scalars
$array->setFormat("float[3..5]"); // checks for 3 to 5 floats
$array->setFormat("float[..5]"); // checks for a maximum of 5 floats
$array->setFormat("float[3..]"); // checks for at least 3 floats