[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/util/ -> sfInflector.class.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the symfony package.
   5   * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
   6   *
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  /**
  12   *
  13   * @package    symfony
  14   * @subpackage util
  15   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  16   * @version    SVN: $Id: sfInflector.class.php 1415 2006-06-11 08:33:51Z fabien $
  17   */
  18  class sfInflector
  19  {
  20    /**
  21     * Returns a camelized string from a lower case and underscored string by replaceing slash with
  22     * double-colol and upper-casing each letter preceded by an underscore.
  23     *
  24     * @param string String to camelize.
  25     *
  26     * @return string Camelized string.
  27     */
  28    public static function camelize($lower_case_and_underscored_word)
  29    {
  30      $tmp = $lower_case_and_underscored_word;
  31      $tmp = sfToolkit::pregtr($tmp, array('#/(.?)#e'    => "'::'.strtoupper('\\1')",
  32                                           '/(^|_)(.)/e' => "strtoupper('\\2')"));
  33  
  34      return $tmp;
  35    }
  36  
  37    /**
  38     * Returns an underscore-syntaxed version or the CamelCased string.
  39     *
  40     * @param string String to underscore.
  41     *
  42     * @return string Underscored string.
  43     */
  44    public static function underscore($camel_cased_word)
  45    {
  46      $tmp = $camel_cased_word;
  47      $tmp = str_replace('::', '/', $tmp);
  48      $tmp = sfToolkit::pregtr($tmp, array('/([A-Z]+)([A-Z][a-z])/' => '\\1_\\2',
  49                                           '/([a-z\d])([A-Z])/'     => '\\1_\\2'));
  50  
  51      return strtolower($tmp);
  52    }
  53  
  54    /**
  55     * Returns classname::module with classname:: stripped off.
  56     *
  57     * @param string Classname and module pair.
  58     *
  59     * @return string Module name.
  60     */
  61    public static function demodulize($class_name_in_module)
  62    {
  63      return preg_replace('/^.*::/', '', $class_name_in_module);
  64    }
  65  
  66    /**
  67     * Returns classname in underscored form, with "_id" tacked on at the end.
  68     * This is for use in dealing with foreign keys in the database.
  69     *
  70     * @param string Class name.
  71     * @param boolean Seperate with underscore.
  72     *
  73     * @return strong Foreign key
  74     */
  75    public static function foreign_key($class_name, $separate_class_name_and_id_with_underscore = true)
  76    {
  77      return sfInflector::underscore(sfInflector::demodulize($class_name)).($separate_class_name_and_id_with_underscore ? "_id" : "id");
  78    }
  79  
  80    /**
  81     * Returns corresponding table name for given classname.
  82     *
  83     * @param string Name of class to get database table name for.
  84     *
  85     * @return string Name of the databse table for given class.
  86     */
  87    public static function tableize($class_name)
  88    {
  89      return sfInflector::underscore($class_name);
  90    }
  91  
  92    /**
  93     * Returns model class name for given database table.
  94     *
  95     * @param string Table name.
  96     *
  97     * @return string Classified table name.
  98     */
  99    public static function classify($table_name)
 100    {
 101      return sfInflector::camelize($table_name);
 102    }
 103  
 104    /**
 105     * Returns a human-readable string from a lower case and underscored word by replacing underscores
 106     * with a space, and by upper-casing the initial characters.
 107     *
 108     * @param string String to make more readable.
 109     *
 110     * @return string Human-readable string.
 111     */
 112    public static function humanize($lower_case_and_underscored_word)
 113    {
 114      if (substr($lower_case_and_underscored_word, -3) === '_id')
 115          $lower_case_and_underscored_word = substr($lower_case_and_underscored_word, 0, -3);
 116      return ucfirst(str_replace('_', ' ', $lower_case_and_underscored_word));
 117    }
 118  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7