Пример #1
0
 /**
  * Converts the given column name to it's expected container type context (UUID or String)
  * @param string $columnName column name
  * @param int $toFmt convert to type (UUID::UUID_FMT_BIN, UUID::UUID_FMT_STR)
  * @return mixed converted column name
  */
 protected function typeConvert($columnName, $toFmt)
 {
     if ($this->_containerType != self::TYPE_UUID) {
         return $columnName;
     }
     $bin = UUID::isBinary($columnName);
     // Save accidental double-conversions on binaries
     if ($bin && $toFmt == UUID::UUID_FMT_BIN || !$bin && $toFmt == UUID::UUID_FMT_STR) {
         return $columnName;
     } elseif (!$bin && !UUID::validUUID($columnName)) {
         throw new RuntimeException('Column Name (' . $columnName . ') cannot be converted');
     }
     return UUID::convert($columnName, $toFmt);
 }
Пример #2
0
 /**
  * Validates a field
  * @param string $errorMsg custom error message for field validation error
  * @return bool field validated correctly
  */
 public static function check($value, $label, $typeDefs, &$errors)
 {
     if (empty($typeDefs)) {
         return TRUE;
     }
     if (!is_array($typeDefs)) {
         $typeDefs = array($typeDefs);
     }
     // normalise to real type defs if complex types found
     self::typeExpander($typeDefs);
     $error = FALSE;
     $errorMsg = array();
     foreach ($typeDefs as $type) {
         if (preg_match('/=/', $type)) {
             list($type, $args) = explode("=", $type);
         }
         if (!in_array($type, self::$primitive)) {
             throw new RuntimeException("undefined type definition ({$type})");
         }
         // check for basic validator types
         switch ($type) {
             case 'notempty':
                 $error = empty($value);
                 if ($error) {
                     $errorMsg[] = "Field cannot be empty";
                 }
                 break;
             case 'isempty':
                 // NULL is never allowed, just empty strings
                 $error = $value != '';
                 if ($error) {
                     $errorMsg[] = "Field must be empty";
                 }
                 break;
             case 'email':
                 $error = !filter_var($value, FILTER_VALIDATE_EMAIL);
                 if ($error) {
                     $errorMsg[] = "Invalid email address";
                 }
                 break;
             case 'url':
                 $error = !filter_var($value, FILTER_VALIDATE_URL);
                 if ($error) {
                     $errorMsg[] = "Invalid URL";
                 }
                 break;
             case 'float':
                 $error = !is_float($value);
                 if ($error) {
                     $errorMsg[] = "Field error, expected " . $type;
                 }
                 break;
             case 'int':
             case 'numeric':
                 $error = !is_numeric($value);
                 if ($error) {
                     $errorMsg[] = "Field error, expected " . $type;
                 }
                 break;
             case 'string':
                 $error = !is_string($value);
                 if ($error) {
                     $errorMsg[] = "Field error, expected " . $type;
                 }
                 break;
             case 'bool':
                 $val = strtolower($value);
                 $boolVals = array('true', 'false', 't', 'f', '1', '0', 'y', 'n');
                 $error = !is_bool($value) && !in_array($val, $boolVals);
                 if ($error) {
                     $errorMsg[] = "Field error, expected " . $type;
                 }
                 break;
             case 'maxlength':
                 if (empty($args)) {
                     throw new RuntimeException("type {$type} requires argument");
                 }
                 $error = strlen($value) > $args;
                 if ($error) {
                     $errorMsg[] = "Maximum length {$args} exceeded";
                 }
                 break;
             case 'minlength':
                 if (empty($args)) {
                     throw new RuntimeException("type {$type} requires argument");
                 }
                 $error = strlen($value) < $args;
                 if ($error) {
                     $errorMsg[] = "Minimum length {$args} unmet";
                 }
                 break;
             case 'enum':
                 if (empty($args)) {
                     throw new RuntimeException("type {$type} requires argument");
                 }
                 $enums = explode(",", $args);
                 $error = !in_array($value, $enums);
                 if ($error) {
                     $errorMsg[] = "Invalid Argument";
                 }
                 break;
             case 'uuid':
                 $error = !UUID::validUUID($value);
                 if ($error) {
                     $errorMsg[] = "Invalid UUID (UUID String expected)";
                 }
                 break;
             default:
                 throw new RuntimeException("Unhandled type definition ({$type})");
                 break;
         }
     }
     if (!empty($errorMsg)) {
         $errors[] = array($label => $errorMsg);
         PandraLog::debug(array($label => $errorMsg));
     }
     return empty($errorMsg);
 }