예제 #1
0
 public function validateField(\Sokil\Mongo\Document $document, $fieldName, array $params)
 {
     $value = $document->get($fieldName);
     if (!$value) {
         return;
     }
     if (!isset($params['min'])) {
         throw new Exception('Minimum value of range not specified');
     }
     if (!isset($params['max'])) {
         throw new Exception('Maximum value of range not specified');
     }
     if ($value < $params['min']) {
         if (empty($params['minMessage'])) {
             $params['minMessage'] = 'Field "' . $fieldName . '" less than minimal value of range in ' . get_called_class();
         }
         $document->addError($fieldName, $this->getName(), $params['minMessage']);
     }
     if ($value > $params['max']) {
         if (empty($params['maxMessage'])) {
             $params['maxMessage'] = 'Field "' . $fieldName . '" less than minimal value of range in ' . get_called_class();
         }
         $document->addError($fieldName, $this->getName(), $params['maxMessage']);
     }
 }
예제 #2
0
 public function validateField(\Sokil\Mongo\Document $document, $fieldName, array $params)
 {
     $value = $document->get($fieldName);
     if (!$value) {
         return;
     }
     if (empty($params[0])) {
         throw new Exception('Type not specified');
     }
     $requiredTypes = (array) $params[0];
     $allowedTypes = array('array', 'bool', 'callable', 'double', 'float', 'int', 'integer', 'long', 'null', 'numeric', 'object', 'real', 'resource', 'scalar', 'string');
     foreach ($requiredTypes as $type) {
         if (!in_array($type, $allowedTypes)) {
             throw new Exception('Type must be one of ' . implode(', ', $allowedTypes));
         }
         if (true === call_user_func('is_' . $type, $value)) {
             return;
         }
     }
     if (!isset($params['message'])) {
         if (count($requiredTypes) === 1) {
             $params['message'] = 'Field "' . $fieldName . '" must be of type ' . $requiredTypes[0] . ' in ' . get_called_class();
         } else {
             $params['message'] = 'Field "' . $fieldName . '" must be one of types ' . implode(', ', $requiredTypes) . ' in ' . get_called_class();
         }
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
예제 #3
0
 public function validateField(\Sokil\Mongo\Document $document, $fieldName, array $params)
 {
     if (!$document->get($fieldName)) {
         return;
     }
     if (in_array($document->get($fieldName), $params['range'])) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Field "' . $fieldName . '" not in range of allowed values in model ' . get_called_class();
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
예제 #4
0
 public function validateField(\Sokil\Mongo\Document $document, $fieldName, array $params)
 {
     if (!$document->get($fieldName)) {
         return;
     }
     if (is_null($document->get($fieldName))) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Field "' . $fieldName . '" must be null in model ' . get_called_class();
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
예제 #5
0
 public function validateField(\Sokil\Mongo\Document $document, $fieldName, array $params)
 {
     $value = $document->get($fieldName);
     if (!$value) {
         return;
     }
     $length = mb_strlen($value);
     // check if field is of specified length
     if (isset($params['is'])) {
         if ($length === $params['is']) {
             return;
         }
         if (!isset($params['message'])) {
             $params['message'] = 'Field "' . $fieldName . '" length not equal to ' . $params['is'] . ' in model ' . get_called_class();
         }
         $document->addError($fieldName, $this->getName(), $params['message']);
         return;
     }
     // check if fied is shorter than required
     if (isset($params['min'])) {
         if ($length < $params['min']) {
             if (!isset($params['messageTooShort'])) {
                 $params['messageTooShort'] = 'Field "' . $fieldName . '" length is shorter tnan ' . $params['min'] . ' in model ' . get_called_class();
             }
             $document->addError($fieldName, $this->getName(), $params['messageTooShort']);
             return;
         }
     }
     // check if fied is longer than required
     if (isset($params['max'])) {
         if ($length > $params['max']) {
             if (!isset($params['messageTooLong'])) {
                 $params['messageTooLong'] = 'Field "' . $fieldName . '" length is longer tnan ' . $params['max'] . ' in model ' . get_called_class();
             }
             $document->addError($fieldName, $this->getName(), $params['messageTooLong']);
             return;
         }
     }
 }
 public function validateField(\Sokil\Mongo\Document $document, $fieldName, array $params)
 {
     if (!$document->get($fieldName)) {
         return;
     }
     if (preg_match('/^\\w+$/', $document->get($fieldName))) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Field "' . $fieldName . '" not alpha-numeric in model ' . get_called_class();
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
예제 #7
0
 public function validateField(\Sokil\Mongo\Document $document, $fieldName, array $params)
 {
     if (!$document->get($fieldName)) {
         return;
     }
     if ($document->get($fieldName) === $params['to']) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Not equals';
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
예제 #8
0
 public function validateField(\Sokil\Mongo\Document $document, $fieldName, array $params)
 {
     if (!$document->get($fieldName)) {
         return;
     }
     if (preg_match($params['pattern'], $document->get($fieldName))) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Field "' . $fieldName . '" not match regexp ' . $params['pattern'] . ' in model ' . get_called_class();
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
예제 #9
0
 public function validateField(\Sokil\Mongo\Document $document, $fieldName, array $params)
 {
     if (!$document->get($fieldName)) {
         return;
     }
     $carsNumber = $document->get($fieldName);
     if (is_numeric($carsNumber) && 0 === $this->getMod($carsNumber)) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Value of field "' . $fieldName . '" is not valid card number at ' . get_called_class();
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
예제 #10
0
 public function validateField(\Sokil\Mongo\Document $document, $fieldName, array $params)
 {
     $value = $document->get($fieldName);
     // check only if set
     if (!$value) {
         return;
     }
     // check if url valid
     $isValidUrl = (bool) filter_var($value, FILTER_VALIDATE_URL);
     if (!$isValidUrl) {
         if (!isset($params['message'])) {
             $params['message'] = 'Value of field "' . $fieldName . '" is not valid url in model ' . get_called_class();
         }
         $document->addError($fieldName, $this->getName(), $params['message']);
         return;
     }
     // ping not required - so url is valid
     if (empty($params['ping'])) {
         return;
     }
     // ping required
     $dnsRecordExists = dns_get_record(parse_url($value, PHP_URL_HOST));
     if (!$dnsRecordExists) {
         if (!isset($params['message'])) {
             $params['message'] = 'Value of field "' . $fieldName . '" is valid url but host is unreachable in model ' . get_called_class();
         }
         $document->addError($fieldName, $this->getName(), $params['message']);
         return;
     }
     if ($this->isUrlAccessible($value)) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Value of field "' . $fieldName . '" is valid url but page not found ' . get_called_class();
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
예제 #11
0
 public function validateField(\Sokil\Mongo\Document $document, $fieldName, array $params)
 {
     $value = $document->get($fieldName);
     if (!$value) {
         return;
     }
     if (!isset($params['than'])) {
         throw new Exception('Maximum value not specified');
     }
     if ($value <= $params['than']) {
         if (empty($params['message'])) {
             $params['message'] = 'Field "' . $fieldName . '" must be greater than specified value in ' . get_called_class();
         }
         $document->addError($fieldName, $this->getName(), $params['message']);
     }
 }
예제 #12
0
 public function validateField(\Sokil\Mongo\Document $document, $fieldName, array $params)
 {
     $value = $document->get($fieldName);
     // check only if set
     if (!$value) {
         return;
     }
     // check if url valid
     if (false !== filter_var($value, FILTER_VALIDATE_IP)) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Value of field "' . $fieldName . '" is not valid IP address in model ' . get_called_class();
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
예제 #13
0
 public function validateField(\Sokil\Mongo\Document $document, $fieldName, array $params)
 {
     $value = $document->get($fieldName);
     if (!$value) {
         return;
     }
     $isValidEmail = filter_var($value, FILTER_VALIDATE_EMAIL);
     $isValidMX = true;
     if ($isValidEmail && !empty($params['mx'])) {
         list(, $host) = explode('@', $value);
         $isValidMX = checkdnsrr($host, 'MX');
     }
     if ($isValidEmail && $isValidMX) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Value of field "' . $fieldName . '" is not email in model ' . get_called_class();
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }