Takes an under_scored version of a word and turns it into an human- readable form
by replacing underscores with a space, and by upper casing the initial character.
public function testIndexScaffold() { $this->_controller->index(); $scaffold = $this->_controller->access('scaffold'); $expected = array('base' => '/radium/configurations', 'controller' => 'Configurations', 'library' => 'radium', 'class' => 'MockConfigurations', 'model' => 'radium\\tests\\mocks\\data\\MockConfigurations', 'slug' => Inflector::underscore('MockConfigurations'), 'singular' => Inflector::singularize('MockConfigurations'), 'plural' => Inflector::pluralize('MockConfigurations'), 'table' => Inflector::tableize('MockConfigurations'), 'human' => Inflector::humanize('MockConfigurations')); $this->assertEqual($expected, $scaffold); }
public static function library($name, array $options = array()) { $defaults = array('docs' => 'config/docs/index.json', 'language' => 'en'); $options += $defaults; if (!($config = Libraries::get($name))) { return array(); } if (file_exists($file = "{$config['path']}/{$options['docs']}")) { $config += (array) json_decode(file_get_contents($file), true); } if (isset($config['languages']) && in_array($options['language'], $config['languages'])) { $config += $config[$options['language']]; foreach ($config['languages'] as $language) { unset($config[$language]); } } $docConfig = Libraries::get('li3_docs'); $category = 'libraries'; if (isset($docConfig['categories']) && is_array($docConfig['categories'])) { if (isset($config['category'])) { unset($config['category']); } foreach ($docConfig['categories'] as $key => $include) { if ($include === true || !in_array($name, array_values((array) $include))) { continue; } $category = $key; } } return $config + array('title' => Inflector::humanize($name), 'category' => $category); }
/** * Growl logger constructor. Accepts an array of settings which are merged with the default * settings and used to create the connection and handle notifications. * * @see lithium\analysis\Logger::write() * @param array $config The settings to configure the logger. Available settings are as follows: * - `'name`' _string_: The name of the application as it should appear in Growl's * system settings. Defaults to the directory name containing your application. * - `'host'` _string_: The Growl host with which to communicate, usually your * local machine. Use this setting to send notifications to another machine on * the network. Defaults to `'127.0.0.1'`. * - `'port'` _integer_: Port of the host machine. Defaults to the standard Growl * port, `9887`. * - `'password'` _string_: Only required if the host machine requires a password. * If notification or registration fails, check this against the host machine's * Growl settings. * - '`protocol'` _string_: Protocol to use when opening socket communication to * Growl. Defaults to `'udp'`. * - `'title'` _string_: The default title to display when showing Growl messages. * The default value is the same as `'name'`, but can be changed on a per-message * basis by specifying a `'title'` key in the `$options` parameter of * `Logger::write()`. * - `'notification'` _array_: A list of message types you wish to register with * Growl to be able to send. Defaults to `array('Errors', 'Messages')`. * @return void */ public function __construct(array $config = array()) { $name = basename(LITHIUM_APP_PATH); $defaults = array('name' => $name, 'host' => '127.0.0.1', 'port' => 9887, 'password' => null, 'protocol' => 'udp', 'title' => Inflector::humanize($name), 'notifications' => array('Errors', 'Messages'), 'connection' => function ($host, $port) { if ($conn = fsockopen($host, $port, $message, $code)) { return $conn; } throw new NetworkException("Growl connection failed: ({$code}) {$message}"); }); parent::__construct($config + $defaults); }
/** * must be called with an array with the following fields to create an asset: * * Array ( * 'name' => 'foobar' // Name of file to be humanized for records name * 'type' => 'jpg' // file-extension, will be adapted to correct asset type * 'tmp_name' => '/dir/some-file.jpg' // FQDN of file, to be retrieved * 'size' => 44 // optional, size in bytes of file * ), * * @param array $file array as described above * @param array $options additional options * - `type`: overwrite type of file, if you want to disable automatic detection * - `delete`: triggers deletion of retrieved temporary file, defaults to true * - `keep`: triggers keeping temporary files in case of errors, defaults to true * @return array parsed content of Assets bytes */ public static function init($file, array $options = array()) { $defaults = array('type' => 'default', 'delete' => true, 'keep' => true); $options += $defaults; // fetch file, if remote // determine size of file on its own // determine md5 of file // find by md5, first $md5 = md5_file($file['tmp_name']); $asset = static::findByMd5($md5, array('fields' => '_id')); if ($asset) { if ($options['delete']) { unlink($file['tmp_name']); } $error = 'Asset already present'; return compact('error', 'asset'); } $mime = Mime::type($file['type']); if (is_array($mime)) { $mime = reset($mime); } $data = array('name' => Inflector::humanize($file['name']), 'filename' => sprintf('%s.%s', $file['name'], $file['type']), 'slug' => strtolower(sprintf('%s.%s', $file['name'], $file['type'])), 'md5' => $md5, 'extension' => $file['type'], 'type' => static::mimetype($mime), 'mime' => $mime, 'size' => $file['size'], 'file' => file_get_contents($file['tmp_name'])); try { $asset = static::create($data); if ($asset->validates()) { $file['success'] = (bool) $asset->save(); $file['asset'] = $asset; } else { $file['errors'] = $asset->errors(); } } catch (Exception $e) { // return array('error' => 'asset could not be saved.'); $file = array('error' => $e->getMessage()); } if (!empty($file['success']) && empty($file['error']) && !$options['keep']) { unlink($file['tmp_name']); } return $file; }
/** * Tests the storage mechanism for `$_underscored`, `$_camelized`, * `$_humanized` and `$_pluralized`. * * @return void */ public function testStorageMechanism() { Inflector::reset(); $expected = array('TestField' => 'test_field'); $this->assertFalse($this->getProtectedValue('$_underscored')); $this->assertEqual(Inflector::underscore('TestField'), 'test_field'); $this->assertEqual($expected, $this->getProtectedValue('$_underscored')); $this->assertEqual(Inflector::underscore('TestField'), 'test_field'); $expected = array('test_field' => 'TestField'); $this->assertFalse($this->getProtectedValue('$_camelized')); $this->assertEqual(Inflector::camelize('test_field', true), 'TestField'); $this->assertEqual($expected, $this->getProtectedValue('$_camelized')); $this->assertEqual(Inflector::camelize('test_field', true), 'TestField'); $expected = array('test_field:_' => 'Test Field'); $this->assertFalse($this->getProtectedValue('$_humanized')); $this->assertEqual(Inflector::humanize('test_field'), 'Test Field'); $this->assertEqual($expected, $this->getProtectedValue('$_humanized')); $this->assertEqual(Inflector::humanize('test_field'), 'Test Field'); $expected = array('field' => 'fields'); $this->assertFalse($this->getProtectedValue('$_pluralized')); $this->assertEqual(Inflector::pluralize('field'), 'fields'); $this->assertEqual($expected, $this->getProtectedValue('$_pluralized')); $this->assertEqual(Inflector::pluralize('field'), 'fields'); }
/** * creates all necessary array keys for rendering a menu/list item * * @param array $navitem all available data for a single navigation item * @return array navitem filled with all needed keys */ private function _item($navitem) { $context = $this->_context; $navitem['url'] = empty($navitem['url']) && !empty($navitem['name']) ? '/' . Inflector::tableize($navitem['name']) : $navitem['url']; $navitem['name'] = empty($navitem['name']) && !empty($navitem['url']) ? Inflector::humanize(basename($navitem['url'])) : $navitem['name']; $navitem['active'] = (bool) stristr($navitem['url'], $context->scaffold->slug); $navitem['link'] = $context->url($navitem['url']); $navitem['badge'] = empty($navitem['badge']) ? null : $this->_badge($navitem['badge']); return $navitem; }
/** * Generates an HTML `<label></label>` object. * * @param string $id The DOM ID of the field that the label is for. * @param string $title The content inside the `<label></label>` object. * @param array $options Besides HTML attributes, this parameter allows one additional flag: * - `'escape'` _boolean_: Defaults to `true`. Indicates whether the title of the * label should be escaped. If `false`, it will be treated as raw HTML. * @return string Returns a `<label>` tag for the name and with HTML attributes. */ public function label($id, $title = null, array $options = array()) { $defaults = array('escape' => true); if (is_array($title)) { list($title, $options) = each($title); } $title = $title ?: Inflector::humanize(str_replace('.', '_', $id)); list($name, $options, $template) = $this->_defaults(__FUNCTION__, $id, $options); list($scope, $options) = $this->_options($defaults, $options); if (strpos($id, '.')) { $generator = $this->_config['attributes']['id']; $id = $generator(__METHOD__, $id, $options); } return $this->_render(__METHOD__, $template, compact('id', 'title', 'options'), $scope); }
/** * Create a html link for sorting by the field, used with pagination * * @param string $field * @param string $title * @return string */ public function sort_header($field, $title = null) { if (!$title) { $title = \lithium\util\Inflector::humanize($field); } $url = $this->_url(); if (!isset($url['?']['dir'])) { $url['?']['dir'] = 'ASC'; } if (isset($url['?']['sort']) && $url['?']['sort'] == $field) { $url['?']['dir'] = $url['?']['dir'] == 'ASC' ? 'DESC' : 'ASC'; } $url['?']['sort'] = $field; return $this->tag('link', array('title' => $title, 'url' => $url)); }
} ?> </ul> </nav> <?php } ?> </div> <?php foreach ($categories as $cat) { ?> <div class="section"> <section> <h3><?php echo $this->title($t(Inflector::humanize($cat), array('scope' => 'li3_docs'))); ?> </h3> <?php foreach ($libraries as $lib => $config) { ?> <?php if ($config['category'] != $cat) { continue; } ?> <h4 class="title"> <?php echo $this->html->link($config['title'], compact('lib') + $defaults); ?>
/** * Constructor. Growl logger constructor. Accepts an array of settings which are merged * with the default settings and used to create the connection and handle notifications. * * @see lithium\analysis\Logger::write() * @param array $config The settings to configure the logger. Available settings are as follows: * - `'name`' _string_: The name of the application as it should appear in Growl's * system settings. Defaults to the directory name containing your application. * - `'host'` _string_: The Growl host with which to communicate, usually your * local machine. Use this setting to send notifications to another machine on * the network. Defaults to `'127.0.0.1'`. * - `'port'` _integer_: Port of the host machine. Defaults to the standard Growl * port, `9887`. * - `'password'` _string_: Only required if the host machine requires a password. * If notification or registration fails, check this against the host machine's * Growl settings. * - '`protocol'` _string_: Protocol to use when opening socket communication to * Growl. Defaults to `'udp'`. * - `'title'` _string_: The default title to display when showing Growl messages. * The default value is the same as `'name'`, but can be changed on a per-message * basis by specifying a `'title'` key in the `$options` parameter of * `Logger::write()`. * - `'notification'` _array_: A list of message types you wish to register with * Growl to be able to send. Defaults to `array('Errors', 'Messages')`. * @return void */ public function __construct(array $config = array()) { $name = basename(Libraries::get(true, 'path')); $defaults = compact('name') + array('host' => '127.0.0.1', 'port' => 9887, 'password' => null, 'protocol' => 'udp', 'title' => Inflector::humanize($name), 'notifications' => array('Errors', 'Messages'), 'registered' => false); parent::__construct($config + $defaults); }
?> <div class="controls hide" id="stepTwoQuestions-<?php echo $name; ?> "> <?php foreach ($matrix['questions'] as $category => $questions) { ?> <?php if (count($questions) <= 0) { continue; } ?> <div style="margin-bottom: 16px"> <h4><?php echo \lithium\util\Inflector::humanize($category); ?> </h4> <div class="questionGroup"> <?php foreach ($questions as $key => $question) { ?> <label class="radio"> <input type="radio" name="grank-<?php echo $name; ?> -<?php echo $category; ?> " data-category="<?php echo $category;
<div class="control-group user-multi-select" data-field-name="<?php echo $fieldname; ?> "> <label class="control-label"><?php echo isset($label) ? $label : \lithium\util\Inflector::humanize($fieldname); ?> :</label> <div class="controls"> <div id="existingCaptains" class="user-list"> <?php foreach ($user_list as $u) { ?> <div><input type="hidden" name="<?php echo $fieldname; ?> []" value="<?php echo $u->_id; ?> "> <span class="span3 uneditable-input"><?php echo $u->firstname; ?> <?php echo $u->lastname; ?> </span> <a href="#remove" class="remove"><i class="icon-remove"></i></a></div> <?php } ?> </div>
public function label($name, $title = null, $options = array()) { $title = $title ?: Inflector::humanize($name); list($name, $options, $template) = $this->_defaults(__FUNCTION__, $name, $options); return $this->_render(__METHOD__, $template, compact('name', 'title', 'options')); }
/** * Generates an HTML `<label></label>` object. * * @param string $name The DOM ID of the field that the label is for. * @param string $title The content inside the `<label></label>` object. * @param array $options Besides HTML attributes, this parameter allows one additional flag: * - `'escape'` _boolean_: Defaults to `true`. Indicates whether the title of the * label should be escaped. If `false`, it will be treated as raw HTML. * @return string Returns a `<label>` tag for the name and with HTML attributes. */ public function label($name, $title = null, array $options = array()) { $defaults = array('escape' => true); if (is_array($title)) { list($title, $options) = each($title); } $title = $title ?: Inflector::humanize($name); list($name, $options, $template) = $this->_defaults(__FUNCTION__, $name, $options); list($scope, $options) = $this->_options($defaults, $options); return $this->_render(__METHOD__, $template, compact('name', 'title', 'options'), $scope); }
/** * Generates different variations of the configured $this->model property name * * If no model is configured (i.e. `null`) - it automatically detects the corresponding * model for this Controller via Inflection and `Libraries::locate()`. * * @see lithium\core\Libraries::locate() * @param string $field defines, what variation of the default you want to have * available are 'class', 'model', 'singular', 'plural' and 'table' and 'human'. * if omitted, returns array containing all of them. * @return array|string **/ protected function _scaffold($field = null) { if (is_null($this->model)) { $this->model = (string) Libraries::locate('models', $this->request->controller); } if (is_null($this->scaffold)) { $class = basename(str_replace('\\', '/', $this->model)); $base = !empty($this->library) ? array('controller' => $this->controller, 'library' => $this->library) : array('controller' => $this->controller); $this->scaffold = array('base' => Router::match($base, $this->request), 'controller' => strtolower($this->controller), 'library' => $this->library, 'class' => $class, 'model' => $this->model, 'slug' => Inflector::underscore($class), 'singular' => Inflector::singularize($class), 'plural' => Inflector::pluralize($class), 'table' => Inflector::tableize($class), 'human' => Inflector::humanize($class)); } if (!is_null($field)) { return isset($this->scaffold[$field]) ? $this->scaffold[$field] : false; } Environment::set(true, array('scaffold' => $this->scaffold)); return $this->scaffold; }
<?php use lithium\util\Inflector; $defaults = array('controller' => 'li3_docs.ApiBrowser', 'action' => 'view'); ?> <article> <h1 class="h-alpha">Documentation</h1> <?php foreach ($categories as $cat) { ?> <section> <h1 class="h-beta"><?php echo $this->title(Inflector::humanize($cat)); ?> </h1> <?php foreach ($libraries as $lib => $config) { ?> <article> <?php if ($config['category'] != $cat) { continue; } ?> <h1 class="h-gamma title"> <?php echo $this->html->link($config['title'], compact('lib') + $defaults); ?> </h1> <?php
protected function _autoSelects($name, array $options = array()) { $model = $this->_binding->model(); $method = Inflector::pluralize($name); $rules = $this->instance->validates; if (method_exists($model, $method)) { $list = $model::$method(); if (!empty($list)) { $options['list'] = $list; return $options; } } if (isset($rules[$name])) { if (is_array($rules[$name][0])) { $rule_list = $rules[$name]; } else { $rule_list = array($rules[$name]); } foreach ($rule_list as $rule) { if ($rule[0] === 'inList' and isset($rule['list'])) { foreach ($rule['list'] as $optval) { $options['list'][$optval] = Inflector::humanize($optval); } } } } return $options; }
/** * testHumanization method * * @return void */ public function testHumanization() { $this->assertEqual(Inflector::humanize('posts'), 'Posts'); $this->assertEqual(Inflector::humanize('posts_tags'), 'Posts Tags'); $this->assertEqual(Inflector::humanize('file_systems'), 'File Systems'); $this->assertEqual(Inflector::humanize('the-post-title', '-'), 'The Post Title'); }
<?php use lithium\util\Inflector; if (!isset($object) || !$object) { return; } $makeTitle = function ($value) { if (strpos($value, '.md') === false) { return $value; } $value = str_replace('.md', '', $value); $value = str_replace('-', '_', $value); if (strlen($value) <= 3) { return strtoupper($value); } return Inflector::humanize($value); }; ?> <nav class="crumbs"> <?php echo $this->html->link('Documentation', ['library' => 'li3_docs', 'controller' => 'ApiBrowser']); ?> > <ul> <?php foreach (array_slice($this->docs->crumbs($object), 1) as $crumb) { ?> <li class="<?php echo $crumb['class']; ?>
<?php if (empty($errors)) { return; } ?> <div class="alert alert-warning"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">x</button> <h4><i class="fa fa-warning2"></i> Warning</h4> <p>Some errors occured, you should double-check your inputs</p> <dl class="dl-intended"> <?php foreach ($errors as $field => $_errors) { echo sprintf('<dt>%s</dt>', \lithium\util\Inflector::humanize($field)); foreach ($_errors as $error) { echo sprintf('<dd>%s</dd>', $error); } } ?> </dl> </div>