/** * Validates is a username has alphanumeric values and is between 3 to 12 characters long. * * @param Request $request * * @return boolean|null */ private function validateUsername(Request $request) { $username = self::$validator->isString('username'); $result = $username->isRequired()->isAlpha()->isBetween(4, 12, true)->validate($request->username); $this->isValid = $this->isValid && $result; $this->errors = array_merge($this->errors, $username->getErrors()); }
/** * @test */ public function itShouldCheckIfHasFileNameFormat() { $validator = Validator::create(); $stringValidator = $validator->isString('image')->isLowercase(); $this->assertTrue(FileUploadValidation::hasFileNameFormat('image', $stringValidator)); $stringValidator = $validator->isString('image')->isAlphanumeric(); $_FILES['image']['name'] = '@sample.png'; $this->assertFalse(FileUploadValidation::hasFileNameFormat('image', $stringValidator)); }
/** * @test */ public function itShouldCheckIfHasKeyFormat() { $array = ['one' => 'hello', 'two' => 'world']; $arrayObject = new \ArrayObject($array); $fixedArray = (new \SplFixedArray())->fromArray(\array_values($array)); $validator = Validator::create(); $keyIsString = $validator->isString('key')->isAlpha(); $keyIsInteger = $validator->isInteger('key')->isPositiveOrZero(); $this->assertTrue($this->getValidator()->hasKeyFormat($keyIsString)->validate($array)); $this->assertTrue($this->getValidator()->hasKeyFormat($keyIsString)->validate($arrayObject)); $this->assertTrue($this->getValidator()->hasKeyFormat($keyIsInteger)->validate($fixedArray)); }
/** * @return \NilPortugues\Validator\Attribute\String\StringAttribute */ protected function getValidator() { $validator = Validator::create(); return $validator->isString('propertyName'); }
/** * */ public function __construct() { if (!isset(self::$validator)) { self::$validator = Validator::create(); } }
<?php use NilPortugues\Validator\Validator; include realpath(dirname(__FILE__)) . '/../vendor/autoload.php'; if (empty($_FILES)) { ?> <h2>Single upload</h2> <form method="POST" enctype="multipart/form-data"> <input type="file" name="one_image" multiple="multiple"> <input type="submit" value="Submit"> </form> <?php } else { $validator = Validator::create(); $fileValidator = $validator->isFileUpload('image'); $isValid = $fileValidator->isBetween(0, 2, 'MB', true)->isImage()->hasValidUploadDirectory('./')->notOverwritingExistingFile('./')->validate('one_image'); echo '<h3>Input data is valid?</h3>'; echo $isValid ? 'Yes' : 'No'; $errors = $fileValidator->getErrors(); echo '<h3>Errors?</h3>'; echo '<pre>'; print_r($errors); echo '</pre>'; }
/** * @param string $name * @param string $type * * @return AbstractValidator */ private static function createValidator($name, $type) { return \call_user_func_array([Validator::create(), ValidatorType::getMethod($type)], [$name]); }