1. Go to this page and download the library: Download peak/array-validation 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/ */
peak / array-validation example snippets
$validator = new Validator();
if ($validator->expectExactlyKeys($data, $keys) === true) {
// ...
}
$validation = new StrictValidation($data);
// will throw an exception if any of tests below fail
$validation
->expectOnlyKeys(['id', 'title', 'description', 'isPrivate', 'tags'])
->expectAtLeastKeys(['id', 'title', 'description'])
->expectKeyToBeInteger('id')
->expectKeysToBeString(['title', 'description'])
->expectKeyToBeBoolean('isPrivate')
->expectKeyToBeArray('tags');
// if we reach this point, it means the array structure is
// valid according to the validation rules above.
$vDef = new ValidationDefinition();
$vDef
->expectOnlyKeys(['title', 'content', 'description'])
->expectAtLeastKeys(['title', 'content'])
->expectKeysToBeString(['title', 'content', 'description']);
$validation = new ValidationFromDefinition($vDef, $data);
if ($validation->hasErrors()) {
// $validation->getErrors();
}
// all validation definitions are executed at object creation and an exception is thrown if any of tests failed
new StrictValidationFromDefinition($validationDefinition, $arrayToValidate);
// all validation definitions are executed at object creation and an exception is thrown if any of tests failed
new StrictValidationFromSchema($schema, $arrayToValidate);
// will throw an exception if any of tests fail
$validation->strictValidate($data);
interface ValidationInterface
{
public function expectExactlyKeys(array $keys);
public function expectOnlyOneFromKeys( array $keys);
public function expectAtLeastKeys(array $keys);
public function expectOnlyKeys(array $keys);
public function expectNKeys(int $n);
public function expectKeyToBeArray(string $key, bool $acceptNull = false);
public function expectKeysToBeArray(array $keys, bool $acceptNull = false);
public function expectKeyToBeInteger(string $key, bool $acceptNull = false);
public function expectKeysToBeInteger(array $keys, bool $acceptNull = false);
public function expectKeyToBeFloat(string $key, bool $acceptNull = false);
public function expectKeysToBeFloat(array $keys, bool $acceptNull = false);
public function expectKeyToBeString(string $key, bool $acceptNull = false);
public function expectKeysToBeString(array $keys, bool $acceptNull = false);
public function expectKeyToBeBoolean(string $key, bool $acceptNull = false);
public function expectKeysToBeBoolean(array $keys, bool $acceptNull = false);
public function expectKeyToBeObject(string $key, bool $acceptNull = false);
public function expectKeysToBeObject(array $keys, bool $acceptNull = false);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.