Example #1
0
 /**
  * Perform basic validation
  * 
  * @param string      $prop
  * @param \Jasny\Meta $meta
  * @return ValidationResult
  */
 protected function validateBasics($prop, $meta)
 {
     $validation = new ValidationResult();
     if (isset($meta['min']) && $this->{$prop} < $meta['min']) {
         $validation->addError("%s should be at least %s", $prop, $meta['min']);
     }
     if (isset($meta['max']) && $this->{$prop} > $meta['max']) {
         $validation->addError("%s should no at most %s", $prop, $meta['max']);
     }
     if (isset($meta['minLength']) && strlen($this->{$prop}) > $meta['minLength']) {
         $validation->addError("%s should be at least %d characters", $prop, $meta['minLength']);
     }
     if (isset($meta['maxLength']) && strlen($this->{$prop}) > $meta['maxLength']) {
         $validation->addError("%s should be at most %d characters", $prop, $meta['maxLength']);
     }
     if (isset($meta['options'])) {
         $options = array_map('trim', explode(',', $meta['options']));
         if (!in_array($this->{$prop}, $options)) {
             $validation->addError("%s should be one of: %s", $prop, $meta['options']);
         }
     }
     if (isset($meta['type']) && !$this->validateType($prop, $meta['type'])) {
         $validation->addError("%s isn't a valid %s", $prop, $meta['type']);
     }
     if (isset($meta['pattern']) && !$this->validatePattern($prop, $meta['pattern'])) {
         $validation->addError("%s isn't valid", $prop);
     }
     return $validation;
 }
Example #2
0
 /**
  * Authenticate using user credentials
  *
  * @param string $username
  * @param string $password
  * @return ValidationResult
  */
 protected function authenticate($username, $password)
 {
     if (!isset($username)) {
         return ValidationResult::error("username isn't set");
     }
     if (!isset($password)) {
         return ValidationResult::error("password isn't set");
     }
     if (!isset(self::$users[$username]) || !password_verify($password, self::$users[$username]['password'])) {
         return ValidationResult::error("Invalid credentials");
     }
     return ValidationResult::success();
 }