/** * Test that insert() replaces tokens with their value. */ public function testInsert() { $this->assertEquals('Titon is the best PHP framework around!', String::insert('{framework} is the best {lang} framework around!', array('framework' => 'Titon', 'lang' => 'PHP'))); $this->assertEquals('Titon is the best PHP framework around!', String::insert(':framework is the best :lang framework around!', array('framework' => 'Titon', 'lang' => 'PHP'), array('before' => ':', 'after' => ''))); }
/** * Render the statement by injecting custom parameters. * Merge with the default parameters to fill in any missing keys. * * @param array $params * @return string */ public function render(array $params = []) { return trim(String::insert($this->getStatement(), $params + $this->getParams(), ['escape' => false])) . ';'; }
function str_insert($string, array $data, array $options = array()) { return String::insert($string, $data, $options); }
/** * Validate the data against the rules schema. Return true if all fields passed validation. * * @return bool * @throws \Titon\Utility\Exception\InvalidValidationRuleException */ public function validate() { if (!$this->_data) { return false; } $fields = $this->getFields(); $messages = $this->getMessages(); foreach ($this->_data as $field => $value) { if (empty($this->_rules[$field])) { continue; } foreach ($this->_rules[$field] as $rule => $params) { $options = $params['options']; $arguments = $options; array_unshift($arguments, $value); // Use G11n if it is available if (class_exists('Titon\\G11n\\Utility\\Validate')) { $class = 'Titon\\G11n\\Utility\\Validate'; } else { $class = 'Titon\\Utility\\Validate'; } if (!call_user_func(array($class, 'hasMethod'), $rule)) { throw new InvalidValidationRuleException(sprintf('Validation rule %s does not exist', $rule)); } // Prepare messages $message = $params['message']; if (!$message && isset($messages[$rule])) { $message = $messages[$rule]; } if ($message) { $message = String::insert($message, array_map(function ($value) { return is_array($value) ? implode(', ', $value) : $value; }, $options + array('field' => $field, 'title' => $fields[$field]))); } else { throw new InvalidValidationRuleException(sprintf('Error message for rule %s does not exist', $rule)); } if (!call_user_func_array(array($class, $rule), $arguments)) { $this->addError($field, $message); break; } } } return empty($this->_errors); }