phramework / validate
phramework's validation library
Installs: 35 250
Dependents: 4
Suggesters: 0
Security: 0
Stars: 3
Watchers: 5
Forks: 1
Open Issues: 22
Requires
- php: ^7.1|^8.0.0
- ext-json: *
- phramework/exceptions: ^1.0.0
Requires (Dev)
- codacy/coverage: ^1.0
- phpunit/phpunit: ^7.5.10
- squizlabs/php_codesniffer: ^3.4.2
- dev-dev-1.x
- 1.3.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.0-RC5
- 1.0.0-RC4
- 1.0.0-RC3
- 1.0.0-RC2
- 1.0.0-RC1
- 1.0.0-RC
- v0.12.0
- 0.11.1
- 0.11.0
- 0.10.3
- 0.10.2
- 0.10.1
- 0.10.0
- 0.9.0
- 0.8.0
- 0.7.2
- 0.7.1
- 0.7.0
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.1
- 0.5.0
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.0
- 0.2.2
- 0.2.0
- 0.1.2
- 0.1.1
- 0.1.0
- 0.0.0
- dev-master
- dev-xs-master-travis-7.4
- dev-xs-travis-7.4
- dev-ak-update_copyright_year
- dev-ak-support_optional_semicolon_in_offset
- dev-ak-fixes_in_tests
- dev-ak-add_more_testcases
- dev-xs-updated-travis
- dev-xs-set-exclusiveMinimum-default-false
- dev-xs-nullable-types
- dev-dev-x-visibility
- dev-dev-pointers
This package is auto-updated.
Last update: 2024-10-11 13:41:13 UTC
README
phramework's validation library https://phramework.github.io/validate/
Usage
Require package using composer
composer require phramework/validate
Parse an integer value
require './vendor/autoload.php'; use \Phramework\Validate\IntegerValidator; $validator = new IntegerValidator(-1, 1); $value = $validator->parse('0'); var_dump($value);
The above example will output:
int(0)
Parse an object of strings
$personalInformationValidator = new ObjectValidator( (object) [ 'name' => new StringValidator(2, 30), 'city' => new StringValidator(2, 30), 'age' => new IntegerValidator(1, 200), ], ['name', 'city', 'age'], //required properties false //no additional properties allowed ); $personalInformation = $validationModel->parse((object) [ 'name' => 'Jane Doe', 'city' => 'Athens', 'age' => 28 ]); print_r($personalInformation);
The above example will output:
stdClass Object
(
[name] => Jane Doe
[city] => Athens,
[age] => 28
)
Validating an array of enum strings
/* * A validator that allows you to pick one or two colors between blue, green and red */ $colorsValidator = new ArrayValidator( 1, //minItems 2, //maxItems (new StringValidator()) //items ->setEnum([ 'blue', 'green', 'red', ]), true //unique items ); /* * $parsedOneItem will be validated successfully */ $parsedOneItem = $colorsValidator->parse(['blue']); //will be [blue] /* * $parsedTwoItems will be validated successfully */ $parsedTwoItems = $colorsValidator->parse(['blue', 'red']); //will be [blue, red] /* * $resultOfZeroItemsStatus cannot be validated true the validator requires minItems of 1 */ $resultOfZeroItemsStatus = $colorsValidator->validate([]); $resultOfZeroItemsStatus->getStatus(); // will be false because validation failed /** @var \Phramework\Exceptions\IncorrectParameterException $exception in this case */ $exception = $resultOfZeroItemsStatus->getException(); $exception->getFailure(); // will be minItems /* * $resultOfIncorrectItemsStatus cannot be validated true because "yellow" is not an allowed item */ $resultOfIncorrectItemsStatus = $colorsValidator->validate(['yellow']); $resultOfIncorrectItemsStatus->getStatus(); // will be false because validation failed /** @var \Phramework\Exceptions\IncorrectParameterException $exception in this case */ $exception = $resultOfIncorrectItemsStatus->getException(); $exception->getFailure(); // will be items /* * Following will throw \Phramework\Exceptions\IncorrectParameterException * with failure maxItems because validator requires maxItems 2 */ $colorsValidator ->parse([ 'blue', 'green', 'red' ]);
Check wiki for more examples.
Development
Install dependencies
composer update
Test and lint code
composer test
composer lint
Generate documentation
composer doc
License
Copyright 2015-2019 Xenofon Spafaridis
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.