/** * Constructor * * @param array $config The config array. Possible values are: * - `'handlers'` _array_ : Some custom handlers. */ public function __construct($config = []) { $defaults = ['meta' => [], 'handlers' => [], 'classes' => $this->_classes, 'error' => function ($name, $options, $meta = []) { return Text::insert($options['message'] ?: $this->message($name), $options); }]; $config = Set::merge($defaults, $config); $this->_classes = $config['classes']; $this->set($config['handlers']); $this->error($config['error']); $this->meta($config['meta']); }
it("inserts a variable as many time as it exists a placeholder", function () { $string = '{:a} {:b} {:a} {:a}'; $expected = '1 2 1 1'; $result = Text::insert($string, ['a' => 1, 'b' => 2]); expect($result)->toBe($expected); }); it("inserts a variable with custom placeholder", function () { $string = '%a %b %a %a'; $expected = '1 2 1 1'; $result = Text::insert($string, ['a' => 1, 'b' => 2], ['before' => '%', 'after' => '']); expect($result)->toBe($expected); }); it("escapes escaped placeholder", function () { $string = '{:a} {:b} \\{:a} {:a}'; $expected = '1 2 {:a} 1'; $result = Text::insert($string, ['a' => 1, 'b' => 2], ['escape' => '\\']); expect($result)->toBe($expected); }); }); describe("::clean()", function () { it("cleans placeholder", function () { $result = Text::clean('{:incomplete}'); expect($result)->toBe(''); }); it("cleans placeholder with a default string", function () { $result = Text::clean('{:incomplete}', ['replacement' => 'complete']); expect($result)->toBe('complete'); }); it("cleans placeholder and adjacent spaces", function () { $result = Text::clean('{:a} 2 3'); expect($result)->toBe('2 3');
/** * Build a SQL column constraint * * @param string $name The name of the meta to build. * @param mixed $constraint The constraint value. * @param array $options The constraint options. * @return string The SQL meta string. */ public function constraint($name, $value, $options = []) { $value += ['options' => []]; $meta = isset($this->_constraints[$name]) ? $this->_constraints[$name] : null; if (!($template = isset($meta['template']) ? $meta['template'] : null)) { throw new SqlException("Invalid constraint template `'{$name}'`."); } $data = []; 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 'constraint': $data[$name] = "CONSTRAINT " . $this->name($value); break; case 'expr': $data[$name] = $this->conditions(is_array($value) ? $value : [$value], $options); break; case 'column': case 'primaryKey': case 'foreignKey': $data[$name] = join(', ', array_map([$this, 'name'], (array) $value)); break; } } return trim(Text::insert($template, $data, ['clean' => ['method' => 'text']])); }