lithium\data\source\Database::_constraint PHP Method

_constraint() protected method

Build a SQL column constraint
protected _constraint ( string $name, mixed $value, object $schema = null ) : string
$name string The name of the meta to build
$value mixed The value used for building the meta
$schema object A `Schema` instance.
return string The SQL meta string
    protected function _constraint($name, $value, $schema = null)
    {
        $value += array('options' => array());
        $meta = isset($this->_constraints[$name]) ? $this->_constraints[$name] : null;
        $template = isset($meta['template']) ? $meta['template'] : null;
        if (!$template) {
            return;
        }
        $data = array();
        foreach ($value as $name => $value) {
            switch ($name) {
                case 'key':
                case 'index':
                    if (isset($meta[$name])) {
                        $data['index'] = $meta[$name];
                    }
                    break;
                case 'to':
                    $data[$name] = $this->name($value);
                    break;
                case 'on':
                    $data[$name] = "ON {$value}";
                    break;
                case 'expr':
                    if (is_array($value)) {
                        $result = array();
                        $context = new Query(array('type' => 'none'));
                        foreach ($value as $key => $val) {
                            $return = $this->_processConditions($key, $val, $context, $schema);
                            if ($return) {
                                $result[] = $return;
                            }
                        }
                        $data[$name] = join(" AND ", $result);
                    } else {
                        $data[$name] = $value;
                    }
                    break;
                case 'toColumn':
                case 'column':
                    $data[$name] = join(', ', array_map(array($this, 'name'), (array) $value));
                    break;
            }
        }
        return trim(String::insert($template, $data, array('clean' => array('method' => 'text'))));
    }