Changes the form of a word from plural to singular.
public static singularize ( string $word ) : string | ||
$word | string | Word in plural form. |
리턴 | string | Word in singular form. |
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); }
/** * Data source READ operation. * * @param string $query * @param array $options * @return mixed */ public function read($query, array $options = array()) { $params = $query->export($this); list($path, $data) = $this->_request($params); $source = $params['source']; $name = Inflector::singularize($source); $data = str_replace("\n", '', $this->_render("{$name}.list")); $result = $this->connection->post('xml-in', $data, array('type' => 'xml')); $result = $data = json_decode(json_encode(simplexml_load_string($result)), true); return $this->item($query->model(), array($result[$source]), array('class' => 'set')); }
public function run($name = null, $null = null) { $library = Libraries::get($this->library); if (empty($library['prefix'])) { return false; } $model = Inflector::classify($name); $use = "\\{$library['prefix']}models\\{$model}"; $params = array('namespace' => "{$library['prefix']}controllers", 'use' => $use, 'class' => "{$name}Controller", 'model' => $model, 'singular' => Inflector::singularize(Inflector::underscore($name)), 'plural' => Inflector::pluralize(Inflector::underscore($name))); if ($this->_save($this->template, $params)) { $this->out("{$params['class']} created in {$params['namespace']}."); return true; } return false; }
protected function _init() { parent::_init(); $config = $this->_config; $singularName = $config['name']; if ($config['type'] == 'hasMany') { $singularName = Inflector::singularize($config['name']); } if (!$config['to']) { $assoc = preg_replace("/\\w+\$/", "", $config['from']) . $singularName; $config['to'] = class_exists($assoc) ? $assoc : Libraries::locate('models', $assoc); } if (!$config['fieldName']) { $config['fieldName'] = lcfirst($config['name']); } $config['keys'] = $this->_keys($config['keys'], $config); $this->_config = $config; }
public function relationship($class, $type, $name, array $config = array()) { $field = Inflector::underscore(Inflector::singularize($name)); $key = "{$field}_id"; $primary = $class::meta('key'); if (is_array($primary)) { $key = array_combine($primary, $primary); } elseif ($type === 'hasMany' || $type === 'hasOne') { if ($type === 'hasMany') { $field = Inflector::pluralize($field); } $secondary = Inflector::underscore(Inflector::singularize($class::meta('name'))); $key = array($primary => "{$secondary}_id"); } $from = $class; $fieldName = $field; $config += compact('type', 'name', 'key', 'from', 'fieldName'); return $this->_instance('relationship', $config); }
/** * Get the singular variable to use for data in controller methods. * * @param string $request * @return string */ protected function _singular($request) { return Inflector::singularize(Inflector::camelize($request->action, false)); }
/** * Returns the field name of a relation name (underscore). * * @param string The type of the relation. * @param string The name of the relation. * @return string */ public function relationFieldName($type, $name) { $fieldName = Inflector::underscore($name); if (preg_match('/Many$/', $type)) { $fieldName = Inflector::pluralize($fieldName); } else { $fieldName = Inflector::singularize($fieldName); } return $fieldName; }
/** * Defines or modifies the default settings of a relationship between two models. * * @param string $class * @param string $type * @param string $name * @param array $config * @return array Returns an array containing the configuration for a model relationship. */ public function relationship($class, $type, $name, array $config = array()) { $singularName = $type == 'hasMany' ? Inflector::singularize($name) : $name; $keys = $type == 'belongsTo' ? $class::meta('name') : $singularName; $keys = Inflector::underscore($keys) . '_id'; $from = $class; return $this->_instance('relationship', $config + compact('type', 'name', 'keys', 'from')); }
public function testLazyMetadataInit() { MockPost::config(array('schema' => new Schema(array('fields' => array('id' => array('type' => 'integer'), 'name' => array('type' => 'string'), 'label' => array('type' => 'string')))))); $this->assertIdentical('mock_posts', MockPost::meta('source')); $this->assertIdentical('name', MockPost::meta('title')); $this->assertEmpty(MockPost::meta('unexisting')); $config = array('schema' => new Schema(array('fields' => array('id' => array('type' => 'integer'), 'name' => array('type' => 'string'), 'label' => array('type' => 'string')))), 'initializers' => array('source' => function ($self) { return Inflector::tableize($self::meta('name')); }, 'name' => function ($self) { return Inflector::singularize('CoolPosts'); }, 'title' => function ($self) { static $i = 1; return 'label' . $i++; })); MockPost::reset(); MockPost::config($config); $this->assertIdentical('cool_posts', MockPost::meta('source')); $this->assertIdentical('label1', MockPost::meta('title')); $this->assertNotIdentical('label2', MockPost::meta('title')); $this->assertIdentical('label1', MockPost::meta('title')); $meta = MockPost::meta(); $this->assertIdentical('label1', $meta['title']); $this->assertIdentical('CoolPost', MockPost::meta('name')); MockPost::reset(); unset($config['initializers']['title']); $config['initializers']['source'] = function ($self) { return Inflector::underscore($self::meta('name')); }; MockPost::config($config); $this->assertIdentical('cool_post', MockPost::meta('source')); $this->assertIdentical('name', MockPost::meta('title')); $this->assertIdentical('CoolPost', MockPost::meta('name')); MockPost::reset(); MockPost::config($config); $expected = array('class' => 'lithium\\tests\\mocks\\data\\MockPost', 'connection' => false, 'key' => 'id', 'name' => 'CoolPost', 'title' => 'name', 'source' => 'cool_post'); $this->assertEqual($expected, MockPost::meta()); }
/** * Checks if the data container values are inside indexed arrays from binding. * Get the values from the binding coresponding to the model if such exists. * * @see lithium\security\auth\adapter\Form::check * @param array $data The array of raw form data. * @return array Original or sub array of the form data. */ protected function _data($data) { $model = $this->_model; $index = strtolower(Inflector::singularize($model::meta('name'))); return isset($data[$index]) && is_array($data[$index]) ? $data[$index] : $data; }
/** * Creates a relationship binding between this model and another. Overwritten to allow model to model relations seperate of data source relations. * * @see lithium\data\model\Relationship * @param string $type The type of relationship to create. Must be one of `'hasOne'`, * `'hasMany'` or `'belongsTo'`. * @param string $name The name of the relationship. If this is also the name of the model, * the model must be in the same namespace as this model. Otherwise, the * fully-namespaced path to the model class must be specified in `$config`. * @param array $config Any other configuration that should be specified in the relationship. * See the `Relationship` class for more information. * @return object Returns an instance of the `Relationship` class that defines the connection. */ public static function bind($type, $name, array $config = array()) { $defaults = array('default' => false); // li3_embedded catch to make embedding easier if (isset($config['embedded']) && !isset($config['default'])) { if (!empty($config['embedded'])) { $config['default'] = true; } else { $config['default'] = false; } } $config += $defaults; $self = static::_object(); if (!isset($config['to']) && isset($config['class'])) { $config['to'] = $config['class']; } if (!isset($config['to'])) { $config['to'] = $name; } $config['to'] = Libraries::locate('models', $config['to']); $targetModel = $config['to']; //TODO, add general exception option & add mongo exception for non embedded if (!empty($targetModel) && $config['default'] === false) { // continue on if default lithium relationship will not work if (isset($config['fieldName'])) { $fieldName = $config['fieldName']; } else { $fieldName = $name; if ($type == 'hasMany') { $fieldName = Inflector::pluralize($name); } else { $fieldName = Inflector::singularize($name); } $fieldName = Inflector::underscore($fieldName); } $key = "{$fieldName}_id"; $from = get_called_class(); $config += compact('type', 'name', 'key', 'from', 'fieldName'); $connection = static::connection(); $relationship = $connection->invokeMethod('_instance', array('relationship', $config)); if (!empty($relationship)) { $self->_alternateRelations[$name] = $relationship; return null; } } return parent::bind($type, $name, $config); }
public function testAddingSingularizationRules() { $before = Inflector::rules('singular'); $result = Inflector::singularize('errata'); $this->assertNull(Inflector::rules('singular', array('/rata/' => '\\1ratus'))); $this->assertEqual(Inflector::singularize('errata'), $result); Inflector::reset(); $this->assertNotEqual(Inflector::singularize('errata'), $result); $after = Inflector::rules('singular'); $expected = array('rules', 'irregular', 'uninflected', 'regexUninflected', 'regexIrregular'); $this->assertEqual(array_keys($before), $expected); $this->assertEqual(array_keys($after), $expected); $result = array_diff($after['rules'], $before['rules']); $this->assertEqual($result, array('/rata/' => '\\1ratus')); foreach (array('irregular', 'uninflected', 'regexUninflected', 'regexIrregular') as $key) { $this->assertIdentical($before[$key], $after[$key]); } $this->assertNull(Inflector::rules('singular', array('rules' => array('/rata/' => '\\1ratus')))); $this->assertIdentical(Inflector::rules('singular'), $after); }
/** * If no resource parameter definition exists for a method, generate a default mapping. * * @param string $method The class method name to be called, i.e. `'index'`, `'add'`, etc. * @return array Returns an array where the key is the singular or plural name of the `Resource` * class, i.e. `'post'` or `'posts'`, and the value is a sub-array with an * optionally-non-namespaced model name or fully-qualified class name as the first * value, and a `'call'` key, indicating the name of the class method to call. */ protected function _default($method) { $name = lcfirst(static::_name()); $isPlural = $method == 'index'; $call = array(true => 'first', $isPlural => 'all', $method == 'add' => 'create'); $key = $isPlural ? Inflector::pluralize($name) : Inflector::singularize($name); return array($key => array(static::binding(), 'call' => $call[true], 'required' => !$isPlural)); }
/** * 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; }
/** * Generic delete() action. * The trick here is that $this->calling_class and $this->calling_method will hold a string * reference for which extended class called this delete() method. We need that in order to * get the proper records and access. */ public function delete() { // get the "_type" ... page_type, user_type, or block_type $model = Inflector::classify(Inflector::singularize($this->request->params['controller'])); $modelClass = 'minerva\models\\'.$model; $conditions = array(); if(isset($this->request->params['id'])) { $conditions = array('id' => $this->request->params['id']); } if(isset($this->request->params['url'])) { $conditions = array('url' => $this->request->params['url']); } if(empty($conditions)) { $this->redirect(array('controller' => $this->request->params['controller'], 'action' => 'index')); } $document = $this->getDocument(array( 'action' => __METHOD__, 'request' => $this->request, 'find_type' => 'first', 'conditions' => $conditions )); if($document->delete()) { FlashMessage::set('The content has been deleted.', 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 deleted, please try again.', array('options' => array('type' => 'error', 'pnotify_title' => 'Error', 'pnotify_opacity' => .8))); $this->redirect(array('controller' => $this->request->params['controller'], 'action' => 'index')); } }