/** * Test that macros can be defined and triggered. */ public function testMacro() { Inflector::macro('caps', function ($value) { return strtoupper($value); }); $this->assertEquals('FOOBAR', Inflector::caps('foObAr')); try { Inflector::lowers('foObAr'); $this->assertTrue(false); } catch (Exception $e) { $this->assertTrue(true); } }
/** * Loads and parses a user created ini file into a set (object, array), then returns the set. * * @access public * @param string $file * @param string $format * @return object|array * @static */ public static function generate($file, $format = self::GENERATE_ARRAY) { $path = CONFIG . 'sets' . DS . Inflector::filename($file, 'ini'); if (file_exists($path)) { $config = parse_ini_file($path, true, INI_SCANNER_NORMAL); $config = Set::expand($config); if ($format === self::GENERATE_OBJECT) { $config = Set::toObject($config); } return $config; } return null; }
function underscore($string) { return Inflector::underscore($string); }
/** * Attaches the defined closure object to the $__objectMap, as well as saving its options to $_classes. * * @uses Titon\Utility\Inflector * * @param array|string $options { * @type string $alias The alias name to use for object linking * @type string $class The fully qualified class name to use for instantiation * @type bool $register Should the instance be stored in Registry * @type bool $callback Should event callbacks be allowed * @type string $interface The fully qualified interface name that all all attachments must inherit * } * @param object|callable $object * @return $this * @throws \Titon\Common\Exception\InvalidObjectException */ public function attachObject($options, $object = null) { if (is_string($options)) { $options = ['alias' => $options]; } $options = $options + ['alias' => null, 'class' => null, 'register' => true, 'callback' => true, 'interface' => null]; if (empty($options['alias'])) { throw new InvalidObjectException('You must define an alias to reference the attached object'); } else { if ($object === null && empty($options['class'])) { throw new InvalidObjectException(sprintf('You must supply an object, Closure or a class name for %s', $options['alias'])); } else { $options['alias'] = Inflector::variable($options['alias']); } } // If closure if ($object instanceof Closure) { $this->__objectMap[$options['alias']] = $object; // If object } else { if (is_object($object)) { $options['class'] = get_class($object); $this->_attached[$options['alias']] =& $object; } } $this->_classes[$options['alias']] = $options; return $this; }
/** * Return a slugged version of a string. * * @param string $string * @return string */ public static function slugify($string) { $string = strip_tags($string); $string = str_replace(['&', '&'], 'and', $string); $string = str_replace('@', 'at', $string); // @codeCoverageIgnoreStart if (class_exists('Titon\\G11n\\Utility\\Inflector')) { return \Titon\G11n\Utility\Inflector::slug($string); } // @codeCoverageIgnoreEnd return Inflector::slug($string); }
/** * Parse all the default and required attributes that are used within the input field. * * @access protected * @param array $defaults * @param array $attributes * @return array */ protected function _prepareInput(array $defaults = array(), array $attributes = array()) { $attributes = array_merge($defaults, $attributes); $input = $attributes['name']; if ($this->_model != 'Form') { $attributes['name'] = $this->_model . '.' . $attributes['name']; } $nameParts = explode('.', $attributes['name']); $name = array_shift($nameParts); if (!empty($nameParts)) { foreach ($nameParts as $part) { $name .= '[' . $part . ']'; } } $attributes['name'] = $name; foreach (array('disabled', 'readonly', 'multiple') as $attr) { if (isset($attributes[$attr])) { if ($attributes[$attr] === true || $attributes[$attr] == $attr) { $attributes[$attr] = $attr; } unset($attributes[$attr]); } } $attributes = array_merge(array('id' => $this->_model . Inflector::camelize(str_replace('.', ' ', $input)), 'value' => $this->value($this->_model, $input)), $attributes); return $attributes; }
/** * Method to apply custom dispatchers to specific container or controller scopes. * * @access public * @param Closure $Dispatcher * @param array $scope * @return void * @static */ public static function setup(Closure $Dispatcher, array $scope = array()) { $scope = $scope + array('container' => '*', 'controller' => '*'); if ($scope['container'] != '*') { $scope['container'] = Inflector::underscore($scope['container']); } if ($scope['controller'] != '*') { $scope['controller'] = Inflector::underscore($scope['controller']); } self::$__mapping[$scope['container'] . '.' . $scope['controller']] = $Dispatcher; }
/** * Check to see if a mutator method exists on the current model. * If so, return the method name, else return null. * * @param string $field * @return string */ public function hasMutator($field) { $method = sprintf('set%sAttribute', Inflector::camelCase($field)); if (method_exists($this, $method)) { return $method; } return null; }
/** * Build out the path based on the passed router parameters. * * @access public * @param string $base * @return string */ public final function routedPath($base) { $path = $base; if (!empty($this->_config['container'])) { $path .= $this->_config['container'] . DS; } $path .= Inflector::filename($this->_config['controller']); return $path; }
/** * Set a variable to the view. The variable name will be inflected if it is invalid. * * @access public * @param string|array $keys * @param mixed $value * @return boolean */ public final function set($keys, $value = null) { if (!is_array($keys)) { $keys = array($keys => $value); } foreach ($keys as $key => $value) { $this->_config['data'][Inflector::variable($key)] = $value; } return true; }
/** * Generate a foreign key column name by inflecting a class name. * * @param string $class * @return string */ public function buildForeignKey($class) { if (strpos($class, '\\') !== false) { $class = Path::className($class); } return Inflector::underscore($class) . '_id'; }
/** * Attaches the defined closure object to the $__objectMap, as well as saving its options to $_classes. * * @access public * @param string|array $options * @param Closure $object * @return void */ public final function attachObject($options, Closure $object) { if (is_string($options)) { $options = array('alias' => $options); } $options = $options + array('alias' => null, 'namespace' => null, 'callback' => true); if (empty($options['alias'])) { throw new Exception('You must define an alias to reference the passed object.'); } else { $options['alias'] = Inflector::variable($options['alias']); } $this->_classes[$options['alias']] = $options; $this->__objectMap[$options['alias']] = $object; }
/** * Allows you to throw up an error page. The error template is derived from the $action passed. * * @access public * @param string $action * @param array $args * @return void */ public function error($action, array $args = array()) { if (!isset($args['pageTitle'])) { switch ($action) { case is_numeric($action): $args['pageTitle'] = $action; if ($title = $this->Response->statusCode($action)) { $args['pageTitle'] .= ' - ' . $title; $this->Response->status($action); } break; default: $args['pageTitle'] = Inflector::normalize($action); break; } } // Build arguments $args['referrer'] = $this->Request->referrer(); $args['url'] = 'todo'; //Router::construct(Router::current()); $this->View->set($args); $this->View->configure(array('error' => true, 'layout' => 'error', 'template' => $action)); return; }
/** * Test that variable() returns strings as acceptable $variable names. */ public function testVariable() { $this->assertEquals('camelCase', Inflector::variable('camel Case')); $this->assertEquals('StuDlyCaSe', Inflector::variable('StuDly CaSe')); $this->assertEquals('TitleCase', Inflector::variable('Title Case')); $this->assertEquals('Normalcase', Inflector::variable('Normal case')); $this->assertEquals('lowercase', Inflector::variable('lowercase')); $this->assertEquals('UPPERCASE', Inflector::variable('UPPERCASE')); $this->assertEquals('under_score', Inflector::variable('under_score')); $this->assertEquals('dashes', Inflector::variable('dash-es')); $this->assertEquals('_123numbers', Inflector::variable('123 numbers')); $this->assertEquals('withEXTxml', Inflector::variable('with EXT.xml')); $this->assertEquals('lotsofwhitespace', Inflector::variable('lots of white space')); }