/** * Test all macros are returned. */ public function testMacros() { $lower = function ($value) { return strtolower($value); }; $upper = function ($value) { return strtoupper($value); }; String::macro('lower', $lower); String::macro('upper', $upper); $this->assertEquals(array('lower' => $lower, 'upper' => $upper), String::macros()); }
/** * Test that uuid() returns valid UUID4 strings. */ public function testUuid() { $this->assertRegExp('/^[a-f0-9]{8}\\-[a-f0-9]{4}\\-4[a-f0-9]{3}\\-[89AB]{1}[a-f0-9]{3}\\-[a-f0-9]{12}$/i', String::uuid()); $this->assertRegExp('/^[a-f0-9]{8}\\-[a-f0-9]{4}\\-4[a-f0-9]{3}\\-[89AB]{1}[a-f0-9]{3}\\-[a-f0-9]{12}$/i', String::uuid()); $this->assertRegExp('/^[a-f0-9]{8}\\-[a-f0-9]{4}\\-4[a-f0-9]{3}\\-[89AB]{1}[a-f0-9]{3}\\-[a-f0-9]{12}$/i', String::uuid()); }
/** * 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_truncate($string, $limit = 25, array $options = array()) { return String::truncate($string, $limit, $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); }