[DDC-2160] Smart Pluralize/Singularize support for Doctrine/Common/Util/Inflector #227

Merged
merged 3 commits into from Jan 10, 2013

6 participants

@jbergler

Doctrine/ORM/Tools/EntityGenerator should pluralize/signularize correctly.

This PR adds functionality to Doctrine/Common/Util/Inflector to singularize/pluralize. The code is largely borrowed from a similar class in the CakePHP project - I'm not sure if the updates to the class doc covers the requirements for this.

Test coverage also added.

@jbergler jbergler Adding pluralize/signularize functionality to inflector. This is to b…
…e used in Doctrine/ORM/Tools/EntityGenerator to fix [DDC-2160] properly.
1dff809
@doctrinebot

Hello,

thank you for positing this Pull Request. I have automatically opened an issue on our Jira Bug Tracker for you with the details of this Pull-Request. See the Link:

http://doctrine-project.org/jira/browse/DCOM-147

@jbergler

I am updating the pull request for Doctrine/ORM/Tools/EntityGenerator upstream to use this functionality

@stof stof commented on an outdated diff Nov 23, 2012
lib/Doctrine/Common/Util/Inflector.php
}
+
+Inflector::reset();
@stof
Doctrine member
stof added a line comment Nov 23, 2012

Please remove it. A file containing a class definition should contain only the class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof stof and 1 other commented on an outdated diff Nov 23, 2012
lib/Doctrine/Common/Util/Inflector.php
+ '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'
+ );
+
+ /**
+ * Method cache array.
+ *
+ * @var array
+ */
+ protected static $_cache = array();
@stof
Doctrine member
stof added a line comment Nov 23, 2012

Please remove the leading underscore.

and properties should be declared before methods

@jbergler
jbergler added a line comment Nov 23, 2012

Sure, on both $_cache and _cache() or just the variable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof stof commented on an outdated diff Nov 23, 2012
tests/Doctrine/Tests/Common/Util/InflectorTest.php
+
+namespace Doctrine\Tests\Common\Util
+{
+ use Doctrine\Tests\DoctrineTestCase;
+ use Doctrine\Common\Util\Inflector;
+
+ class InflectorTest extends DoctrineTestCase
+ {
+ /**
+ * testInflectingSingulars method
+ *
+ * @return void
+ */
+ public function testInflectingSingulars() {
+ Inflector::reset();
+ $this->assertEquals(Inflector::singularize('categorias'), 'categoria');
@stof
Doctrine member
stof added a line comment Nov 23, 2012

the expectation is the first argument. Please keep them in the right order to have the right error message when it fails.

and you should probably use a dataProvider for this test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof stof commented on an outdated diff Nov 23, 2012
lib/Doctrine/Common/Util/Inflector.php
+ /**
+ * The initial state of Inflector so reset() works.
+ *
+ * @var array
+ */
+ protected static $_initialState = array();
+
+ /**
+ * Cache inflected values, and return if already available
+ *
+ * @param string $type Inflection type
+ * @param string $key Original value
+ * @param string $value Inflected value
+ * @return string Inflected value, from cache
+ */
+ protected static function _cache($type, $key, $value = false) {
@stof
Doctrine member
stof added a line comment Nov 23, 2012

the curly brace should be on its own line for methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@beberlei
Doctrine member

Awesome, can you change the protected to private? This class can reastically not be overwritten, lets not make people hopes :-)

@jbergler

Done

@schmittjoh
Doctrine member

How about making this a dedicated composer package which we can just reference?

There is no point in each project coming up with own inflectors, no?

@Ocramius
Doctrine member

@schmittjoh the problem is, again, all the non-composer folks.

@schmittjoh
Doctrine member

@Ocramius, if they exists, let them voice their concern, but we cannot stay in the past forever.

@Ocramius
Doctrine member

@schmittjoh I'm all for deprecating all that stuff, we are just forced to keep BC until 3.x.

@schmittjoh
Doctrine member

Regarding BC that should be easy here as these are all new features.

@beberlei
Doctrine member

@schmittjoh i haven't actually thought about this, i suppose a dedicated component for this would be pretty nice. I started an internal inquiry if we should drop PEAR support for 2.4

@beberlei
Doctrine member

We will drop PEAR support for 2.4, and will start moving parts out of Common into their own subprojects.

@beberlei
Doctrine member

I will merge this for now, then start extracting it.

@beberlei beberlei merged commit 28b0e7f into doctrine:master Jan 10, 2013

1 check passed

Details default The Travis build passed
@schmittjoh
Doctrine member

That's great news!

Thanks for the swift response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment