lithium\util\Inflector::rules PHP Method

rules() public static method

Gets or adds inflection and transliteration rules.
public static rules ( string $type, array $config = [] ) : mixed
$type string Either `'transliteration'`, `'uninflected'`, `'singular'` or `'plural'`.
$config array
return mixed If `$config` is empty, returns the rules list specified by `$type`, otherwise returns `null`.
    public static function rules($type, $config = array())
    {
        $var = '_' . $type;
        if (!isset(static::${$var})) {
            return null;
        }
        if (empty($config)) {
            return static::${$var};
        }
        switch ($type) {
            case 'transliteration':
                $_config = array();
                foreach ($config as $key => $val) {
                    if ($key[0] !== '/') {
                        $key = '/' . join('|', array_filter(preg_split('//u', $key))) . '/';
                    }
                    $_config[$key] = $val;
                }
                static::$_transliteration = array_merge($_config, static::$_transliteration, $_config);
                break;
            case 'uninflected':
                static::$_uninflected = array_merge(static::$_uninflected, (array) $config);
                static::$_plural['regexUninflected'] = null;
                static::$_singular['regexUninflected'] = null;
                foreach ((array) $config as $word) {
                    unset(static::$_singularized[$word], static::$_pluralized[$word]);
                }
                break;
            case 'singular':
            case 'plural':
                if (isset(static::${$var}[key($config)])) {
                    foreach ($config as $rType => $set) {
                        static::${$var}[$rType] = array_merge($set, static::${$var}[$rType], $set);
                        if ($rType === 'irregular') {
                            $swap = $type === 'singular' ? '_plural' : '_singular';
                            static::${$swap}[$rType] = array_flip(static::${$var}[$rType]);
                        }
                    }
                } else {
                    static::${$var}['rules'] = array_merge($config, static::${$var}['rules'], $config);
                }
                break;
        }
    }

Usage Example

Beispiel #1
0
	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);
	}
All Usage Examples Of lithium\util\Inflector::rules