/** * Serve fields anotation and description * * @param string Field general rule * @param array Field custom rule * @param string Field anotation * @return void */ public static final function field($type = '', $args = array(), $schema = '') { $rules = array(); $args = is_array($args) ? $args : (array) $args; $annotations = array(); // Diagnose the type pattern if (preg_match('/^([^)]+)\\[(.*?)\\]$/', $type, $m) and count($m) == 3) { // Parsing [n,n] $type = $m[1]; $constraint = explode(',', $m[2]); if (count($constraint) == 2) { $rules[] = 'min_length[' . trim($constraint[0]) . ']'; $rules[] = 'max_length[' . trim($constraint[1]) . ']'; $annotations[] = trim($constraint[1]); } else { $rules[] = 'max_length[' . $constraint[0] . ']'; $annotations[] = $constraint[0]; } } // Determine each type into its validation rule respectively switch ($type) { case 'auto': $rules[] = 'callback_auto_check'; // Add exception for database spec if (self::driver('mysql')) { $annotations[] = 'INT'; $annotations[] = 'unsigned'; $annotations[] = 'auto_increment'; } elseif (self::driver('postgre')) { $annotations[] = 'SERIAL'; } else { $annotations[] = 'INTEGER'; } break; case 'datetime': $rules[] = 'callback_date_check'; $annotations[] = 'DATETIME'; break; case 'string': $rules[] = 'callback_char_check'; if (self::driver('postgre')) { $annotations[] = 'VARCHAR'; } else { $annotations[] = 'TEXT'; } break; case 'spatial': $rules[] = 'callback_char_check'; $annotations[] = 'GEOMETRY'; break; case 'char': $rules[] = 'callback_char_check'; $annotations[] = 'VARCHAR'; break; case 'numeric': $rules[] = 'numeric'; // Add exception for mysql if (self::driver('mysql')) { $annotations[] = 'TINYINT'; } else { $annotations[] = 'INTEGER'; } break; case 'int': $rules[] = 'integer'; // Add exception for mysql if (self::driver('mysql')) { $annotations[] = 'INT'; } else { $annotations[] = 'INTEGER'; } break; case 'email': $rules[] = 'valid_email'; $annotations[] = 'VARCHAR'; break; } // Are there other annotations? $other_annotations = explode(',', $schema); // If yes, then merge it with above if (!empty($other_annotations)) { $other_annotations = Janitor::arr_trim($other_annotations); $annotations = array_merge($annotations, $other_annotations); } // Merge the rules and separate between internal callback // And CI validation rules $callbacks = array(); $rules = array_merge($rules, $args); foreach ($rules as $index => $rule) { if (strpos($rule, 'callback') === 0) { $callbacks[] = str_replace('callback', '', $rule); unset($rules[$index]); } } // We now have define all rules, callbacks and annotations return array('rules' => implode('|', $rules), 'callbacks' => $callbacks, 'annotations' => $annotations); }
/** * Generate the relationship option for pre-process queries * * @param array Gas relationship option spec * @return array Formatted option */ public static function generate_options($options) { // Initiate new queries holder, and define allowable options $queries = array(); $allowed = array('select', 'order_by', 'limit'); // Loop over it foreach ($options as $option) { // Parse option annotation list($method, $args) = explode(':', $option); if (!in_array($method, $allowed)) { // No valid method found continue; } else { // Casting the argument annotation // and do the pre-process switch ($method) { case 'select': $select_statement = explode(',', $args); $queries[$method] = Janitor::arr_trim($select_statement); break; case 'limit': $queries[$method] = " 0, {$args}"; break; case 'order_by': if (preg_match('/^([^\\n]+)\\[(.*?)\\]$/', $args, $m) and count($m) == 3) { $queries[$method] = "`{$m['1']}` " . strtoupper($m[2]); } break; } } } // Return the formatted queries options return $queries; }