Inflector.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /*
  3. * $Id: Inflector.php 3189 2007-11-18 20:37:44Z meus $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\Common\Util;
  22. /**
  23. * Doctrine inflector has static methods for inflecting text
  24. *
  25. * The methods in these classes are from several different sources collected
  26. * across several different php projects and several different authors. The
  27. * original author names and emails are not known
  28. *
  29. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  30. * @link www.doctrine-project.org
  31. * @since 1.0
  32. * @version $Revision: 3189 $
  33. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  34. * @author Jonathan H. Wage <jonwage@gmail.com>
  35. */
  36. class Inflector
  37. {
  38. /**
  39. * Convert word in to the format for a Doctrine table name. Converts 'ModelName' to 'model_name'
  40. *
  41. * @param string $word Word to tableize
  42. * @return string $word Tableized word
  43. */
  44. public static function tableize($word)
  45. {
  46. return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $word));
  47. }
  48. /**
  49. * Convert a word in to the format for a Doctrine class name. Converts 'table_name' to 'TableName'
  50. *
  51. * @param string $word Word to classify
  52. * @return string $word Classified word
  53. */
  54. public static function classify($word)
  55. {
  56. return str_replace(" ", "", ucwords(strtr($word, "_-", " ")));
  57. }
  58. /**
  59. * Camelize a word. This uses the classify() method and turns the first character to lowercase
  60. *
  61. * @param string $word
  62. * @return string $word
  63. */
  64. public static function camelize($word)
  65. {
  66. return lcfirst(self::classify($word));
  67. }
  68. }