예제 #1
0
 /**
  * Resolve default filter and assign default value if field not changed
  */
 public function resolveDefaultFilter()
 {
     $app = Base::instance();
     foreach ($this->map ? $this->map->schema() : [] as $field => $schema) {
         if ($schema['pkey'] && PDO::PARAM_INT === $schema['pdo_type']) {
             continue;
         }
         $filters = [];
         $filters['required'] = [!$schema['nullable']];
         if (preg_match('/^(?<type>\\w+)(?:\\((?<length>.+)\\))?/', $schema['type'], $match)) {
             $length = isset($match['length']) ? $match['length'] : null;
             switch ($match['type']) {
                 case 'int':
                 case 'bigint':
                 case 'smallint':
                 case 'tinyint':
                 case 'integer':
                     $filters['maxInt'] = [null, $length];
                     break;
                 case 'decimal':
                 case 'double':
                 case 'float':
                 case 'real':
                     $x = $app->split($length);
                     $base = pow(10, $x[0] - $x[1]) - 1;
                     $precision = (pow(10, $x[1]) - 1) * 1 / pow(10, $x[1]);
                     $max = $base + $precision;
                     $filters['maxFloat'] = [$max, $length];
                     break;
                 case 'enum':
                 case 'set':
                     $filters['choices'] = [$app->split(str_replace(['"', "'"], '', $length)), $schema['nullable']];
                     break;
                 case 'date':
                     $filters['date'] = [$schema['nullable']];
                     break;
                 default:
                     $filters['maxLength'] = [$length, $schema['nullable']];
                     break;
             }
         }
         $this->filters[$field] = $filters;
         if (!$schema['changed'] && (is_null($schema['value']) || '' === $schema['value']) && !(is_null($schema['default']) || '' === $schema['default'])) {
             $this->map->set($field, $schema['default']);
         }
     }
     return $this;
 }