Returns a string with all spaces converted to given replacement and
non word characters removed. Maps special characters to ASCII using
Inflector::$_transliteration, which can be updated using Inflector::rules().
/** * Get content of file, parse it with lessc and return formatted css * * @todo allow for css-file name only, and search it in all avail. webroots * @param string $file full path to file * @param array $options Additional options to control flow of method * - header - controls, whether to prepend a header * - cache - controls, whether to cache the result * - cachePath - Where to cache files, defaults to * resources/tmp/cache * @return string|boolean generated css, false in case of error */ public static function file($file, array $options = array()) { $defaults = array('header' => true, 'cache' => true, 'cachePath' => Libraries::get(true, 'resources') . '/tmp/cache', 'cacheKey' => Inflector::slug(str_replace(array(LITHIUM_APP_PATH, '.less'), array('', '.css'), $file))); $options += $defaults; $css_file = $options['cachePath'] . '/' . $options['cacheKey']; if (file_exists($css_file) && filemtime($css_file) >= filemtime($file)) { return file_get_contents($css_file); } if (!file_exists($file)) { return false; } try { $less = static::_getLess($file); $output = $less->parse(); } catch (Exception $e) { $output = "/* less compiler exception: {$e->getMessage()} */"; } if ($options['header']) { $output = static::_prependHeader($output); } if ($options['cache']) { file_put_contents($css_file, $output); } return $output; }
public function render($data, $contents = array(), array $options = array()) { $this->_mustache(); $defaults = array('id' => '', 'class' => '', 'hash' => true, 'right' => ''); $options += $defaults; $tabs = $panes = array(); foreach ($data as $slug => $tab) { if (!is_array($tab)) { $tab = array('name' => $tab); } $slug = is_numeric($slug) ? strtolower(Inflector::slug($tab['name'])) : $slug; $tab = $this->_tab($tab, array('id' => $slug)); if (empty($tab['url'])) { $tab['url'] = $options['hash'] ? sprintf('#%s', $slug) : $slug; } else { $slug = str_replace('#', '', $tab['url']); } if (isset($tab['content'])) { $panes[] = $this->_pane($tab['content'], $tab); unset($tab['content']); } $tabs[] = $tab; } $params = $options += compact('tabs', 'panes'); return $this->mustache->render('tabs', $params, array('library' => 'li3_bootstrap')); }
public function testTransliteration() { $data = array( 'transliteration' => array( '\$' => 'dollar', '&' => 'and' ) ); Catalog::write('runtime', 'inflection', 'en', $data); Inflector::rules( 'transliteration', Catalog::read('runtime', 'inflection.transliteration', 'en') ); $result = Inflector::slug('this & that'); $expected = 'this-and-that'; $this->assertEqual($expected, $result); $data = array( 'transliteration' => array( 't' => 'd', '&' => 'und' ) ); Catalog::write('runtime', 'inflection', 'de', $data); Inflector::rules( 'transliteration', Catalog::read('runtime', 'inflection.transliteration', 'de') ); $result = Inflector::slug('this & that'); $expected = 'dhis-und-dhad'; $this->assertEqual($expected, $result); }
public static function add($name, array $config = array()) { $session = static::$_classes['session']; $name = strtolower(Inflector::slug($name)); $defaults = array('adapter' => '', 'name' => $name); return static::$_configurations[$name] = $config + $defaults; }
/** * Initialization of the cookie adapter. * * @return void */ protected function _init() { parent::_init(); if (!$this->_config['name']) { $this->_config['name'] = Inflector::slug(basename(LITHIUM_APP_PATH)) . 'cookie'; } }
/** * Auto run the help command * * @param string $name COMMAND to get help * @return void */ public function run($name = null) { if (!$name) { $this->out('COMMANDS', 'heading1', 2); $commands = Libraries::locate('command', null, array('recursive' => false)); foreach ($commands as $command) { $info = Inspector::info($command); $name = strtolower(Inflector::slug($info['shortName'])); $this->out($this->_pad($name), 'heading2'); $this->out($this->_pad($info['description']), 2); } $message = 'See `{:command}li3 help COMMAND{:end}`'; $message .= ' for more information on a specific command.'; $this->out($message, 2); return true; } $name = Inflector::classify($name); if (!($class = Libraries::locate('command', $name))) { $this->error("{$name} not found"); return false; } if (strpos($name, '\\') !== false) { $name = join('', array_slice(explode("\\", $name), -1)); } $name = strtolower(Inflector::slug($name)); $methods = $this->_methods($class); $properties = $this->_properties($class); $this->out('USAGE', 'heading1'); $this->out($this->_pad(sprintf("{:command}li3 %s{:end}{:option}%s{:end} [ARGS]", $name ?: 'COMMAND', array_reduce($properties, function ($a, $b) { return "{$a} {$b['usage']}"; })))); $info = Inspector::info($class); if (!empty($info['description'])) { $this->nl(); $this->out('DESCRIPTION'); $this->out($this->_pad(strtok($info['description'], "\n"), 1)); $this->nl(); } if ($properties || $methods) { $this->out('OPTIONS', 'heading2'); } if ($properties) { $this->_render($properties); } if ($methods) { $this->_render($methods); } return true; }
/** * Initialize location * Apply method filters */ public static function __init() { static::config(); static::applyFilter('mkdir', function ($self, $params, $chain) { $name = explode('/', $params['name']); foreach ($name as $i => $s) { $name[$i] = Inflector::slug($s); } $params['name'] = join('/', $name); return $chain->next($self, $params, $chain); }); static::applyFilter('upload', function ($self, $params, $chain) { $file = explode('.', $params['file']['name']); $end = count($file) - 1; $extension = $file[$end]; unset($file[$end]); $name = join($file); $params['file']['name'] = Inflector::slug($name) . ".{$extension}"; return $chain->next($self, $params, $chain); }); static::applyFilter('copy', function ($self, $params, $chain) { $source = trim($params['source'], '/'); $dest = trim($params['dest'], '/'); if (substr($dest, 0, strlen($source)) === $source) { return false; } return $chain->next($self, $params, $chain); }); static::applyFilter('move', function ($self, $params, $chain) { $source = trim($params['oldname'], '/'); $dest = trim($params['newname'], '/'); if (substr($dest, 0, strlen($source)) === $source) { return false; } return $chain->next($self, $params, $chain); }); }
public function testDefaultCookieName() { $cookie = new Cookie(); $expected = Inflector::slug(basename(Libraries::get(true, 'path'))) . 'cookie'; $this->assertEqual($expected, $cookie->key()); }
/** * Connect a resource to the `Router`. * * @param string $resource The name of the resource * @param array $options */ public static function bind($resource, $options = array()) { $resources = explode('/', $resource); $splitCount = count($resources); $class = static::$_classes['route']; $scope = isset($options['scope']) ? '(/{:scope:' . strtolower($options['scope']) . '})' : ''; if ($splitCount > 1) { $controller = $resources[$splitCount - 1]; $resource = Inflector::underscore($controller); for ($i = $splitCount - 2; $i >= 0; $i--) { $resource = Inflector::underscore($resources[$i]) . '/{:' . Inflector::underscore($resources[$i]) . '_id:[0-9a-f]{24}|[0-9]+}/' . $resource; } } else { $resource = Inflector::pluralize(strtolower(Inflector::slug($resource))); $controller = $resource; $resource = Inflector::underscore($resource); } $types = static::$_types; if (isset($options['types'])) { $types = $options['types'] + $types; } if (isset($options['except'])) { foreach (array_intersect((array) $options['except'], array_keys($types)) as $k) { unset($types[$k]); } } if (isset($options['only'])) { foreach (array_keys($types) as $k) { if (!in_array($k, (array) $options['only'])) { unset($types[$k]); } } } $configs = array(); foreach ($types as $action => $params) { $config = array('template' => $scope . String::insert($params['template'], array('resource' => $resource)), 'params' => $params['params'] + array('controller' => $controller, 'action' => isset($params['action']) ? $params['action'] : $action)); $configs[] = $config; if (@$options['type_support'] != false) { if (isset($params['type_support']) && $params['type_support'] || @$options['type_support']) { $config = array('template' => $scope . String::insert($params['template'] . '(.{:type:\\w+})?', array('resource' => $resource)), 'params' => $params['params'] + array('controller' => $controller, 'action' => isset($params['action']) ? $params['action'] : $action)); $configs[] = $config; } } } return $configs; }
public function __construct(array $config = array()) { $self =& $this; $defaults = array('base' => array(), 'text' => array(), 'textarea' => array(), 'select' => array('multiple' => false), 'attributes' => array('id' => function ($method, $name, $options) use(&$self) { if (in_array($method, array('create', 'end', 'label', 'error'))) { return; } if (!$name || $method == 'hidden' && $name == '_method') { return; } $id = Inflector::camelize(Inflector::slug($name)); $model = ($binding = $self->binding()) ? $binding->model() : null; return $model ? basename(str_replace('\\', '/', $model)) . $id : $id; }, 'name' => function ($method, $name, $options) { if (!strpos($name, '.')) { return $name; } $name = explode('.', $name); $first = array_shift($name); return $first . '[' . join('][', $name) . ']'; })); parent::__construct(Set::merge($defaults, $config)); }
<?php $nav = $panels = ''; foreach ($tabs as $tab => $fields) { $slug = strtolower(\lithium\util\Inflector::slug($tab)); $active = $nav ? '' : ' active'; $nav .= sprintf('<li class="%s"><a href="#%s" data-toggle="tab">%s</a></li>', $active, $slug, $tab); $panels .= sprintf('<div class="tab-pane%s" id="%s">', $active, $slug); $panels .= $this->scaffold->render('form.fields', compact('fields', 'skip', 'readonly')); $panels .= '</div>'; } ?> <div class="row"> <div class="col-md-4"> <h3><?php echo $this->scaffold->human; ?> meta</h3> <div class="well"> <div class="form-group"> <?php echo $this->scaffold->render('form.meta', compact('skip', 'readonly')); ?> </div> </div> </div> <div class="col-md-8"> <h3><?php echo $this->scaffold->human;
public function testDefaultCookieName() { $cookie = new Cookie(); $expected = Inflector::slug(basename(LITHIUM_APP_PATH)) . 'cookie'; $this->assertEqual($expected, $cookie->key()); }
/** * Output the formatted available commands. * * @return void */ protected function _renderCommands() { $commands = Libraries::locate('command', null, array('recursive' => false)); foreach ($commands as $key => $command) { $library = strtok($command, '\\'); if (!$key || strtok($commands[$key - 1], '\\') != $library) { $this->out("{:heading}COMMANDS{:end} {:blue}via {$library}{:end}"); } $info = Inspector::info($command); $name = strtolower(Inflector::slug($info['shortName'])); $this->out($this->_pad($name), 'heading'); $this->out($this->_pad($info['description']), 2); } $message = 'See `{:command}li3 help COMMAND{:end}`'; $message .= ' for more information on a specific command.'; $this->out($message, 2); }
public function getPlaceUrl() { return ROOT_URL . \strtolower(Inflector::slug($this->getAddress()->getCity()->getState())) . '/' . \strtolower(Inflector::slug($this->getAddress()->getCity()->getName())) . '/' . \strtolower(Inflector::slug($this->getCategory())) . '/' . \strtolower(Inflector::slug($this->getName())) . '/' . $this->getId() . '.html'; }
public function __construct(array $config = array()) { $self =& $this; $defaults = array('base' => array(), 'text' => array(), 'textarea' => array(), 'select' => array('multiple' => false), 'attributes' => array('id' => function ($method, $name, $options) use(&$self) { if (in_array($method, array('create', 'end', 'label', 'error'))) { return; } if (!$name || $method === 'hidden' && $name === '_method') { return; } $info = $self->binding($name); $model = $info->class; $id = Inflector::camelize(Inflector::slug($info->name)); return $model ? basename(str_replace('\\', '/', $model)) . $id : $id; }, 'name' => function ($method, $name, $options) { if (!strpos($name, '.')) { return $name; } $name = explode('.', $name); $first = array_shift($name); return $first . '[' . join('][', $name) . ']'; }), 'binding' => function ($object, $name = null) { $result = compact('name') + array('data' => null, 'errors' => null, 'class' => null); if (is_object($object)) { $result = compact('name') + array('data' => $object->data($name), 'errors' => $object->errors($name), 'class' => $object->model()); } return (object) $result; }); parent::__construct(Set::merge($defaults, $config)); }
/** * Generates a pretty URL for the user document. * * @return string */ private function _generateUrl($id = null) { $url = ''; $url_field = User::urlField(); $url_separator = User::urlSeparator(); if ($url_field != '_id' && !empty($url_field)) { if (is_array($url_field)) { foreach ($url_field as $field) { if (isset($this->request->data[$field]) && $field != '_id') { $url .= $this->request->data[$field] . ' '; } } $url = Inflector::slug(trim($url), $url_separator); } else { $url = Inflector::slug($this->request->data[$url_field], $url_separator); } } // Last check for the URL...if it's empty for some reason set it to "user" if (empty($url)) { $url = 'user'; } // Then get a unique URL from the desired URL (numbers will be appended if URL is duplicate) this also ensures the URLs are lowercase $options = array('url' => $url, 'model' => 'li3b_users\\models\\User'); // If an id was passed, this will ensure a document can use its own pretty URL on update instead of getting a new one. if (!empty($id)) { $options['id'] = $id; } return Util::uniqueUrl($options); }
/** * Returns a sluggified exercise name based off the class name. * * @return string */ public function exerciseName($className) { $info = Inspector::info($className); return strtolower(Inflector::slug($info['shortName'])); }
/** * Generic create() action. * The trick here is that $this->calling_class and $this->calling_method will hold a string * reference for which extended class called this create() method. We need that in order to * get the proper records and access. */ public function create() { // get the "_type" ... page_type, user_type, or block_type $model = Inflector::classify(Inflector::singularize($this->request->params['controller'])); $modelClass = 'minerva\models\\'.$model; $x_type = strtolower($model) . '_type'; // or set it to "all" if there wasn't a param passed $type = ((isset($this->request->params[$x_type])) && (in_array($x_type, $this->library_fields))) ? $this->request->params[$x_type]:'all'; // this just checks access $this->getDocument(array('action' => $this->calling_method, 'request' => $this->request, 'find_type' => false)); // Get the model class we should be using for this (it could be an extended class from a library) $modelClass = $modelClass::getMinervaModel($model, $type); // Get the name for the page, so if another type library uses the "admin" (core) templates for this action, it will be shown $display_name = $modelClass::display_name(); // Lock the schema. We don't want any unwanted data passed to be saved $modelClass::meta('locked', true); // Get the fields so the view template can iterate through them and build the form $fields = $modelClass::schema(); // Don't need to have these fields in the form unset($fields['_id']); // If a page type was passed in the params, we'll need it to save to the page document. $fields[$x_type]['form']['value'] = ($type != 'all') ? $type:null; // If data was passed, set some more data and save if ($this->request->data) { $document = $modelClass::create(); $now = date('Y-m-d h:i:s'); $this->request->data['created'] = $now; $this->request->data['modified'] = $now; $this->request->data['url'] = Util::unique_url(array( 'url' => Inflector::slug($this->request->data['title']), 'model' => $modelClass )); $user = Auth::check('minerva_user'); if($user) { $this->request->data['owner_id'] = $user['_id']; } else { // TODO: possible for anonymous users to create things? do we need to put in any value here? $this->request->data['owner_id'] = ''; } // (note: this will only be useful for UsersController) if(($this->request->params['controller'] == 'users') && (isset($this->request->data['password']))) { $this->request->data['password'] = String::hash($this->request->data['password']); } // Save if($document->save($this->request->data)) { FlashMessage::set('The content has been created successfully.', array('options' => array('type' => 'success', 'pnotify_title' => 'Success', 'pnotify_opacity' => .8))); $this->redirect(array('controller' => $this->request->params['controller'], 'action' => 'index')); } else { FlashMessage::set('The content could not be saved, please try again.', array('options' => array('type' => 'error', 'pnotify_title' => 'Error', 'pnotify_opacity' => .8))); } } if(empty($document)) { $document = $modelClass::create(); // Create an empty page object } $this->set(compact('document', 'fields', 'display_name')); }
/** * Returns the name of the log file for this exercise. * * @return void */ protected function _getLogFileName() { $tmpDir = LITHIUM_APP_PATH . '/resources/tmp'; $filename = $tmpDir . '/' . \lithium\util\Inflector::slug(__CLASS__) . '.log'; return $filename; }
/** * generates a script-tag with a mustache template in it * * The script tag has the given template as content and has a * attribute type="text/html" which is proposed as a template * to be used clientside with javascript. * * @param string $name Name of template to include * @param string $options additional options, to put into the view()->render() * @return string the script-tag with correct attributes and template */ public function script($name, $options = array()) { $defaults = array('name' => 'tpl_' . Inflector::slug($name, '_')); $options += $defaults; $options += array('template' => $this->template($name, array(), $options)); $scriptblock = '<script id="{:name}" type="text/html" charset="utf-8">{:template}</script>'; return String::insert($scriptblock, $options); }
/** * Tests adding transliterated characters to the map used in `Inflector::slug()`. * * @return void */ public function testAddTransliterations() { $this->assertEqual(Inflector::slug('Montréal'), 'Montreal'); $this->assertNotEqual(Inflector::slug('Écaussines'), 'Ecaussines'); Inflector::rules('transliteration', array('/É|Ê/' => 'E')); $this->assertEqual(Inflector::slug('Écaussines-d\'Enghien'), 'Ecaussines-d-Enghien'); $this->assertNotEqual(Inflector::slug('JØRGEN'), 'JORGEN'); Inflector::rules('transliteration', array('/Ø/' => 'O')); $this->assertEqual(Inflector::slug('JØRGEN'), 'JORGEN'); $this->assertNotEqual(Inflector::slug('ÎÍ'), 'II'); Inflector::rules('transliteration', array('/Î|Í/' => 'I')); $this->assertEqual(Inflector::slug('ÎÍ'), 'II'); $this->assertEqual(Inflector::slug('ABc'), 'ABc'); Inflector::rules('transliteration', array('AB' => 'a')); $this->assertEqual(Inflector::slug('ABc'), 'aac'); }