Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
17
 */
18
class sfInflector
19
{
20
  /**
21
   * Returns a camelized string from a lower case and underscored string by replaceing slash with
22
   * double-colon and upper-casing each letter preceded by an underscore.
23
   *
24
   * @param  string $lower_case_and_underscored_word  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 $camel_cased_word  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 $class_name_in_module  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                Class name.
71
   * @param bool   $separate_with_underscore  Separate with underscore.
72
   *
73
   * @return strong Foreign key
74
   */
75
  public static function foreign_key($class_name, $separate_with_underscore = true)
76
  {
77
    return sfInflector::underscore(sfInflector::demodulize($class_name)).($separate_with_underscore ? "_id" : "id");
78
  }
79
 
80
  /**
81
   * Returns corresponding table name for given classname.
82
   *
83
   * @param  string $class_name  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  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 $lower_case_and_underscored_word 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
    {
116
      $lower_case_and_underscored_word = substr($lower_case_and_underscored_word, 0, -3);
117
    }
118
 
119
    return ucfirst(str_replace('_', ' ', $lower_case_and_underscored_word));
120
  }
121
}