From edd7ec421485888ad7fe4999d1c44485ffba24c0 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Mon, 20 May 2013 00:38:25 +0200 Subject: [PATCH 01/20] Inflector second revision --- yii/helpers/base/Inflector.php | 58 +++++++++++++----------------------------- 1 file changed, 17 insertions(+), 41 deletions(-) diff --git a/yii/helpers/base/Inflector.php b/yii/helpers/base/Inflector.php index 8c2d487..33b3dec 100644 --- a/yii/helpers/base/Inflector.php +++ b/yii/helpers/base/Inflector.php @@ -357,22 +357,15 @@ class Inflector * Converts an underscored or CamelCase word into a English * sentence. * - * The titleize function converts text like "WelcomePage", - * "welcome_page" or "welcome page" to this "Welcome - * Page". - * If second parameter is set to 'first' it will only - * capitalize the first character of the title. - * - * @param string $word Word to format as tile - * @param string $uppercase If set to 'first' it will only uppercase the - * first character. Otherwise it will uppercase all - * the words in the title. - * @return string Text formatted as title + * @param string $words + * @param bool $ucAll whether to set all words to uppercase + * @return string */ - public static function titleize($word, $uppercase = '') + public static function titleize($words, $ucAll = false) { - $uppercase = $uppercase == 'first' ? 'ucfirst' : 'ucwords'; - return $uppercase(static::humanize(static::underscore($word))); + + $words = static::humanize(static::underscore($words), $ucAll); + return $ucAll ? ucwords($words) : ucfirst($words); } /** @@ -383,8 +376,8 @@ class Inflector * "who's online" will be converted to "WhoSOnline" * * @see variablize - * @param string $word Word to convert to camel case - * @return string UpperCamelCasedWord + * @param string $word the word to CamelCase + * @return string */ public static function camelize($word) { @@ -392,44 +385,27 @@ class Inflector } /** - * Converts a word "into_it_s_underscored_version" + * Converts any "CamelCased" or "ordinary Word" into an "underscored_word". * - * Convert any "CamelCased" or "ordinary Word" into an - * "underscored_word". - * - * This can be really useful for creating friendly URLs. - * - * @access public - * @static - * @param string $word Word to underscore - * @return string Underscored word + * @param string $words the word(s) to underscore + * @return string */ - public static function underscore($word) + public static function underscore($words) { - return strtolower( - preg_replace( - '/[^A-Z^a-z^0-9]+/', - '_', - preg_replace( - '/([a-zd])([A-Z])/', - '1_2', - preg_replace('/([A-Z]+)([A-Z][a-z])/', '1_2', $word) - ) - ) - ); + return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $words)); } /** * Returns a human-readable string from $word * * @param string $word the string to humanize - * @param bool $uppercase whether to set all words to uppercase or not + * @param bool $ucAll whether to set all words to uppercase or not * @return string */ - public static function humanize($word, $uppercase = false) + public static function humanize($word, $ucAll = false) { $word = str_replace('_', ' ', preg_replace('/_id$/', '', $word)); - return $uppercase ? ucwords($word) : ucfirst($word); + return $ucAll ? ucwords($word) : ucfirst($word); } /** From 82a6281c659e7245872438319081ea4518a42cdd Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Tue, 21 May 2013 01:04:28 +0200 Subject: [PATCH 02/20] added tests --- tests/unit/framework/helpers/InflectorTest.php | 51 ++++++++++++++++++++++++++ yii/helpers/Inflector.php | 18 +++++++++ yii/helpers/base/Inflector.php | 2 +- 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 tests/unit/framework/helpers/InflectorTest.php create mode 100644 yii/helpers/Inflector.php diff --git a/tests/unit/framework/helpers/InflectorTest.php b/tests/unit/framework/helpers/InflectorTest.php new file mode 100644 index 0000000..4d0f133 --- /dev/null +++ b/tests/unit/framework/helpers/InflectorTest.php @@ -0,0 +1,51 @@ +assertEquals("people", Inflector::pluralize('person')); + $this->assertEquals("fish", Inflector::pluralize('fish')); + $this->assertEquals("men", Inflector::pluralize('man')); + $this->assertEquals("tables", Inflector::pluralize('table')); + } + + public function testSingularize() + { + $this->assertEquals("person", Inflector::singularize('people')); + $this->assertEquals("fish", Inflector::singularize('fish')); + $this->assertEquals("man", Inflector::singularize('men')); + $this->assertEquals("table", Inflector::singularize('tables')); + } + + public function testTitleize() + { + $this->assertEquals("Me my self and i", Inflector::titleize('MeMySelfAndI')); + $this->assertEquals("Me My Self And I", Inflector::titleize('MeMySelfAndI', true)); + } + + public function testCamelize() + { + $this->assertEquals("MeMySelfAndI", Inflector::camelize('me my_self-andI')); + } + + public function testUnderscore() + { + $this->assertEquals("me_my_self_and_i", Inflector::underscore('Me my self and I')); + } + + public function testHumanize() + { + $this->assertEquals("Me my self and i", Inflector::humanize('me_my_self_and_i')); + $this->assertEquals("Me My Self And i", Inflector::humanize('me_my_self_and_i'), true); + } + +} diff --git a/yii/helpers/Inflector.php b/yii/helpers/Inflector.php new file mode 100644 index 0000000..72f77a7 --- /dev/null +++ b/yii/helpers/Inflector.php @@ -0,0 +1,18 @@ + + * @since 2.0 + */ +class Inflector extends base\Inflector +{ +} diff --git a/yii/helpers/base/Inflector.php b/yii/helpers/base/Inflector.php index 33b3dec..51317c8 100644 --- a/yii/helpers/base/Inflector.php +++ b/yii/helpers/base/Inflector.php @@ -10,7 +10,7 @@ namespace yii\helpers\base; use Yii; /** - * Inflector pluralizes and singularizes English nouns. It also contains other useful methods. + * Inflector pluralizes and singularizes English nouns. It also contains some other useful methods. * * @author Antonio Ramirez * @since 2.0 From 3575b99650e57d26e52497d90598ef0a2b378f82 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Tue, 21 May 2013 08:22:20 +0200 Subject: [PATCH 03/20] finished all tests --- tests/unit/framework/helpers/InflectorTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/unit/framework/helpers/InflectorTest.php b/tests/unit/framework/helpers/InflectorTest.php index 4d0f133..36700f4 100644 --- a/tests/unit/framework/helpers/InflectorTest.php +++ b/tests/unit/framework/helpers/InflectorTest.php @@ -48,4 +48,28 @@ class InflectorTest extends TestCase $this->assertEquals("Me My Self And i", Inflector::humanize('me_my_self_and_i'), true); } + public function testVariablize() + { + $this->assertEquals("customerTable", Inflector::variablize('customer_table')); + } + + public function testTableize() + { + $this->assertEquals("customer_tables", Inflector::tableize('customerTable')); + } + + public function testSlug() + { + $this->assertEquals("this-is-a-title", Inflector::humanize('this is a title')); + } + + public function testClassify() + { + $this->assertEquals("CustomerTable", Inflector::classify('customer_tables')); + } + + public function testOrdinalize() + { + $this->assertEquals("21st", Inflector::humanize('21')); + } } From f9222a083a4e09e5ebde5189e389de358206f973 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Tue, 21 May 2013 08:42:38 +0200 Subject: [PATCH 04/20] fix failing test --- tests/unit/framework/helpers/InflectorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/framework/helpers/InflectorTest.php b/tests/unit/framework/helpers/InflectorTest.php index 36700f4..859d8bf 100644 --- a/tests/unit/framework/helpers/InflectorTest.php +++ b/tests/unit/framework/helpers/InflectorTest.php @@ -39,7 +39,7 @@ class InflectorTest extends TestCase public function testUnderscore() { - $this->assertEquals("me_my_self_and_i", Inflector::underscore('Me my self and I')); + $this->assertEquals("me_my_self_and_i", Inflector::underscore('MeMySelfAndI')); } public function testHumanize() From 20e18e1bdd702f041c8eba8505f29f3133925fae Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Tue, 21 May 2013 11:35:19 +0200 Subject: [PATCH 05/20] fix phpdoc of underscore --- yii/helpers/base/Inflector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yii/helpers/base/Inflector.php b/yii/helpers/base/Inflector.php index 51317c8..af3f266 100644 --- a/yii/helpers/base/Inflector.php +++ b/yii/helpers/base/Inflector.php @@ -385,7 +385,7 @@ class Inflector } /** - * Converts any "CamelCased" or "ordinary Word" into an "underscored_word". + * Converts any "CamelCased" into an "underscored_word". * * @param string $words the word(s) to underscore * @return string From 641e6ee7cfae41c87b9c00a711c081391b35a3be Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Wed, 22 May 2013 08:34:17 +0200 Subject: [PATCH 06/20] ported inflection methods from StringHelper to Inflector helper class --- framework/yii/base/Model.php | 4 +- framework/yii/db/ActiveRecord.php | 5 +- framework/yii/helpers/base/StringHelper.php | 80 ----------------------- tests/unit/framework/helpers/StringHelperTest.php | 52 --------------- 4 files changed, 5 insertions(+), 136 deletions(-) diff --git a/framework/yii/base/Model.php b/framework/yii/base/Model.php index 98901bf..c7432f5 100644 --- a/framework/yii/base/Model.php +++ b/framework/yii/base/Model.php @@ -9,7 +9,7 @@ namespace yii\base; use ArrayObject; use ArrayIterator; -use yii\helpers\StringHelper; +use yii\helpers\Inflector; use yii\validators\RequiredValidator; use yii\validators\Validator; @@ -504,7 +504,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess */ public function generateAttributeLabel($name) { - return StringHelper::camel2words($name, true); + return Inflector::camel2words($name, true); } /** diff --git a/framework/yii/db/ActiveRecord.php b/framework/yii/db/ActiveRecord.php index 2b7d2b8..dd90782 100644 --- a/framework/yii/db/ActiveRecord.php +++ b/framework/yii/db/ActiveRecord.php @@ -18,6 +18,7 @@ use yii\db\Connection; use yii\db\TableSchema; use yii\db\Expression; use yii\helpers\StringHelper; +use yii\helpers\Inflector; /** * ActiveRecord is the base class for classes representing relational data in terms of objects. @@ -261,14 +262,14 @@ class ActiveRecord extends Model /** * Declares the name of the database table associated with this AR class. - * By default this method returns the class name as the table name by calling [[StringHelper::camel2id()]] + * By default this method returns the class name as the table name by calling [[Inflector::camel2id()]] * with prefix 'tbl_'. For example, 'Customer' becomes 'tbl_customer', and 'OrderItem' becomes * 'tbl_order_item'. You may override this method if the table is not named after this convention. * @return string the table name */ public static function tableName() { - return 'tbl_' . StringHelper::camel2id(StringHelper::basename(get_called_class()), '_'); + return 'tbl_' . Inflector::camel2id(StringHelper::basename(get_called_class()), '_'); } /** diff --git a/framework/yii/helpers/base/StringHelper.php b/framework/yii/helpers/base/StringHelper.php index 646bcbb..5b854ac 100644 --- a/framework/yii/helpers/base/StringHelper.php +++ b/framework/yii/helpers/base/StringHelper.php @@ -65,84 +65,4 @@ class StringHelper } return $path; } - - /** - * Converts a word to its plural form. - * Note that this is for English only! - * For example, 'apple' will become 'apples', and 'child' will become 'children'. - * @param string $name the word to be pluralized - * @return string the pluralized word - */ - public static function pluralize($name) - { - static $rules = array( - '/(m)ove$/i' => '\1oves', - '/(f)oot$/i' => '\1eet', - '/(c)hild$/i' => '\1hildren', - '/(h)uman$/i' => '\1umans', - '/(m)an$/i' => '\1en', - '/(s)taff$/i' => '\1taff', - '/(t)ooth$/i' => '\1eeth', - '/(p)erson$/i' => '\1eople', - '/([m|l])ouse$/i' => '\1ice', - '/(x|ch|ss|sh|us|as|is|os)$/i' => '\1es', - '/([^aeiouy]|qu)y$/i' => '\1ies', - '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', - '/(shea|lea|loa|thie)f$/i' => '\1ves', - '/([ti])um$/i' => '\1a', - '/(tomat|potat|ech|her|vet)o$/i' => '\1oes', - '/(bu)s$/i' => '\1ses', - '/(ax|test)is$/i' => '\1es', - '/s$/' => 's', - ); - foreach ($rules as $rule => $replacement) { - if (preg_match($rule, $name)) { - return preg_replace($rule, $replacement, $name); - } - } - return $name . 's'; - } - - /** - * Converts a CamelCase name into space-separated words. - * For example, 'PostTag' will be converted to 'Post Tag'. - * @param string $name the string to be converted - * @param boolean $ucwords whether to capitalize the first letter in each word - * @return string the resulting words - */ - public static function camel2words($name, $ucwords = true) - { - $label = trim(strtolower(str_replace(array('-', '_', '.'), ' ', preg_replace('/(?assertEquals('э', StringHelper::substr('это', 0, 2)); } - public function testPluralize() - { - $testData = array( - 'move' => 'moves', - 'foot' => 'feet', - 'child' => 'children', - 'human' => 'humans', - 'man' => 'men', - 'staff' => 'staff', - 'tooth' => 'teeth', - 'person' => 'people', - 'mouse' => 'mice', - 'touch' => 'touches', - 'hash' => 'hashes', - 'shelf' => 'shelves', - 'potato' => 'potatoes', - 'bus' => 'buses', - 'test' => 'tests', - 'car' => 'cars', - ); - - foreach ($testData as $testIn => $testOut) { - $this->assertEquals($testOut, StringHelper::pluralize($testIn)); - $this->assertEquals(ucfirst($testOut), ucfirst(StringHelper::pluralize($testIn))); - } - } - - public function testCamel2words() - { - $this->assertEquals('Camel Case', StringHelper::camel2words('camelCase')); - $this->assertEquals('Lower Case', StringHelper::camel2words('lower_case')); - $this->assertEquals('Tricky Stuff It Is Testing', StringHelper::camel2words(' tricky_stuff.it-is testing... ')); - } - - public function testCamel2id() - { - $this->assertEquals('post-tag', StringHelper::camel2id('PostTag')); - $this->assertEquals('post_tag', StringHelper::camel2id('PostTag', '_')); - - $this->assertEquals('post-tag', StringHelper::camel2id('postTag')); - $this->assertEquals('post_tag', StringHelper::camel2id('postTag', '_')); - } - - public function testId2camel() - { - $this->assertEquals('PostTag', StringHelper::id2camel('post-tag')); - $this->assertEquals('PostTag', StringHelper::id2camel('post_tag', '_')); - - $this->assertEquals('PostTag', StringHelper::id2camel('post-tag')); - $this->assertEquals('PostTag', StringHelper::id2camel('post_tag', '_')); - } - public function testBasename() { $this->assertEquals('', StringHelper::basename('')); From 7b98b4241873735dc8517dbb3a05e72634f3ddfa Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Wed, 22 May 2013 08:35:03 +0200 Subject: [PATCH 07/20] added inflection methods from StringHelper + some comment fixes --- framework/yii/helpers/Inflector.php | 18 + framework/yii/helpers/base/Inflector.php | 533 +++++++++++++++++++++++++ tests/unit/framework/helpers/InflectorTest.php | 54 ++- yii/helpers/Inflector.php | 18 - yii/helpers/base/Inflector.php | 499 ----------------------- 5 files changed, 600 insertions(+), 522 deletions(-) create mode 100644 framework/yii/helpers/Inflector.php create mode 100644 framework/yii/helpers/base/Inflector.php delete mode 100644 yii/helpers/Inflector.php delete mode 100644 yii/helpers/base/Inflector.php diff --git a/framework/yii/helpers/Inflector.php b/framework/yii/helpers/Inflector.php new file mode 100644 index 0000000..72f77a7 --- /dev/null +++ b/framework/yii/helpers/Inflector.php @@ -0,0 +1,18 @@ + + * @since 2.0 + */ +class Inflector extends base\Inflector +{ +} diff --git a/framework/yii/helpers/base/Inflector.php b/framework/yii/helpers/base/Inflector.php new file mode 100644 index 0000000..f233547 --- /dev/null +++ b/framework/yii/helpers/base/Inflector.php @@ -0,0 +1,533 @@ + + * @since 2.0 + */ +class Inflector +{ + + /** + * @var array rules of plural words + */ + protected static $plural = array( + 'rules' => array( + '/(s)tatus$/i' => '\1\2tatuses', + '/(quiz)$/i' => '\1zes', + '/^(ox)$/i' => '\1\2en', + '/([m|l])ouse$/i' => '\1ice', + '/(matr|vert|ind)(ix|ex)$/i' => '\1ices', + '/(x|ch|ss|sh)$/i' => '\1es', + '/([^aeiouy]|qu)y$/i' => '\1ies', + '/(hive)$/i' => '\1s', + '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', + '/sis$/i' => 'ses', + '/([ti])um$/i' => '\1a', + '/(p)erson$/i' => '\1eople', + '/(m)an$/i' => '\1en', + '/(c)hild$/i' => '\1hildren', + '/(buffal|tomat)o$/i' => '\1\2oes', + '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i', + '/us$/i' => 'uses', + '/(alias)$/i' => '\1es', + '/(ax|cris|test)is$/i' => '\1es', + '/s$/' => 's', + '/^$/' => '', + '/$/' => 's', + ), + 'uninflected' => array( + '.*[nrlm]ese', + '.*deer', + '.*fish', + '.*measles', + '.*ois', + '.*pox', + '.*sheep', + 'people' + ), + 'irregular' => array( + 'atlas' => 'atlases', + 'beef' => 'beefs', + 'brother' => 'brothers', + 'cafe' => 'cafes', + 'child' => 'children', + 'cookie' => 'cookies', + 'corpus' => 'corpuses', + 'cow' => 'cows', + 'ganglion' => 'ganglions', + 'genie' => 'genies', + 'genus' => 'genera', + 'graffito' => 'graffiti', + 'hoof' => 'hoofs', + 'loaf' => 'loaves', + 'man' => 'men', + 'money' => 'monies', + 'mongoose' => 'mongooses', + 'move' => 'moves', + 'mythos' => 'mythoi', + 'niche' => 'niches', + 'numen' => 'numina', + 'occiput' => 'occiputs', + 'octopus' => 'octopuses', + 'opus' => 'opuses', + 'ox' => 'oxen', + 'penis' => 'penises', + 'person' => 'people', + 'sex' => 'sexes', + 'soliloquy' => 'soliloquies', + 'testis' => 'testes', + 'trilby' => 'trilbys', + 'turf' => 'turfs' + ) + ); + /** + * @var array the rules to singular inflector + */ + protected static $singular = array( + 'rules' => array( + '/(s)tatuses$/i' => '\1\2tatus', + '/^(.*)(menu)s$/i' => '\1\2', + '/(quiz)zes$/i' => '\\1', + '/(matr)ices$/i' => '\1ix', + '/(vert|ind)ices$/i' => '\1ex', + '/^(ox)en/i' => '\1', + '/(alias)(es)*$/i' => '\1', + '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us', + '/([ftw]ax)es/i' => '\1', + '/(cris|ax|test)es$/i' => '\1is', + '/(shoe|slave)s$/i' => '\1', + '/(o)es$/i' => '\1', + '/ouses$/' => 'ouse', + '/([^a])uses$/' => '\1us', + '/([m|l])ice$/i' => '\1ouse', + '/(x|ch|ss|sh)es$/i' => '\1', + '/(m)ovies$/i' => '\1\2ovie', + '/(s)eries$/i' => '\1\2eries', + '/([^aeiouy]|qu)ies$/i' => '\1y', + '/([lr])ves$/i' => '\1f', + '/(tive)s$/i' => '\1', + '/(hive)s$/i' => '\1', + '/(drive)s$/i' => '\1', + '/([^fo])ves$/i' => '\1fe', + '/(^analy)ses$/i' => '\1sis', + '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis', + '/([ti])a$/i' => '\1um', + '/(p)eople$/i' => '\1\2erson', + '/(m)en$/i' => '\1an', + '/(c)hildren$/i' => '\1\2hild', + '/(n)ews$/i' => '\1\2ews', + '/eaus$/' => 'eau', + '/^(.*us)$/' => '\\1', + '/s$/i' => '' + ), + 'uninflected' => array( + '.*[nrlm]ese', + '.*deer', + '.*fish', + '.*measles', + '.*ois', + '.*pox', + '.*sheep', + '.*ss' + ), + 'irregular' => array( + 'foes' => 'foe', + 'waves' => 'wave', + 'curves' => 'curve' + ) + ); + + /** + * @var array list of words that should not be inflected + */ + protected static $uninflected = array( + 'Amoyese', + 'bison', + 'Borghese', + 'bream', + 'breeches', + 'britches', + 'buffalo', + 'cantus', + 'carp', + 'chassis', + 'clippers', + 'cod', + 'coitus', + 'Congoese', + 'contretemps', + 'corps', + 'debris', + 'diabetes', + 'djinn', + 'eland', + 'elk', + 'equipment', + 'Faroese', + 'flounder', + 'Foochowese', + 'gallows', + 'Genevese', + 'Genoese', + 'Gilbertese', + 'graffiti', + 'headquarters', + 'herpes', + 'hijinks', + 'Hottentotese', + 'information', + 'innings', + 'jackanapes', + 'Kiplingese', + 'Kongoese', + 'Lucchese', + 'mackerel', + 'Maltese', + '.*?media', + 'mews', + 'moose', + 'mumps', + 'Nankingese', + 'news', + 'nexus', + 'Niasese', + 'Pekingese', + 'Piedmontese', + 'pincers', + 'Pistoiese', + 'pliers', + 'Portuguese', + 'proceedings', + 'rabies', + 'rice', + 'rhinoceros', + 'salmon', + 'Sarawakese', + 'scissors', + 'sea[- ]bass', + 'series', + 'Shavese', + 'shears', + 'siemens', + 'species', + 'swine', + 'testes', + 'trousers', + 'trout', + 'tuna', + 'Vermontese', + 'Wenchowese', + 'whiting', + 'wildebeest', + 'Yengeese' + ); + + /** + * @var array map of special chars and its translation + */ + protected static $transliteration = array( + '/ä|æ|ǽ/' => 'ae', + '/ö|œ/' => 'oe', + '/ü/' => 'ue', + '/Ä/' => 'Ae', + '/Ü/' => 'Ue', + '/Ö/' => 'Oe', + '/À|Á|Â|Ã|Å|Ǻ|Ā|Ă|Ą|Ǎ/' => 'A', + '/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª/' => 'a', + '/Ç|Ć|Ĉ|Ċ|Č/' => 'C', + '/ç|ć|ĉ|ċ|č/' => 'c', + '/Ð|Ď|Đ/' => 'D', + '/ð|ď|đ/' => 'd', + '/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě/' => 'E', + '/è|é|ê|ë|ē|ĕ|ė|ę|ě/' => 'e', + '/Ĝ|Ğ|Ġ|Ģ/' => 'G', + '/ĝ|ğ|ġ|ģ/' => 'g', + '/Ĥ|Ħ/' => 'H', + '/ĥ|ħ/' => 'h', + '/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ/' => 'I', + '/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı/' => 'i', + '/Ĵ/' => 'J', + '/ĵ/' => 'j', + '/Ķ/' => 'K', + '/ķ/' => 'k', + '/Ĺ|Ļ|Ľ|Ŀ|Ł/' => 'L', + '/ĺ|ļ|ľ|ŀ|ł/' => 'l', + '/Ñ|Ń|Ņ|Ň/' => 'N', + '/ñ|ń|ņ|ň|ʼn/' => 'n', + '/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ/' => 'O', + '/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º/' => 'o', + '/Ŕ|Ŗ|Ř/' => 'R', + '/ŕ|ŗ|ř/' => 'r', + '/Ś|Ŝ|Ş|Ș|Š/' => 'S', + '/ś|ŝ|ş|ș|š|ſ/' => 's', + '/Ţ|Ț|Ť|Ŧ/' => 'T', + '/ţ|ț|ť|ŧ/' => 't', + '/Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ/' => 'U', + '/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'u', + '/Ý|Ÿ|Ŷ/' => 'Y', + '/ý|ÿ|ŷ/' => 'y', + '/Ŵ/' => 'W', + '/ŵ/' => 'w', + '/Ź|Ż|Ž/' => 'Z', + '/ź|ż|ž/' => 'z', + '/Æ|Ǽ/' => 'AE', + '/ß/' => 'ss', + '/IJ/' => 'IJ', + '/ij/' => 'ij', + '/Œ/' => 'OE', + '/ƒ/' => 'f' + ); + + /** + * Converts a word to its plural form. + * Note that this is for English only! + * For example, 'apple' will become 'apples', and 'child' will become 'children'. + * @param string $word the word to be pluralized + * @return string the pluralized word + */ + public static function pluralize($word) + { + $unInflected = ArrayHelper::merge(static::$plural['uninflected'], static::$uninflected); + $irregular = array_keys(static::$plural['irregular']); + + $unInflectedRegex = '(?:' . implode('|', $unInflected) . ')'; + $irregularRegex = '(?:' . implode('|', $irregular) . ')'; + + if (preg_match('/(.*)\\b(' . $irregularRegex . ')$/i', $word, $regs)) + return $regs[1] . substr($word, 0, 1) . substr(static::$plural['irregular'][strtolower($regs[2])], 1); + + if (preg_match('/^(' . $unInflectedRegex . ')$/i', $word, $regs)) + return $word; + + foreach (static::$plural['rules'] as $rule => $replacement) { + if (preg_match($rule, $word)) { + return preg_replace($rule, $replacement, $word); + } + } + return $word; + } + + /** + * Returns the singular of the $word + * @param string $word the english word to singularize + * @return string Singular noun. + */ + public static function singularize($word) + { + + $unInflected = ArrayHelper::merge(static::$singular['uninflected'], static::$uninflected); + + $irregular = array_merge( + static::$singular['irregular'], + array_flip(static::$plural['irregular']) + ); + + $unInflectedRegex = '(?:' . implode('|', $unInflected) . ')'; + $irregularRegex = '(?:' . implode('|', array_keys($irregular)) . ')'; + + + if (preg_match('/(.*)\\b(' . $irregularRegex . ')$/i', $word, $regs)) + return $regs[1] . substr($word, 0, 1) . substr($irregular[strtolower($regs[2])], 1); + + + if (preg_match('/^(' . $unInflectedRegex . ')$/i', $word, $regs)) + return $word; + + + foreach (static::$singular['rules'] as $rule => $replacement) { + if (preg_match($rule, $word)) { + return preg_replace($rule, $replacement, $word); + } + } + return $word; + } + + /** + * Converts an underscored or CamelCase word into a English + * sentence. + * @param string $words + * @param bool $ucAll whether to set all words to uppercase + * @return string + */ + public static function titleize($words, $ucAll = false) + { + + $words = static::humanize(static::underscore($words), $ucAll); + return $ucAll ? ucwords($words) : ucfirst($words); + } + + /** + * Returns given word as CamelCased + * Converts a word like "send_email" to "SendEmail". It + * will remove non alphanumeric character from the word, so + * "who's online" will be converted to "WhoSOnline" + * @see variablize + * @param string $word the word to CamelCase + * @return string + */ + public static function camelize($word) + { + return str_replace(' ', '', ucwords(preg_replace('/[^A-Z^a-z^0-9]+/', ' ', $word))); + } + + /** + * Converts a CamelCase name into space-separated words. + * For example, 'PostTag' will be converted to 'Post Tag'. + * @param string $name the string to be converted + * @param boolean $ucwords whether to capitalize the first letter in each word + * @return string the resulting words + */ + public static function camel2words($name, $ucwords = true) + { + $label = trim(strtolower(str_replace(array( + '-', + '_', + '.' + ), ' ', preg_replace('/(? ' ', + '/\\s+/' => $replacement, + '/(?<=[a-z])([A-Z])/' => $replacement . '\\1', + str_replace(':rep', preg_quote($replacement, '/'), '/^[:rep]+|[:rep]+$/') => '' + ); + return preg_replace(array_keys($map), array_values($map), $string); + } + + /** + * Converts a table name to its class name. For example, converts "people" to "Person" + * @param string $table_name + * @return string + */ + public static function classify($table_name) + { + return static::camelize(static::singularize($table_name)); + } + + /** + * Converts number to its ordinal English form. For example, converts 13 to 13th, 2 to 2nd ... + * @param int $number the number to get its ordinal value + * @return string + */ + public static function ordinalize($number) + { + if (in_array(($number % 100), range(11, 13))) { + return $number . 'th'; + } else { + switch (($number % 10)) { + case 1: + return $number . 'st'; + break; + case 2: + return $number . 'nd'; + break; + case 3: + return $number . 'rd'; + default: + return $number . 'th'; + break; + } + } + } +} diff --git a/tests/unit/framework/helpers/InflectorTest.php b/tests/unit/framework/helpers/InflectorTest.php index 859d8bf..49c958e 100644 --- a/tests/unit/framework/helpers/InflectorTest.php +++ b/tests/unit/framework/helpers/InflectorTest.php @@ -12,10 +12,29 @@ class InflectorTest extends TestCase public function testPluralize() { - $this->assertEquals("people", Inflector::pluralize('person')); - $this->assertEquals("fish", Inflector::pluralize('fish')); - $this->assertEquals("men", Inflector::pluralize('man')); - $this->assertEquals("tables", Inflector::pluralize('table')); + $testData = array( + 'move' => 'moves', + 'foot' => 'feet', + 'child' => 'children', + 'human' => 'humans', + 'man' => 'men', + 'staff' => 'staff', + 'tooth' => 'teeth', + 'person' => 'people', + 'mouse' => 'mice', + 'touch' => 'touches', + 'hash' => 'hashes', + 'shelf' => 'shelves', + 'potato' => 'potatoes', + 'bus' => 'buses', + 'test' => 'tests', + 'car' => 'cars', + ); + + foreach ($testData as $testIn => $testOut) { + $this->assertEquals($testOut, Inflector::pluralize($testIn)); + $this->assertEquals(ucfirst($testOut), ucfirst(Inflector::pluralize($testIn))); + } } public function testSingularize() @@ -42,10 +61,35 @@ class InflectorTest extends TestCase $this->assertEquals("me_my_self_and_i", Inflector::underscore('MeMySelfAndI')); } + public function testCamel2words() + { + $this->assertEquals('Camel Case', Inflector::camel2words('camelCase')); + $this->assertEquals('Lower Case', Inflector::camel2words('lower_case')); + $this->assertEquals('Tricky Stuff It Is Testing', Inflector::camel2words(' tricky_stuff.it-is testing... ')); + } + + public function testCamel2id() + { + $this->assertEquals('post-tag', Inflector::camel2id('PostTag')); + $this->assertEquals('post_tag', Inflector::camel2id('PostTag', '_')); + + $this->assertEquals('post-tag', Inflector::camel2id('postTag')); + $this->assertEquals('post_tag', Inflector::camel2id('postTag', '_')); + } + + public function testId2camel() + { + $this->assertEquals('PostTag', Inflector::id2camel('post-tag')); + $this->assertEquals('PostTag', Inflector::id2camel('post_tag', '_')); + + $this->assertEquals('PostTag', Inflector::id2camel('post-tag')); + $this->assertEquals('PostTag', Inflector::id2camel('post_tag', '_')); + } + public function testHumanize() { $this->assertEquals("Me my self and i", Inflector::humanize('me_my_self_and_i')); - $this->assertEquals("Me My Self And i", Inflector::humanize('me_my_self_and_i'), true); + $this->assertEquals("Me My Self And I", Inflector::humanize('me_my_self_and_i'), true); } public function testVariablize() diff --git a/yii/helpers/Inflector.php b/yii/helpers/Inflector.php deleted file mode 100644 index 72f77a7..0000000 --- a/yii/helpers/Inflector.php +++ /dev/null @@ -1,18 +0,0 @@ - - * @since 2.0 - */ -class Inflector extends base\Inflector -{ -} diff --git a/yii/helpers/base/Inflector.php b/yii/helpers/base/Inflector.php deleted file mode 100644 index af3f266..0000000 --- a/yii/helpers/base/Inflector.php +++ /dev/null @@ -1,499 +0,0 @@ - - * @since 2.0 - */ -class Inflector -{ - - /** - * @var array rules of plural words - */ - protected static $plural = array( - 'rules' => array( - '/(s)tatus$/i' => '\1\2tatuses', - '/(quiz)$/i' => '\1zes', - '/^(ox)$/i' => '\1\2en', - '/([m|l])ouse$/i' => '\1ice', - '/(matr|vert|ind)(ix|ex)$/i' => '\1ices', - '/(x|ch|ss|sh)$/i' => '\1es', - '/([^aeiouy]|qu)y$/i' => '\1ies', - '/(hive)$/i' => '\1s', - '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', - '/sis$/i' => 'ses', - '/([ti])um$/i' => '\1a', - '/(p)erson$/i' => '\1eople', - '/(m)an$/i' => '\1en', - '/(c)hild$/i' => '\1hildren', - '/(buffal|tomat)o$/i' => '\1\2oes', - '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i', - '/us$/i' => 'uses', - '/(alias)$/i' => '\1es', - '/(ax|cris|test)is$/i' => '\1es', - '/s$/' => 's', - '/^$/' => '', - '/$/' => 's', - ), - 'uninflected' => array( - '.*[nrlm]ese', - '.*deer', - '.*fish', - '.*measles', - '.*ois', - '.*pox', - '.*sheep', - 'people' - ), - 'irregular' => array( - 'atlas' => 'atlases', - 'beef' => 'beefs', - 'brother' => 'brothers', - 'cafe' => 'cafes', - 'child' => 'children', - 'cookie' => 'cookies', - 'corpus' => 'corpuses', - 'cow' => 'cows', - 'ganglion' => 'ganglions', - 'genie' => 'genies', - 'genus' => 'genera', - 'graffito' => 'graffiti', - 'hoof' => 'hoofs', - 'loaf' => 'loaves', - 'man' => 'men', - 'money' => 'monies', - 'mongoose' => 'mongooses', - 'move' => 'moves', - 'mythos' => 'mythoi', - 'niche' => 'niches', - 'numen' => 'numina', - 'occiput' => 'occiputs', - 'octopus' => 'octopuses', - 'opus' => 'opuses', - 'ox' => 'oxen', - 'penis' => 'penises', - 'person' => 'people', - 'sex' => 'sexes', - 'soliloquy' => 'soliloquies', - 'testis' => 'testes', - 'trilby' => 'trilbys', - 'turf' => 'turfs' - ) - ); - /** - * @var array the rules to singular inflector - */ - protected static $singular = array( - 'rules' => array( - '/(s)tatuses$/i' => '\1\2tatus', - '/^(.*)(menu)s$/i' => '\1\2', - '/(quiz)zes$/i' => '\\1', - '/(matr)ices$/i' => '\1ix', - '/(vert|ind)ices$/i' => '\1ex', - '/^(ox)en/i' => '\1', - '/(alias)(es)*$/i' => '\1', - '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us', - '/([ftw]ax)es/i' => '\1', - '/(cris|ax|test)es$/i' => '\1is', - '/(shoe|slave)s$/i' => '\1', - '/(o)es$/i' => '\1', - '/ouses$/' => 'ouse', - '/([^a])uses$/' => '\1us', - '/([m|l])ice$/i' => '\1ouse', - '/(x|ch|ss|sh)es$/i' => '\1', - '/(m)ovies$/i' => '\1\2ovie', - '/(s)eries$/i' => '\1\2eries', - '/([^aeiouy]|qu)ies$/i' => '\1y', - '/([lr])ves$/i' => '\1f', - '/(tive)s$/i' => '\1', - '/(hive)s$/i' => '\1', - '/(drive)s$/i' => '\1', - '/([^fo])ves$/i' => '\1fe', - '/(^analy)ses$/i' => '\1sis', - '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis', - '/([ti])a$/i' => '\1um', - '/(p)eople$/i' => '\1\2erson', - '/(m)en$/i' => '\1an', - '/(c)hildren$/i' => '\1\2hild', - '/(n)ews$/i' => '\1\2ews', - '/eaus$/' => 'eau', - '/^(.*us)$/' => '\\1', - '/s$/i' => '' - ), - 'uninflected' => array( - '.*[nrlm]ese', - '.*deer', - '.*fish', - '.*measles', - '.*ois', - '.*pox', - '.*sheep', - '.*ss' - ), - 'irregular' => array( - 'foes' => 'foe', - 'waves' => 'wave', - 'curves' => 'curve' - ) - ); - - /** - * @var array list of words that should not be inflected - */ - protected static $uninflected = array( - 'Amoyese', - 'bison', - 'Borghese', - 'bream', - 'breeches', - 'britches', - 'buffalo', - 'cantus', - 'carp', - 'chassis', - 'clippers', - 'cod', - 'coitus', - 'Congoese', - 'contretemps', - 'corps', - 'debris', - 'diabetes', - 'djinn', - 'eland', - 'elk', - 'equipment', - 'Faroese', - 'flounder', - 'Foochowese', - 'gallows', - 'Genevese', - 'Genoese', - 'Gilbertese', - 'graffiti', - 'headquarters', - 'herpes', - 'hijinks', - 'Hottentotese', - 'information', - 'innings', - 'jackanapes', - 'Kiplingese', - 'Kongoese', - 'Lucchese', - 'mackerel', - 'Maltese', - '.*?media', - 'mews', - 'moose', - 'mumps', - 'Nankingese', - 'news', - 'nexus', - 'Niasese', - 'Pekingese', - 'Piedmontese', - 'pincers', - 'Pistoiese', - 'pliers', - 'Portuguese', - 'proceedings', - 'rabies', - 'rice', - 'rhinoceros', - 'salmon', - 'Sarawakese', - 'scissors', - 'sea[- ]bass', - 'series', - 'Shavese', - 'shears', - 'siemens', - 'species', - 'swine', - 'testes', - 'trousers', - 'trout', - 'tuna', - 'Vermontese', - 'Wenchowese', - 'whiting', - 'wildebeest', - 'Yengeese' - ); - - /** - * @var array map of special chars and its translation - */ - protected static $transliteration = array( - '/ä|æ|ǽ/' => 'ae', - '/ö|œ/' => 'oe', - '/ü/' => 'ue', - '/Ä/' => 'Ae', - '/Ü/' => 'Ue', - '/Ö/' => 'Oe', - '/À|Á|Â|Ã|Å|Ǻ|Ā|Ă|Ą|Ǎ/' => 'A', - '/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª/' => 'a', - '/Ç|Ć|Ĉ|Ċ|Č/' => 'C', - '/ç|ć|ĉ|ċ|č/' => 'c', - '/Ð|Ď|Đ/' => 'D', - '/ð|ď|đ/' => 'd', - '/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě/' => 'E', - '/è|é|ê|ë|ē|ĕ|ė|ę|ě/' => 'e', - '/Ĝ|Ğ|Ġ|Ģ/' => 'G', - '/ĝ|ğ|ġ|ģ/' => 'g', - '/Ĥ|Ħ/' => 'H', - '/ĥ|ħ/' => 'h', - '/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ/' => 'I', - '/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı/' => 'i', - '/Ĵ/' => 'J', - '/ĵ/' => 'j', - '/Ķ/' => 'K', - '/ķ/' => 'k', - '/Ĺ|Ļ|Ľ|Ŀ|Ł/' => 'L', - '/ĺ|ļ|ľ|ŀ|ł/' => 'l', - '/Ñ|Ń|Ņ|Ň/' => 'N', - '/ñ|ń|ņ|ň|ʼn/' => 'n', - '/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ/' => 'O', - '/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º/' => 'o', - '/Ŕ|Ŗ|Ř/' => 'R', - '/ŕ|ŗ|ř/' => 'r', - '/Ś|Ŝ|Ş|Ș|Š/' => 'S', - '/ś|ŝ|ş|ș|š|ſ/' => 's', - '/Ţ|Ț|Ť|Ŧ/' => 'T', - '/ţ|ț|ť|ŧ/' => 't', - '/Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ/' => 'U', - '/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'u', - '/Ý|Ÿ|Ŷ/' => 'Y', - '/ý|ÿ|ŷ/' => 'y', - '/Ŵ/' => 'W', - '/ŵ/' => 'w', - '/Ź|Ż|Ž/' => 'Z', - '/ź|ż|ž/' => 'z', - '/Æ|Ǽ/' => 'AE', - '/ß/' => 'ss', - '/IJ/' => 'IJ', - '/ij/' => 'ij', - '/Œ/' => 'OE', - '/ƒ/' => 'f' - ); - - /** - * Returns the plural of a $word - * - * @param string $word the word to pluralize - * @return string - */ - public static function pluralize($word) - { - $unInflected = ArrayHelper::merge(static::$plural['uninflected'], static::$uninflected); - $irregular = array_keys(static::$plural['irregular']); - - $unInflectedRegex = '(?:' . implode('|', $unInflected) . ')'; - $irregularRegex = '(?:' . implode('|', $irregular) . ')'; - - if (preg_match('/(.*)\\b(' . $irregularRegex . ')$/i', $word, $regs)) - return $regs[1] . substr($word, 0, 1) . substr(static::$plural['irregular'][strtolower($regs[2])], 1); - - if (preg_match('/^(' . $unInflectedRegex . ')$/i', $word, $regs)) - return $word; - - foreach (static::$plural['rules'] as $rule => $replacement) { - if (preg_match($rule, $word)) { - return preg_replace($rule, $replacement, $word); - } - } - return $word; - } - - /** - * Returns the singular of the $word - * - * @param string $word the english word to singularize - * @return string Singular noun. - */ - public static function singularize($word) - { - - $unInflected = ArrayHelper::merge(static::$singular['uninflected'], static::$uninflected); - - $irregular = array_merge( - static::$singular['irregular'], - array_flip(static::$plural['irregular']) - ); - - $unInflectedRegex = '(?:' . implode('|', $unInflected) . ')'; - $irregularRegex = '(?:' . implode('|', array_keys($irregular)) . ')'; - - - if (preg_match('/(.*)\\b(' . $irregularRegex . ')$/i', $word, $regs)) - return $regs[1] . substr($word, 0, 1) . substr($irregular[strtolower($regs[2])], 1); - - - if (preg_match('/^(' . $unInflectedRegex . ')$/i', $word, $regs)) - return $word; - - - foreach (static::$singular['rules'] as $rule => $replacement) { - if (preg_match($rule, $word)) { - return preg_replace($rule, $replacement, $word); - } - } - return $word; - } - - /** - * Converts an underscored or CamelCase word into a English - * sentence. - * - * @param string $words - * @param bool $ucAll whether to set all words to uppercase - * @return string - */ - public static function titleize($words, $ucAll = false) - { - - $words = static::humanize(static::underscore($words), $ucAll); - return $ucAll ? ucwords($words) : ucfirst($words); - } - - /** - * Returns given word as CamelCased - * - * Converts a word like "send_email" to "SendEmail". It - * will remove non alphanumeric character from the word, so - * "who's online" will be converted to "WhoSOnline" - * - * @see variablize - * @param string $word the word to CamelCase - * @return string - */ - public static function camelize($word) - { - return str_replace(' ', '', ucwords(preg_replace('/[^A-Z^a-z^0-9]+/', ' ', $word))); - } - - /** - * Converts any "CamelCased" into an "underscored_word". - * - * @param string $words the word(s) to underscore - * @return string - */ - public static function underscore($words) - { - return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $words)); - } - - /** - * Returns a human-readable string from $word - * - * @param string $word the string to humanize - * @param bool $ucAll whether to set all words to uppercase or not - * @return string - */ - public static function humanize($word, $ucAll = false) - { - $word = str_replace('_', ' ', preg_replace('/_id$/', '', $word)); - return $ucAll ? ucwords($word) : ucfirst($word); - } - - /** - * Same as camelize but first char is in lowercase - * - * Converts a word like "send_email" to "sendEmail". It - * will remove non alphanumeric character from the word, so - * "who's online" will be converted to "whoSOnline" - * - * @param string $word to lowerCamelCase - * @return string - */ - public static function variablize($word) - { - $word = static::camelize($word); - return strtolower($word[0]) . substr($word, 1); - } - - /** - * Converts a class name to its table name (pluralized) - * naming conventions. For example, converts "Person" to "people" - * - * @param string $class_name the class name for getting related table_name - * @return string - */ - public static function tableize($class_name) - { - return static::pluralize(static::underscore($class_name)); - } - - /** - * Returns a string with all spaces converted to given replacement and - * non word characters removed. Maps special characters to ASCII using - * `Inflector::$transliteration`. - * - * @param string $string An arbitrary string to convert. - * @param string $replacement The replacement to use for spaces. - * @return string The converted string. - */ - public static function slug($string, $replacement = '-') - { - - $map = static::$transliteration + array( - '/[^\w\s]/' => ' ', - '/\\s+/' => $replacement, - '/(?<=[a-z])([A-Z])/' => $replacement . '\\1', - str_replace(':rep', preg_quote($replacement, '/'), '/^[:rep]+|[:rep]+$/') => '' - ); - return preg_replace(array_keys($map), array_values($map), $string); - } - - /** - * Converts a table name to its class name. For example, converts "people" to "Person" - * - * @param string $table_name - * @return string - */ - public static function classify($table_name) - { - return static::camelize(static::singularize($table_name)); - } - - /** - * Converts number to its ordinal English form. - * - * This method converts 13 to 13th, 2 to 2nd ... - * - * @param int $number the number to get its ordinal value - * @return string - */ - public static function ordinalize($number) - { - if (in_array(($number % 100), range(11, 13))) { - return $number . 'th'; - } else { - switch (($number % 10)) { - case 1: - return $number . 'st'; - break; - case 2: - return $number . 'nd'; - break; - case 3: - return $number . 'rd'; - default: - return $number . 'th'; - break; - } - } - } -} From 05523640f2a65ae09b75480a445b085b5dca5077 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Wed, 22 May 2013 10:59:09 +0200 Subject: [PATCH 08/20] fixed pluralize + singularize rules for tests --- framework/yii/helpers/base/Inflector.php | 9 ++++++++- tests/unit/framework/helpers/InflectorTest.php | 26 ++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/framework/yii/helpers/base/Inflector.php b/framework/yii/helpers/base/Inflector.php index f233547..3c1927c 100644 --- a/framework/yii/helpers/base/Inflector.php +++ b/framework/yii/helpers/base/Inflector.php @@ -23,7 +23,12 @@ class Inflector */ protected static $plural = array( 'rules' => array( + '/(m)ove$/i' => '\1oves', + '/(f)oot$/i' => '\1eet', + '/(h)uman$/i' => '\1umans', '/(s)tatus$/i' => '\1\2tatuses', + '/(s)taff$/i' => '\1taff', + '/(t)ooth$/i' => '\1eeth', '/(quiz)$/i' => '\1zes', '/^(ox)$/i' => '\1\2en', '/([m|l])ouse$/i' => '\1ice', @@ -37,7 +42,7 @@ class Inflector '/(p)erson$/i' => '\1eople', '/(m)an$/i' => '\1en', '/(c)hild$/i' => '\1hildren', - '/(buffal|tomat)o$/i' => '\1\2oes', + '/(buffal|tomat|potat|ech|her|vet)o$/i' => '\1oes', '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i', '/us$/i' => 'uses', '/(alias)$/i' => '\1es', @@ -97,6 +102,8 @@ class Inflector protected static $singular = array( 'rules' => array( '/(s)tatuses$/i' => '\1\2tatus', + '/(f)eet$/i' => '\1oot', + '/(t)eeth$/i' => '\1ooth', '/^(.*)(menu)s$/i' => '\1\2', '/(quiz)zes$/i' => '\\1', '/(matr)ices$/i' => '\1ix', diff --git a/tests/unit/framework/helpers/InflectorTest.php b/tests/unit/framework/helpers/InflectorTest.php index 49c958e..ee6050c 100644 --- a/tests/unit/framework/helpers/InflectorTest.php +++ b/tests/unit/framework/helpers/InflectorTest.php @@ -39,10 +39,28 @@ class InflectorTest extends TestCase public function testSingularize() { - $this->assertEquals("person", Inflector::singularize('people')); - $this->assertEquals("fish", Inflector::singularize('fish')); - $this->assertEquals("man", Inflector::singularize('men')); - $this->assertEquals("table", Inflector::singularize('tables')); + $testData = array( + 'moves' => 'move', + 'feet' => 'foot', + 'children' => 'child', + 'humans' => 'human', + 'men' => 'man', + 'staff' => 'staff', + 'teeth' => 'tooth', + 'people' => 'person', + 'mice' => 'mouse', + 'touches' => 'touch', + 'hashes' => 'hash', + 'shelves' => 'shelf', + 'potatoes' => 'potato', + 'buses' => 'bus', + 'tests' => 'test', + 'cars' => 'car', + ); + foreach ($testData as $testIn => $testOut) { + $this->assertEquals($testOut, Inflector::pluralize($testIn)); + $this->assertEquals(ucfirst($testOut), ucfirst(Inflector::pluralize($testIn))); + } } public function testTitleize() From dc8dbdc64a969da181f7e13a7a2dee0898753f7b Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Wed, 22 May 2013 11:04:08 +0200 Subject: [PATCH 09/20] fixed tests --- tests/unit/framework/helpers/InflectorTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/unit/framework/helpers/InflectorTest.php b/tests/unit/framework/helpers/InflectorTest.php index ee6050c..9f9626d 100644 --- a/tests/unit/framework/helpers/InflectorTest.php +++ b/tests/unit/framework/helpers/InflectorTest.php @@ -58,8 +58,8 @@ class InflectorTest extends TestCase 'cars' => 'car', ); foreach ($testData as $testIn => $testOut) { - $this->assertEquals($testOut, Inflector::pluralize($testIn)); - $this->assertEquals(ucfirst($testOut), ucfirst(Inflector::pluralize($testIn))); + $this->assertEquals($testOut, Inflector::singularize($testIn)); + $this->assertEquals(ucfirst($testOut), ucfirst(Inflector::singularize($testIn))); } } @@ -107,7 +107,7 @@ class InflectorTest extends TestCase public function testHumanize() { $this->assertEquals("Me my self and i", Inflector::humanize('me_my_self_and_i')); - $this->assertEquals("Me My Self And I", Inflector::humanize('me_my_self_and_i'), true); + $this->assertEquals("Me My Self And I", Inflector::humanize('me_my_self_and_i', true)); } public function testVariablize() @@ -122,7 +122,7 @@ class InflectorTest extends TestCase public function testSlug() { - $this->assertEquals("this-is-a-title", Inflector::humanize('this is a title')); + $this->assertEquals("this-is-a-title", Inflector::slug('this is a title')); } public function testClassify() @@ -132,6 +132,6 @@ class InflectorTest extends TestCase public function testOrdinalize() { - $this->assertEquals("21st", Inflector::humanize('21')); + $this->assertEquals('21st', Inflector::ordinalize('21')); } } From b5ad49ed099c6c063900ce1fa5271ec4f71791a2 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Wed, 22 May 2013 12:44:02 +0200 Subject: [PATCH 10/20] added carousel widget --- framework/yii/bootstrap/Carousel.php | 201 +++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 framework/yii/bootstrap/Carousel.php diff --git a/framework/yii/bootstrap/Carousel.php b/framework/yii/bootstrap/Carousel.php new file mode 100644 index 0000000..bcb8649 --- /dev/null +++ b/framework/yii/bootstrap/Carousel.php @@ -0,0 +1,201 @@ + array( + * array( + * 'src' => 'http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-01.jpg', + * ), + * array( + * 'src' => 'http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-02.jpg', + * 'captionLabel' => 'This is the caption label', + * 'captionText' => 'This is the caption text' + * ), + * ) + * )); + * ``` + * + * @see http://twitter.github.io/bootstrap/javascript.html#carousel + * @author Antonio Ramirez + * @since 2.0 + */ +class Carousel extends Widget +{ + /** + * @var string the previous button label. Defaults to '‹'. + */ + public $previousLabel = '‹'; + /** + * @var string the next button label. Defaults to '›'. + */ + public $nextLabel = '›'; + /** + * @var boolean indicates whether the carousel should slide items. + */ + public $slide = true; + /** + * @var boolean indicates whether to display the previous and next links. + */ + public $displayPreviousAndNext = true; + /** + * @var array list of images to appear in the carousel. If this property is empty, + * the widget will not render anything. Each array element represents a single image in the carousel + * with the following structure: + * + * ```php + * array( + * 'src' => 'src of the image', // required + * 'options' => ['html attributes of the item'], // optional + * 'imageOptions'=> ['html attributes of the image'] // optional + * 'captionLabel' => 'Title of the caption', // optional + * 'captionOptions' => ['html attributes of the caption'], // optional + * 'captionText' => 'Caption text, long description', // optional + * 'visible' => 'boolean', // optional, whether to display the item or not + * ) + * ``` + */ + public $items = array(); + + + /** + * Initializes the widget. + */ + public function init() + { + parent::init(); + $this->getView()->registerAssetBundle('yii/bootstrap/carousel'); + $this->initOptions(); + } + + /** + * Renders the widget. + */ + public function run() + { + if (empty($this->items)) { + return; + } + + echo Html::beginTag('div', $this->options) . "\n"; + $this->renderIndicators() . "\n"; + $this->renderItems() . "\n"; + $this->renderPreviousAndNext() . "\n"; + echo Html::endTag('div') . "\n"; + } + + /** + * Renders carousel indicators + */ + public function renderIndicators() + { + echo Html::beginTag('ol', array('class' => 'carousel-indicators')) . "\n"; + for ($i = 0, $ln = count($this->items); $i < $ln; $i++) { + $options = array('data-target' => '#' . $this->options['id'], 'data-slide-to' => $i); + if ($i === 0) { + $this->addCssClass($options, 'active'); + } + echo Html::tag('li', '', $options) . "\n"; + } + echo Html::endTag('ol') . "\n"; + } + + /** + * Renders carousel items as specified on [[items]] + */ + public function renderItems() + { + echo Html::beginTag('div', array('class' => 'carousel-inner')) . "\n"; + for ($i = 0, $ln = count($this->items); $i < $ln; $i++) { + echo $this->renderItem($this->items[$i], $i); + } + echo Html::endTag('div') . "\n"; + } + + /** + * Renders a single carousel item + * @param array $item a single item from [[items]] + * @param integer $index the item index as the first item should be set to `active` + */ + public function renderItem($item, $index) + { + $itemOptions = ArrayHelper::getValue($item, 'options', array()); + $this->addCssClass($itemOptions, 'item'); + if ($index === 0) { + $this->addCssClass($itemOptions, 'active'); + } + echo Html::beginTag('div', $itemOptions) . "\n"; + echo Html::img($item['src'], ArrayHelper::getValue($item, 'imageOptions', array())) . "\n"; + + if (ArrayHelper::getValue($item, 'captionLabel')) { + $this->renderCaptioN($item); + } + + echo Html::endTag('div') . "\n"; + + } + + /** + * Renders the caption of an item + * @param array $item a single item from [[items]] + */ + public function renderCaption($item) + { + $captionOptions = ArrayHelper::getValue($item, 'captionOptions', array()); + $this->addCssClass($captionOptions, 'carousel-caption'); + + echo Html::beginTag('div', $captionOptions) . "\n"; + echo Html::tag('h4', ArrayHelper::getValue($item, 'captionLabel')) . "\n"; + echo Html::tag('p', ArrayHelper::getValue($item, 'captionText', '')) . "\n"; + echo Html::endTag('div'); + } + + /** + * Renders previous and next button if [[displayPreviousAndNext]] is set to `true` + */ + public function renderPreviousAndNext() + { + if (!$this->displayPreviousAndNext) { + return; + } + echo Html::a($this->previousLabel, '#' . $this->options['id'], array( + 'class' => 'left carousel-control', + 'data-slide' => 'prev' + )) . + "\n" . + Html::a($this->nextLabel, '#' . $this->options['id'], array( + 'class' => 'right carousel-control', + 'data-slide' => 'next' + )) . + "\n"; + } + + /** + * Initializes the widget options. + * This method sets the default values for various options. + */ + public function initOptions() + { + $this->addCssClass($this->options, 'carousel'); + if ($this->slide) { + $this->addCssClass($this->options, 'slide'); + } + } +} \ No newline at end of file From d227044c6fcb3e685329e667c14272cb15c54305 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Wed, 22 May 2013 23:39:30 +0400 Subject: [PATCH 11/20] Bootstrap widget comment typo fix --- framework/yii/bootstrap/TypeAhead.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/yii/bootstrap/TypeAhead.php b/framework/yii/bootstrap/TypeAhead.php index cd60ff3..0704404 100644 --- a/framework/yii/bootstrap/TypeAhead.php +++ b/framework/yii/bootstrap/TypeAhead.php @@ -32,7 +32,7 @@ use yii\helpers\Html; * ```php * echo TypeAhead::widget(array( * 'name' => 'country', - * 'cloentOptions' => array( + * 'clientOptions' => array( * 'source' => array('USA', 'ESP'), * ), * )); From e1a3ae65d6983992a01df14967885f870982b8d4 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Wed, 22 May 2013 22:17:20 +0200 Subject: [PATCH 12/20] refactored and apply suggested changes --- framework/yii/bootstrap/Carousel.php | 92 ++++++++++++------------------------ 1 file changed, 29 insertions(+), 63 deletions(-) diff --git a/framework/yii/bootstrap/Carousel.php b/framework/yii/bootstrap/Carousel.php index bcb8649..0b9fb2d 100644 --- a/framework/yii/bootstrap/Carousel.php +++ b/framework/yii/bootstrap/Carousel.php @@ -21,13 +21,14 @@ use yii\helpers\Html; * ```php * echo Carousel::widget(array( * 'items' => array( + * 'http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-01.jpg', * array( - * 'src' => 'http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-01.jpg', + * 'content' => 'http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-02.jpg', * ), * array( - * 'src' => 'http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-02.jpg', - * 'captionLabel' => 'This is the caption label', - * 'captionText' => 'This is the caption text' + * 'content' => 'http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-03.jpg', + * 'options' => array(...) + * 'caption' => '

This is title

This is the caption text

' * ), * ) * )); @@ -40,21 +41,10 @@ use yii\helpers\Html; class Carousel extends Widget { /** - * @var string the previous button label. Defaults to '‹'. + * @var array indicates what labels should be displayed on next and previous carousel controls. If [[controls]] is + * set to `false` or they do not hold `left` and `right` keys, the controls will not be displayed. */ - public $previousLabel = '‹'; - /** - * @var string the next button label. Defaults to '›'. - */ - public $nextLabel = '›'; - /** - * @var boolean indicates whether the carousel should slide items. - */ - public $slide = true; - /** - * @var boolean indicates whether to display the previous and next links. - */ - public $displayPreviousAndNext = true; + public $controls = array('left' => '‹', 'right' => '›'); /** * @var array list of images to appear in the carousel. If this property is empty, * the widget will not render anything. Each array element represents a single image in the carousel @@ -62,13 +52,9 @@ class Carousel extends Widget * * ```php * array( - * 'src' => 'src of the image', // required + * 'content' => 'src of the image', // required * 'options' => ['html attributes of the item'], // optional - * 'imageOptions'=> ['html attributes of the image'] // optional - * 'captionLabel' => 'Title of the caption', // optional - * 'captionOptions' => ['html attributes of the caption'], // optional - * 'captionText' => 'Caption text, long description', // optional - * 'visible' => 'boolean', // optional, whether to display the item or not + * 'caption'=> ['html attributes of the image'] // optional * ) * ``` */ @@ -81,8 +67,7 @@ class Carousel extends Widget public function init() { parent::init(); - $this->getView()->registerAssetBundle('yii/bootstrap/carousel'); - $this->initOptions(); + $this->addCssClass($this->options, 'carousel'); } /** @@ -95,10 +80,12 @@ class Carousel extends Widget } echo Html::beginTag('div', $this->options) . "\n"; - $this->renderIndicators() . "\n"; - $this->renderItems() . "\n"; - $this->renderPreviousAndNext() . "\n"; + echo $this->renderIndicators() . "\n"; + echo $this->renderItems() . "\n"; + echo $this->renderPreviousAndNext() . "\n"; echo Html::endTag('div') . "\n"; + + $this->registerPlugin('carousel'); } /** @@ -106,6 +93,7 @@ class Carousel extends Widget */ public function renderIndicators() { + ob_start(); echo Html::beginTag('ol', array('class' => 'carousel-indicators')) . "\n"; for ($i = 0, $ln = count($this->items); $i < $ln; $i++) { $options = array('data-target' => '#' . $this->options['id'], 'data-slide-to' => $i); @@ -115,6 +103,7 @@ class Carousel extends Widget echo Html::tag('li', '', $options) . "\n"; } echo Html::endTag('ol') . "\n"; + return ob_get_clean(); } /** @@ -122,16 +111,18 @@ class Carousel extends Widget */ public function renderItems() { + ob_start(); echo Html::beginTag('div', array('class' => 'carousel-inner')) . "\n"; for ($i = 0, $ln = count($this->items); $i < $ln; $i++) { echo $this->renderItem($this->items[$i], $i); } echo Html::endTag('div') . "\n"; + return ob_get_clean(); } /** * Renders a single carousel item - * @param array $item a single item from [[items]] + * @param mixed $item a single item from [[items]] * @param integer $index the item index as the first item should be set to `active` */ public function renderItem($item, $index) @@ -142,10 +133,12 @@ class Carousel extends Widget $this->addCssClass($itemOptions, 'active'); } echo Html::beginTag('div', $itemOptions) . "\n"; - echo Html::img($item['src'], ArrayHelper::getValue($item, 'imageOptions', array())) . "\n"; + echo is_string($item) + ? $item + : $item['content']; // if not string, must be array, force required key - if (ArrayHelper::getValue($item, 'captionLabel')) { - $this->renderCaptioN($item); + if (ArrayHelper::getValue($item, 'caption')) { + echo ArrayHelper::getValue($item, 'caption'); } echo Html::endTag('div') . "\n"; @@ -153,49 +146,22 @@ class Carousel extends Widget } /** - * Renders the caption of an item - * @param array $item a single item from [[items]] - */ - public function renderCaption($item) - { - $captionOptions = ArrayHelper::getValue($item, 'captionOptions', array()); - $this->addCssClass($captionOptions, 'carousel-caption'); - - echo Html::beginTag('div', $captionOptions) . "\n"; - echo Html::tag('h4', ArrayHelper::getValue($item, 'captionLabel')) . "\n"; - echo Html::tag('p', ArrayHelper::getValue($item, 'captionText', '')) . "\n"; - echo Html::endTag('div'); - } - - /** * Renders previous and next button if [[displayPreviousAndNext]] is set to `true` */ public function renderPreviousAndNext() { - if (!$this->displayPreviousAndNext) { + if ($this->controls === false) { return; } - echo Html::a($this->previousLabel, '#' . $this->options['id'], array( + echo Html::a($this->controls['left'], '#' . $this->options['id'], array( 'class' => 'left carousel-control', 'data-slide' => 'prev' )) . "\n" . - Html::a($this->nextLabel, '#' . $this->options['id'], array( + Html::a($this->controls['right'], '#' . $this->options['id'], array( 'class' => 'right carousel-control', 'data-slide' => 'next' )) . "\n"; } - - /** - * Initializes the widget options. - * This method sets the default values for various options. - */ - public function initOptions() - { - $this->addCssClass($this->options, 'carousel'); - if ($this->slide) { - $this->addCssClass($this->options, 'slide'); - } - } } \ No newline at end of file From 8bc48333c40d0970a404ea48865d4c72e6c72c6f Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Thu, 23 May 2013 00:45:57 +0400 Subject: [PATCH 13/20] jQuery UI widgets --- framework/yii/jui/AutoComplete.php | 92 ++++++++++++++++++++++++++++++++++++++ framework/yii/jui/Widget.php | 84 ++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 framework/yii/jui/AutoComplete.php create mode 100644 framework/yii/jui/Widget.php diff --git a/framework/yii/jui/AutoComplete.php b/framework/yii/jui/AutoComplete.php new file mode 100644 index 0000000..f5bbae9 --- /dev/null +++ b/framework/yii/jui/AutoComplete.php @@ -0,0 +1,92 @@ + $model, + * 'attribute' => 'country', + * 'clientOptions' => array( + * 'source' => array('USA', 'RUS'), + * ), + * )); + * ``` + * + * The following example will use the name property instead: + * + * ```php + * echo AutoComplete::widget(array( + * 'name' => 'country', + * 'clientOptions' => array( + * 'source' => array('USA', 'RUS'), + * ), + * )); + *``` + * + * @see http://api.jqueryui.com/autocomplete/ + * @author Alexander Kochetov + * @since 2.0 + */ +class AutoComplete extends Widget +{ + /** + * @var \yii\base\Model the data model that this widget is associated with. + */ + public $model; + /** + * @var string the model attribute that this widget is associated with. + */ + public $attribute; + /** + * @var string the input name. This must be set if [[model]] and [[attribute]] are not set. + */ + public $name; + /** + * @var string the input value. + */ + public $value; + + + /** + * Renders the widget. + */ + public function run() + { + echo $this->renderField(); + $this->registerWidget('autocomplete'); + } + + /** + * Renders the AutoComplete field. If [[model]] has been specified then it will render an active field. + * If [[model]] is null or not from an [[Model]] instance, then the field will be rendered according to + * the [[name]] attribute. + * @return string the rendering result. + * @throws InvalidConfigException when none of the required attributes are set to render the textInput. + * That is, if [[model]] and [[attribute]] are not set, then [[name]] is required. + */ + public function renderField() + { + if ($this->model instanceof Model && $this->attribute !== null) { + return Html::activeTextInput($this->model, $this->attribute, $this->options); + } elseif ($this->name !== null) { + return Html::textInput($this->name, $this->value, $this->options); + } else { + throw new InvalidConfigException("Either 'name' or 'model' and 'attribute' properties must be specified."); + } + } +} diff --git a/framework/yii/jui/Widget.php b/framework/yii/jui/Widget.php new file mode 100644 index 0000000..f6efd91 --- /dev/null +++ b/framework/yii/jui/Widget.php @@ -0,0 +1,84 @@ + + * @since 2.0 + */ +class Widget extends \yii\base\Widget +{ + /** + * @var string the jQuery UI theme bundle. + */ + public static $theme = 'yii/jui/theme/base'; + /** + * @var array the HTML attributes for the widget container tag. + */ + public $options = array(); + /** + * @var array the options for the underlying jQuery UI widget. + * Please refer to the corresponding jQuery UI widget Web page for possible options. + * For example, [this page](http://api.jqueryui.com/accordion/) shows + * how to use the "Accordion" widget and the supported options (e.g. "header"). + */ + public $clientOptions = array(); + /** + * @var array the event handlers for the underlying jQuery UI widget. + * Please refer to the corresponding jQuery UI widget Web page for possible events. + * For example, [this page](http://api.jqueryui.com/accordion/) shows + * how to use the "Accordion" widget and the supported events (e.g. "create"). + */ + public $clientEvents = array(); + + + /** + * Initializes the widget. + * If you override this method, make sure you call the parent implementation first. + */ + public function init() + { + parent::init(); + if (!isset($this->options['id'])) { + $this->options['id'] = $this->getId(); + } + } + + /** + * Registers a specific jQuery UI widget and the related events + * @param string $name the name of the jQuery UI widget + */ + protected function registerWidget($name) + { + $id = $this->options['id']; + $view = $this->getView(); + $view->registerAssetBundle("yii/jui/$name"); + $view->registerAssetBundle(static::$theme . "/$name"); + + if ($this->clientOptions !== false) { + $options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions); + $js = "jQuery('#$id').$name($options);"; + $view->registerJs($js); + } + + if (!empty($this->clientEvents)) { + $js = array(); + foreach ($this->clientEvents as $event => $handler) { + $js[] = "jQuery('#$id').on('$name$event', $handler);"; + } + $view->registerJs(implode("\n", $js)); + } + } +} From d42cfd6ff15821846f357f9f284c9e63290b3bdc Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Wed, 22 May 2013 22:53:57 +0200 Subject: [PATCH 14/20] some other fixes --- framework/yii/bootstrap/Carousel.php | 38 ++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/framework/yii/bootstrap/Carousel.php b/framework/yii/bootstrap/Carousel.php index 0b9fb2d..21fc5ff 100644 --- a/framework/yii/bootstrap/Carousel.php +++ b/framework/yii/bootstrap/Carousel.php @@ -21,12 +21,12 @@ use yii\helpers\Html; * ```php * echo Carousel::widget(array( * 'items' => array( - * 'http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-01.jpg', + * '', * array( - * 'content' => 'http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-02.jpg', + * 'content' => '', * ), * array( - * 'content' => 'http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-03.jpg', + * 'content' => '', * 'options' => array(...) * 'caption' => '

This is title

This is the caption text

' * ), @@ -114,7 +114,7 @@ class Carousel extends Widget ob_start(); echo Html::beginTag('div', array('class' => 'carousel-inner')) . "\n"; for ($i = 0, $ln = count($this->items); $i < $ln; $i++) { - echo $this->renderItem($this->items[$i], $i); + $this->renderItem($this->items[$i], $i); } echo Html::endTag('div') . "\n"; return ob_get_clean(); @@ -127,22 +127,27 @@ class Carousel extends Widget */ public function renderItem($item, $index) { - $itemOptions = ArrayHelper::getValue($item, 'options', array()); + if (is_string($item)) { + $itemOptions = array(); + $itemContent = $item; + $itemCaption = ''; + } else { + $itemOptions = ArrayHelper::getValue($item, 'options', array()); + $itemContent = $item['content']; // if not string, must be array, force required key + $itemCaption = ArrayHelper::getValue($item, 'caption'); + if ($itemCaption) { + $itemCaption = Html::tag('div', $itemCaption, array('class' => 'carousel-caption')); + } + } + $this->addCssClass($itemOptions, 'item'); if ($index === 0) { $this->addCssClass($itemOptions, 'active'); } echo Html::beginTag('div', $itemOptions) . "\n"; - echo is_string($item) - ? $item - : $item['content']; // if not string, must be array, force required key - - if (ArrayHelper::getValue($item, 'caption')) { - echo ArrayHelper::getValue($item, 'caption'); - } - + echo $itemContent . "\n"; + echo $itemCaption . "\n"; echo Html::endTag('div') . "\n"; - } /** @@ -150,7 +155,7 @@ class Carousel extends Widget */ public function renderPreviousAndNext() { - if ($this->controls === false) { + if ($this->controls === false || !(isset($this->controls['left']) && isset($this->controls['left']))) { return; } echo Html::a($this->controls['left'], '#' . $this->options['id'], array( @@ -161,7 +166,6 @@ class Carousel extends Widget Html::a($this->controls['right'], '#' . $this->options['id'], array( 'class' => 'right carousel-control', 'data-slide' => 'next' - )) . - "\n"; + )); } } \ No newline at end of file From 639cd641d8179a5fea6cb68ce108df157b8ea4b2 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Thu, 23 May 2013 02:08:12 +0400 Subject: [PATCH 15/20] yii\bootstrap\Carousel fixes --- framework/yii/bootstrap/Carousel.php | 48 +++++++++++++++++------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/framework/yii/bootstrap/Carousel.php b/framework/yii/bootstrap/Carousel.php index 21fc5ff..6515eb6 100644 --- a/framework/yii/bootstrap/Carousel.php +++ b/framework/yii/bootstrap/Carousel.php @@ -8,7 +8,6 @@ namespace yii\bootstrap; use Yii; -use yii\base\InvalidConfigException; use yii\base\Model; use yii\helpers\base\ArrayHelper; use yii\helpers\Html; @@ -16,7 +15,7 @@ use yii\helpers\Html; /** * Carousel renders a carousel bootstrap javascript component. * - * For example, + * For example: * * ```php * echo Carousel::widget(array( @@ -27,8 +26,8 @@ use yii\helpers\Html; * ), * array( * 'content' => '', - * 'options' => array(...) - * 'caption' => '

This is title

This is the caption text

' + * 'caption' => '

This is title

This is the caption text

', + * 'options' => array(...), * ), * ) * )); @@ -42,9 +41,9 @@ class Carousel extends Widget { /** * @var array indicates what labels should be displayed on next and previous carousel controls. If [[controls]] is - * set to `false` or they do not hold `left` and `right` keys, the controls will not be displayed. + * set to `false` the controls will not be displayed. */ - public $controls = array('left' => '‹', 'right' => '›'); + public $controls = array('‹', '›'); /** * @var array list of images to appear in the carousel. If this property is empty, * the widget will not render anything. Each array element represents a single image in the carousel @@ -52,9 +51,9 @@ class Carousel extends Widget * * ```php * array( - * 'content' => 'src of the image', // required + * 'content' => 'html, for example image', // required + * 'caption'=> ['html attributes of the image'], // optional * 'options' => ['html attributes of the item'], // optional - * 'caption'=> ['html attributes of the image'] // optional * ) * ``` */ @@ -95,7 +94,7 @@ class Carousel extends Widget { ob_start(); echo Html::beginTag('ol', array('class' => 'carousel-indicators')) . "\n"; - for ($i = 0, $ln = count($this->items); $i < $ln; $i++) { + for ($i = 0, $count = count($this->items); $i < $count; $i++) { $options = array('data-target' => '#' . $this->options['id'], 'data-slide-to' => $i); if ($i === 0) { $this->addCssClass($options, 'active'); @@ -113,7 +112,7 @@ class Carousel extends Widget { ob_start(); echo Html::beginTag('div', array('class' => 'carousel-inner')) . "\n"; - for ($i = 0, $ln = count($this->items); $i < $ln; $i++) { + for ($i = 0, $count = count($this->items); $i < $count; $i++) { $this->renderItem($this->items[$i], $i); } echo Html::endTag('div') . "\n"; @@ -128,25 +127,25 @@ class Carousel extends Widget public function renderItem($item, $index) { if (is_string($item)) { - $itemOptions = array(); $itemContent = $item; - $itemCaption = ''; + $itemCaption = null; + $itemOptions = array(); } else { - $itemOptions = ArrayHelper::getValue($item, 'options', array()); $itemContent = $item['content']; // if not string, must be array, force required key $itemCaption = ArrayHelper::getValue($item, 'caption'); - if ($itemCaption) { - $itemCaption = Html::tag('div', $itemCaption, array('class' => 'carousel-caption')); - } + $itemOptions = ArrayHelper::getValue($item, 'options', array()); } $this->addCssClass($itemOptions, 'item'); if ($index === 0) { $this->addCssClass($itemOptions, 'active'); } + echo Html::beginTag('div', $itemOptions) . "\n"; echo $itemContent . "\n"; - echo $itemCaption . "\n"; + if ($itemCaption !== null) { + echo Html::tag('div', $itemCaption, array('class' => 'carousel-caption')) . "\n"; + } echo Html::endTag('div') . "\n"; } @@ -155,17 +154,16 @@ class Carousel extends Widget */ public function renderPreviousAndNext() { - if ($this->controls === false || !(isset($this->controls['left']) && isset($this->controls['left']))) { + if ($this->controls === false || !(isset($this->controls[0], $this->controls[1]))) { return; } - echo Html::a($this->controls['left'], '#' . $this->options['id'], array( + echo Html::a($this->controls[0], '#' . $this->options['id'], array( 'class' => 'left carousel-control', - 'data-slide' => 'prev' - )) . - "\n" . - Html::a($this->controls['right'], '#' . $this->options['id'], array( + 'data-slide' => 'prev', + )) . "\n" + . Html::a($this->controls[1], '#' . $this->options['id'], array( 'class' => 'right carousel-control', - 'data-slide' => 'next' + 'data-slide' => 'next', )); } -} \ No newline at end of file +} From c4c16d88768de87e2fa4a3d78c5b7322accc298f Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Thu, 23 May 2013 02:14:21 +0400 Subject: [PATCH 16/20] yii\bootstrap\Carousel fixes --- framework/yii/bootstrap/Carousel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/yii/bootstrap/Carousel.php b/framework/yii/bootstrap/Carousel.php index 6515eb6..5b3449e 100644 --- a/framework/yii/bootstrap/Carousel.php +++ b/framework/yii/bootstrap/Carousel.php @@ -26,7 +26,7 @@ use yii\helpers\Html; * ), * array( * 'content' => '', - * 'caption' => '

This is title

This is the caption text

', + * 'caption' => '

This is title

This is the caption text

', * 'options' => array(...), * ), * ) From 291f3b35d0b696cfbb201fb797486caa0434cb0c Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Wed, 22 May 2013 18:20:56 -0400 Subject: [PATCH 17/20] Refactored Inflector. --- framework/yii/helpers/base/Inflector.php | 470 +++++++++++-------------- tests/unit/framework/helpers/InflectorTest.php | 2 - 2 files changed, 204 insertions(+), 268 deletions(-) diff --git a/framework/yii/helpers/base/Inflector.php b/framework/yii/helpers/base/Inflector.php index 3c1927c..cc5d33f 100644 --- a/framework/yii/helpers/base/Inflector.php +++ b/framework/yii/helpers/base/Inflector.php @@ -17,233 +17,206 @@ use Yii; */ class Inflector { - /** - * @var array rules of plural words + * @var array the rules for converting a word into its plural form. + * The keys are the regular expressions and the values are the corresponding replacements. */ - protected static $plural = array( - 'rules' => array( - '/(m)ove$/i' => '\1oves', - '/(f)oot$/i' => '\1eet', - '/(h)uman$/i' => '\1umans', - '/(s)tatus$/i' => '\1\2tatuses', - '/(s)taff$/i' => '\1taff', - '/(t)ooth$/i' => '\1eeth', - '/(quiz)$/i' => '\1zes', - '/^(ox)$/i' => '\1\2en', - '/([m|l])ouse$/i' => '\1ice', - '/(matr|vert|ind)(ix|ex)$/i' => '\1ices', - '/(x|ch|ss|sh)$/i' => '\1es', - '/([^aeiouy]|qu)y$/i' => '\1ies', - '/(hive)$/i' => '\1s', - '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', - '/sis$/i' => 'ses', - '/([ti])um$/i' => '\1a', - '/(p)erson$/i' => '\1eople', - '/(m)an$/i' => '\1en', - '/(c)hild$/i' => '\1hildren', - '/(buffal|tomat|potat|ech|her|vet)o$/i' => '\1oes', - '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i', - '/us$/i' => 'uses', - '/(alias)$/i' => '\1es', - '/(ax|cris|test)is$/i' => '\1es', - '/s$/' => 's', - '/^$/' => '', - '/$/' => 's', - ), - 'uninflected' => array( - '.*[nrlm]ese', - '.*deer', - '.*fish', - '.*measles', - '.*ois', - '.*pox', - '.*sheep', - 'people' - ), - 'irregular' => array( - 'atlas' => 'atlases', - 'beef' => 'beefs', - 'brother' => 'brothers', - 'cafe' => 'cafes', - 'child' => 'children', - 'cookie' => 'cookies', - 'corpus' => 'corpuses', - 'cow' => 'cows', - 'ganglion' => 'ganglions', - 'genie' => 'genies', - 'genus' => 'genera', - 'graffito' => 'graffiti', - 'hoof' => 'hoofs', - 'loaf' => 'loaves', - 'man' => 'men', - 'money' => 'monies', - 'mongoose' => 'mongooses', - 'move' => 'moves', - 'mythos' => 'mythoi', - 'niche' => 'niches', - 'numen' => 'numina', - 'occiput' => 'occiputs', - 'octopus' => 'octopuses', - 'opus' => 'opuses', - 'ox' => 'oxen', - 'penis' => 'penises', - 'person' => 'people', - 'sex' => 'sexes', - 'soliloquy' => 'soliloquies', - 'testis' => 'testes', - 'trilby' => 'trilbys', - 'turf' => 'turfs' - ) + public static $plurals = array( + '/([nrlm]ese|deer|fish|sheep|measles|ois|pox|media)$/i' => '\1', + '/^(sea[- ]bass)$/i' => '\1', + '/(m)ove$/i' => '\1oves', + '/(f)oot$/i' => '\1eet', + '/(h)uman$/i' => '\1umans', + '/(s)tatus$/i' => '\1tatuses', + '/(s)taff$/i' => '\1taff', + '/(t)ooth$/i' => '\1eeth', + '/(quiz)$/i' => '\1zes', + '/^(ox)$/i' => '\1\2en', + '/([m|l])ouse$/i' => '\1ice', + '/(matr|vert|ind)(ix|ex)$/i' => '\1ices', + '/(x|ch|ss|sh)$/i' => '\1es', + '/([^aeiouy]|qu)y$/i' => '\1ies', + '/(hive)$/i' => '\1s', + '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', + '/sis$/i' => 'ses', + '/([ti])um$/i' => '\1a', + '/(p)erson$/i' => '\1eople', + '/(m)an$/i' => '\1en', + '/(c)hild$/i' => '\1hildren', + '/(buffal|tomat|potat|ech|her|vet)o$/i' => '\1oes', + '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i', + '/us$/i' => 'uses', + '/(alias)$/i' => '\1es', + '/(ax|cris|test)is$/i' => '\1es', + '/s$/' => 's', + '/^$/' => '', + '/$/' => 's', ); /** - * @var array the rules to singular inflector + * @var array the rules for converting a word into its singular form. + * The keys are the regular expressions and the values are the corresponding replacements. */ - protected static $singular = array( - 'rules' => array( - '/(s)tatuses$/i' => '\1\2tatus', - '/(f)eet$/i' => '\1oot', - '/(t)eeth$/i' => '\1ooth', - '/^(.*)(menu)s$/i' => '\1\2', - '/(quiz)zes$/i' => '\\1', - '/(matr)ices$/i' => '\1ix', - '/(vert|ind)ices$/i' => '\1ex', - '/^(ox)en/i' => '\1', - '/(alias)(es)*$/i' => '\1', - '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us', - '/([ftw]ax)es/i' => '\1', - '/(cris|ax|test)es$/i' => '\1is', - '/(shoe|slave)s$/i' => '\1', - '/(o)es$/i' => '\1', - '/ouses$/' => 'ouse', - '/([^a])uses$/' => '\1us', - '/([m|l])ice$/i' => '\1ouse', - '/(x|ch|ss|sh)es$/i' => '\1', - '/(m)ovies$/i' => '\1\2ovie', - '/(s)eries$/i' => '\1\2eries', - '/([^aeiouy]|qu)ies$/i' => '\1y', - '/([lr])ves$/i' => '\1f', - '/(tive)s$/i' => '\1', - '/(hive)s$/i' => '\1', - '/(drive)s$/i' => '\1', - '/([^fo])ves$/i' => '\1fe', - '/(^analy)ses$/i' => '\1sis', - '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis', - '/([ti])a$/i' => '\1um', - '/(p)eople$/i' => '\1\2erson', - '/(m)en$/i' => '\1an', - '/(c)hildren$/i' => '\1\2hild', - '/(n)ews$/i' => '\1\2ews', - '/eaus$/' => 'eau', - '/^(.*us)$/' => '\\1', - '/s$/i' => '' - ), - 'uninflected' => array( - '.*[nrlm]ese', - '.*deer', - '.*fish', - '.*measles', - '.*ois', - '.*pox', - '.*sheep', - '.*ss' - ), - 'irregular' => array( - 'foes' => 'foe', - 'waves' => 'wave', - 'curves' => 'curve' - ) + public static $singulars = array( + '/([nrlm]ese|deer|fish|sheep|measles|ois|pox|media|ss)$/i' => '\1', + '/^(sea[- ]bass)$/i' => '\1', + '/(s)tatuses$/i' => '\1tatus', + '/(f)eet$/i' => '\1oot', + '/(t)eeth$/i' => '\1ooth', + '/^(.*)(menu)s$/i' => '\1\2', + '/(quiz)zes$/i' => '\\1', + '/(matr)ices$/i' => '\1ix', + '/(vert|ind)ices$/i' => '\1ex', + '/^(ox)en/i' => '\1', + '/(alias)(es)*$/i' => '\1', + '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us', + '/([ftw]ax)es/i' => '\1', + '/(cris|ax|test)es$/i' => '\1is', + '/(shoe|slave)s$/i' => '\1', + '/(o)es$/i' => '\1', + '/ouses$/' => 'ouse', + '/([^a])uses$/' => '\1us', + '/([m|l])ice$/i' => '\1ouse', + '/(x|ch|ss|sh)es$/i' => '\1', + '/(m)ovies$/i' => '\1\2ovie', + '/(s)eries$/i' => '\1\2eries', + '/([^aeiouy]|qu)ies$/i' => '\1y', + '/([lr])ves$/i' => '\1f', + '/(tive)s$/i' => '\1', + '/(hive)s$/i' => '\1', + '/(drive)s$/i' => '\1', + '/([^fo])ves$/i' => '\1fe', + '/(^analy)ses$/i' => '\1sis', + '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis', + '/([ti])a$/i' => '\1um', + '/(p)eople$/i' => '\1\2erson', + '/(m)en$/i' => '\1an', + '/(c)hildren$/i' => '\1\2hild', + '/(n)ews$/i' => '\1\2ews', + '/eaus$/' => 'eau', + '/^(.*us)$/' => '\\1', + '/s$/i' => '', ); - /** - * @var array list of words that should not be inflected + * @var array the special rules for converting a word between its plural form and singular form. + * The keys are the special words in singular form, and the values are the corresponding plural form. */ - protected static $uninflected = array( - 'Amoyese', - 'bison', - 'Borghese', - 'bream', - 'breeches', - 'britches', - 'buffalo', - 'cantus', - 'carp', - 'chassis', - 'clippers', - 'cod', - 'coitus', - 'Congoese', - 'contretemps', - 'corps', - 'debris', - 'diabetes', - 'djinn', - 'eland', - 'elk', - 'equipment', - 'Faroese', - 'flounder', - 'Foochowese', - 'gallows', - 'Genevese', - 'Genoese', - 'Gilbertese', - 'graffiti', - 'headquarters', - 'herpes', - 'hijinks', - 'Hottentotese', - 'information', - 'innings', - 'jackanapes', - 'Kiplingese', - 'Kongoese', - 'Lucchese', - 'mackerel', - 'Maltese', - '.*?media', - 'mews', - 'moose', - 'mumps', - 'Nankingese', - 'news', - 'nexus', - 'Niasese', - 'Pekingese', - 'Piedmontese', - 'pincers', - 'Pistoiese', - 'pliers', - 'Portuguese', - 'proceedings', - 'rabies', - 'rice', - 'rhinoceros', - 'salmon', - 'Sarawakese', - 'scissors', - 'sea[- ]bass', - 'series', - 'Shavese', - 'shears', - 'siemens', - 'species', - 'swine', - 'testes', - 'trousers', - 'trout', - 'tuna', - 'Vermontese', - 'Wenchowese', - 'whiting', - 'wildebeest', - 'Yengeese' + public static $specials = array( + 'atlas' => 'atlases', + 'beef' => 'beefs', + 'brother' => 'brothers', + 'cafe' => 'cafes', + 'child' => 'children', + 'cookie' => 'cookies', + 'corpus' => 'corpuses', + 'cow' => 'cows', + 'curve' => 'curves', + 'foe' => 'foes', + 'ganglion' => 'ganglions', + 'genie' => 'genies', + 'genus' => 'genera', + 'graffito' => 'graffiti', + 'hoof' => 'hoofs', + 'loaf' => 'loaves', + 'man' => 'men', + 'money' => 'monies', + 'mongoose' => 'mongooses', + 'move' => 'moves', + 'mythos' => 'mythoi', + 'niche' => 'niches', + 'numen' => 'numina', + 'occiput' => 'occiputs', + 'octopus' => 'octopuses', + 'opus' => 'opuses', + 'ox' => 'oxen', + 'penis' => 'penises', + 'sex' => 'sexes', + 'soliloquy' => 'soliloquies', + 'testis' => 'testes', + 'trilby' => 'trilbys', + 'turf' => 'turfs', + 'wave' => 'waves', + 'Amoyese' => 'Amoyese', + 'bison' => 'bison', + 'Borghese' => 'Borghese', + 'bream' => 'bream', + 'breeches' => 'breeches', + 'britches' => 'britches', + 'buffalo' => 'buffalo', + 'cantus' => 'cantus', + 'carp' => 'carp', + 'chassis' => 'chassis', + 'clippers' => 'clippers', + 'cod' => 'cod', + 'coitus' => 'coitus', + 'Congoese' => 'Congoese', + 'contretemps' => 'contretemps', + 'corps' => 'corps', + 'debris' => 'debris', + 'diabetes' => 'diabetes', + 'djinn' => 'djinn', + 'eland' => 'eland', + 'elk' => 'elk', + 'equipment' => 'equipment', + 'Faroese' => 'Faroese', + 'flounder' => 'flounder', + 'Foochowese' => 'Foochowese', + 'gallows' => 'gallows', + 'Genevese' => 'Genevese', + 'Genoese' => 'Genoese', + 'Gilbertese' => 'Gilbertese', + 'graffiti' => 'graffiti', + 'headquarters' => 'headquarters', + 'herpes' => 'herpes', + 'hijinks' => 'hijinks', + 'Hottentotese' => 'Hottentotese', + 'information' => 'information', + 'innings' => 'innings', + 'jackanapes' => 'jackanapes', + 'Kiplingese' => 'Kiplingese', + 'Kongoese' => 'Kongoese', + 'Lucchese' => 'Lucchese', + 'mackerel' => 'mackerel', + 'Maltese' => 'Maltese', + 'mews' => 'mews', + 'moose' => 'moose', + 'mumps' => 'mumps', + 'Nankingese' => 'Nankingese', + 'news' => 'news', + 'nexus' => 'nexus', + 'Niasese' => 'Niasese', + 'Pekingese' => 'Pekingese', + 'Piedmontese' => 'Piedmontese', + 'pincers' => 'pincers', + 'Pistoiese' => 'Pistoiese', + 'pliers' => 'pliers', + 'Portuguese' => 'Portuguese', + 'proceedings' => 'proceedings', + 'rabies' => 'rabies', + 'rice' => 'rice', + 'rhinoceros' => 'rhinoceros', + 'salmon' => 'salmon', + 'Sarawakese' => 'Sarawakese', + 'scissors' => 'scissors', + 'series' => 'series', + 'Shavese' => 'Shavese', + 'shears' => 'shears', + 'siemens' => 'siemens', + 'species' => 'species', + 'swine' => 'swine', + 'testes' => 'testes', + 'trousers' => 'trousers', + 'trout' => 'trout', + 'tuna' => 'tuna', + 'Vermontese' => 'Vermontese', + 'Wenchowese' => 'Wenchowese', + 'whiting' => 'whiting', + 'wildebeest' => 'wildebeest', + 'Yengeese' => 'Yengeese', ); - /** - * @var array map of special chars and its translation + * @var array map of special chars and its translation. This is used by [[slug()]]. */ - protected static $transliteration = array( + public static $transliteration = array( '/ä|æ|ǽ/' => 'ae', '/ö|œ/' => 'oe', '/ü/' => 'ue', @@ -305,19 +278,10 @@ class Inflector */ public static function pluralize($word) { - $unInflected = ArrayHelper::merge(static::$plural['uninflected'], static::$uninflected); - $irregular = array_keys(static::$plural['irregular']); - - $unInflectedRegex = '(?:' . implode('|', $unInflected) . ')'; - $irregularRegex = '(?:' . implode('|', $irregular) . ')'; - - if (preg_match('/(.*)\\b(' . $irregularRegex . ')$/i', $word, $regs)) - return $regs[1] . substr($word, 0, 1) . substr(static::$plural['irregular'][strtolower($regs[2])], 1); - - if (preg_match('/^(' . $unInflectedRegex . ')$/i', $word, $regs)) - return $word; - - foreach (static::$plural['rules'] as $rule => $replacement) { + if (isset(self::$specials[$word])) { + return self::$specials[$word]; + } + foreach (static::$plurals as $rule => $replacement) { if (preg_match($rule, $word)) { return preg_replace($rule, $replacement, $word); } @@ -332,27 +296,11 @@ class Inflector */ public static function singularize($word) { - - $unInflected = ArrayHelper::merge(static::$singular['uninflected'], static::$uninflected); - - $irregular = array_merge( - static::$singular['irregular'], - array_flip(static::$plural['irregular']) - ); - - $unInflectedRegex = '(?:' . implode('|', $unInflected) . ')'; - $irregularRegex = '(?:' . implode('|', array_keys($irregular)) . ')'; - - - if (preg_match('/(.*)\\b(' . $irregularRegex . ')$/i', $word, $regs)) - return $regs[1] . substr($word, 0, 1) . substr($irregular[strtolower($regs[2])], 1); - - - if (preg_match('/^(' . $unInflectedRegex . ')$/i', $word, $regs)) - return $word; - - - foreach (static::$singular['rules'] as $rule => $replacement) { + $result = array_search($word, self::$specials, true); + if ($result !== false) { + return $result; + } + foreach (static::$singulars as $rule => $replacement) { if (preg_match($rule, $word)) { return preg_replace($rule, $replacement, $word); } @@ -369,7 +317,6 @@ class Inflector */ public static function titleize($words, $ucAll = false) { - $words = static::humanize(static::underscore($words), $ucAll); return $ucAll ? ucwords($words) : ucfirst($words); } @@ -492,7 +439,6 @@ class Inflector */ public static function slug($string, $replacement = '-') { - $map = static::$transliteration + array( '/[^\w\s]/' => ' ', '/\\s+/' => $replacement, @@ -521,20 +467,12 @@ class Inflector { if (in_array(($number % 100), range(11, 13))) { return $number . 'th'; - } else { - switch (($number % 10)) { - case 1: - return $number . 'st'; - break; - case 2: - return $number . 'nd'; - break; - case 3: - return $number . 'rd'; - default: - return $number . 'th'; - break; - } + } + switch (($number % 10)) { + case 1: return $number . 'st'; + case 2: return $number . 'nd'; + case 3: return $number . 'rd'; + default: return $number . 'th'; } } } diff --git a/tests/unit/framework/helpers/InflectorTest.php b/tests/unit/framework/helpers/InflectorTest.php index 9f9626d..f1a98ba 100644 --- a/tests/unit/framework/helpers/InflectorTest.php +++ b/tests/unit/framework/helpers/InflectorTest.php @@ -8,8 +8,6 @@ use yiiunit\TestCase; class InflectorTest extends TestCase { - - public function testPluralize() { $testData = array( From 210faf787e02adee1ffcf9d052cc2337f3744078 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Wed, 22 May 2013 19:52:31 -0400 Subject: [PATCH 18/20] refactored Carousel. --- framework/yii/bootstrap/Carousel.php | 101 ++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 49 deletions(-) diff --git a/framework/yii/bootstrap/Carousel.php b/framework/yii/bootstrap/Carousel.php index 5b3449e..3d38b54 100644 --- a/framework/yii/bootstrap/Carousel.php +++ b/framework/yii/bootstrap/Carousel.php @@ -7,8 +7,7 @@ namespace yii\bootstrap; -use Yii; -use yii\base\Model; +use yii\base\InvalidConfigException; use yii\helpers\base\ArrayHelper; use yii\helpers\Html; @@ -20,10 +19,13 @@ use yii\helpers\Html; * ```php * echo Carousel::widget(array( * 'items' => array( + * // the item contains only the image * '', + * // equivalent to the above * array( * 'content' => '', * ), + * // the item contains both the image and the caption * array( * 'content' => '', * 'caption' => '

This is title

This is the caption text

', @@ -40,20 +42,22 @@ use yii\helpers\Html; class Carousel extends Widget { /** - * @var array indicates what labels should be displayed on next and previous carousel controls. If [[controls]] is - * set to `false` the controls will not be displayed. + * @var array|boolean the labels for the previous and the next control buttons. + * If false, it means the previous and the next control buttons should not be displayed. */ public $controls = array('‹', '›'); /** - * @var array list of images to appear in the carousel. If this property is empty, - * the widget will not render anything. Each array element represents a single image in the carousel - * with the following structure: + * @var array list of slides in the carousel. Each array element represents a single + * slide with the following structure: * * ```php * array( - * 'content' => 'html, for example image', // required - * 'caption'=> ['html attributes of the image'], // optional - * 'options' => ['html attributes of the item'], // optional + * // required, slide content (HTML), such as an image tag + * 'content' => '', + * // optional, the caption (HTML) of the slide + * 'caption'=> '

This is title

This is the caption text

', + * // optional the HTML attributes of the slide container + * 'options' => array(), * ) * ``` */ @@ -74,90 +78,84 @@ class Carousel extends Widget */ public function run() { - if (empty($this->items)) { - return; - } - echo Html::beginTag('div', $this->options) . "\n"; echo $this->renderIndicators() . "\n"; echo $this->renderItems() . "\n"; - echo $this->renderPreviousAndNext() . "\n"; + echo $this->renderControls() . "\n"; echo Html::endTag('div') . "\n"; - $this->registerPlugin('carousel'); } /** - * Renders carousel indicators + * Renders carousel indicators. + * @return string the rendering result */ public function renderIndicators() { - ob_start(); - echo Html::beginTag('ol', array('class' => 'carousel-indicators')) . "\n"; + $indicators = array(); for ($i = 0, $count = count($this->items); $i < $count; $i++) { $options = array('data-target' => '#' . $this->options['id'], 'data-slide-to' => $i); if ($i === 0) { $this->addCssClass($options, 'active'); } - echo Html::tag('li', '', $options) . "\n"; + $indicators[] = Html::tag('li', '', $options); } - echo Html::endTag('ol') . "\n"; - return ob_get_clean(); + return Html::tag('ol', implode("\n", $indicators), array('class' => 'carousel-indicators')); } /** - * Renders carousel items as specified on [[items]] + * Renders carousel items as specified on [[items]]. + * @return string the rendering result */ public function renderItems() { - ob_start(); - echo Html::beginTag('div', array('class' => 'carousel-inner')) . "\n"; + $items = array(); for ($i = 0, $count = count($this->items); $i < $count; $i++) { - $this->renderItem($this->items[$i], $i); + $items[] = $this->renderItem($this->items[$i], $i); } - echo Html::endTag('div') . "\n"; - return ob_get_clean(); + return Html::tag('div', implode("\n", $items), array('class' => 'carousel-inner')); } /** * Renders a single carousel item - * @param mixed $item a single item from [[items]] + * @param string|array $item a single item from [[items]] * @param integer $index the item index as the first item should be set to `active` + * @return string the rendering result + * @throws InvalidConfigException if the item is invalid */ public function renderItem($item, $index) { if (is_string($item)) { - $itemContent = $item; - $itemCaption = null; - $itemOptions = array(); + $content = $item; + $caption = null; + $options = array(); + } elseif (isset($item['content'])) { + $content = $item['content']; + $caption = ArrayHelper::getValue($item, 'caption'); + if ($caption !== null) { + $caption = Html::tag('div', $caption, array('class' => 'carousel-caption')); + } + $options = ArrayHelper::getValue($item, 'options', array()); } else { - $itemContent = $item['content']; // if not string, must be array, force required key - $itemCaption = ArrayHelper::getValue($item, 'caption'); - $itemOptions = ArrayHelper::getValue($item, 'options', array()); + throw new InvalidConfigException('The "content" option is required.'); } - $this->addCssClass($itemOptions, 'item'); + $this->addCssClass($options, 'item'); if ($index === 0) { - $this->addCssClass($itemOptions, 'active'); + $this->addCssClass($options, 'active'); } - echo Html::beginTag('div', $itemOptions) . "\n"; - echo $itemContent . "\n"; - if ($itemCaption !== null) { - echo Html::tag('div', $itemCaption, array('class' => 'carousel-caption')) . "\n"; - } - echo Html::endTag('div') . "\n"; + return Html::tag('div', $content . "\n" . $caption, $options); } /** - * Renders previous and next button if [[displayPreviousAndNext]] is set to `true` + * Renders previous and next control buttons. + * @throws InvalidConfigException if [[controls]] is invalid. */ - public function renderPreviousAndNext() + public function renderControls() { - if ($this->controls === false || !(isset($this->controls[0], $this->controls[1]))) { - return; - } - echo Html::a($this->controls[0], '#' . $this->options['id'], array( + if (isset($this->controls[0], $this->controls[1])) { + return Html::a($this->controls[0], '#' . $this->options['id'], array( 'class' => 'left carousel-control', 'data-slide' => 'prev', )) . "\n" @@ -165,5 +163,10 @@ class Carousel extends Widget 'class' => 'right carousel-control', 'data-slide' => 'next', )); + } elseif ($this->controls === false) { + return ''; + } else { + throw new InvalidConfigException('The "controls" property must be either false or an array of two elements.'); + } } } From 767d99c30bb8b7fc748304c057bf91dd74cf4e15 Mon Sep 17 00:00:00 2001 From: Luciano Baraglia Date: Thu, 23 May 2013 00:49:07 -0300 Subject: [PATCH 19/20] Fixing some outdated comments --- docs/guide/upgrade-from-v1.md | 8 +++++--- framework/yii/widgets/Menu.php | 1 - 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/guide/upgrade-from-v1.md b/docs/guide/upgrade-from-v1.md index cc0de73..b3d4411 100644 --- a/docs/guide/upgrade-from-v1.md +++ b/docs/guide/upgrade-from-v1.md @@ -216,12 +216,14 @@ Using a widget is more straightforward in 2.0. You mainly use the `begin()`, `en methods of the `Widget` class. For example, ```php -// $this refers to the View object // Note that you have to "echo" the result to display it echo \yii\widgets\Menu::widget(array('items' => $items)); -// $this refers to the View object -$form = \yii\widgets\ActiveForm::begin($this); +// Passing an array to initialize the object properties +$form = \yii\widgets\ActiveForm::begin(array( + 'options' => array('class' => 'form-horizontal'), + 'fieldConfig' => array('inputOptions' => array('class' => 'input-xlarge')), +)); ... form inputs here ... \yii\widgets\ActiveForm::end(); ``` diff --git a/framework/yii/widgets/Menu.php b/framework/yii/widgets/Menu.php index e76f11f..d63f202 100644 --- a/framework/yii/widgets/Menu.php +++ b/framework/yii/widgets/Menu.php @@ -26,7 +26,6 @@ use yii\helpers\Html; * The following example shows how to use Menu: * * ~~~ - * // $this is the view object currently being used * echo Menu::widget(array( * 'items' => array( * // Important: you need to specify url as 'controller/action', From 68adc6117e42ad28e7026c7aae5d641354a1a780 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Thu, 23 May 2013 12:38:42 +0400 Subject: [PATCH 20/20] Lowered PHP requirement to 5.3.7 --- framework/composer.json | 2 +- framework/yii/requirements/requirements.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/framework/composer.json b/framework/composer.json index 4ef6f89..2f0e85f 100644 --- a/framework/composer.json +++ b/framework/composer.json @@ -64,7 +64,7 @@ "source": "https://github.com/yiisoft/yii2" }, "require": { - "php": ">=5.3.11", + "php": ">=5.3.7", "ext-mbstring": "*", "lib-pcre": "*" }, diff --git a/framework/yii/requirements/requirements.php b/framework/yii/requirements/requirements.php index bc53c03..0dbc1fc 100644 --- a/framework/yii/requirements/requirements.php +++ b/framework/yii/requirements/requirements.php @@ -9,9 +9,9 @@ return array( array( 'name' => 'PHP version', 'mandatory' => true, - 'condition' => version_compare(PHP_VERSION, '5.3.11', '>='), + 'condition' => version_compare(PHP_VERSION, '5.3.7', '>='), 'by' => 'Yii Framework', - 'memo' => 'PHP 5.3.11 or higher is required.', + 'memo' => 'PHP 5.3.7 or higher is required.', ), array( 'name' => 'Reflection extension',