/var/www/(Del)gepgroup.hk/php-activerecord/lib/Inflector.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
 * @package ActiveRecord
 */
namespace ActiveRecord;

/**
 * @package ActiveRecord
 */
abstract class Inflector
{
    
/**
     * Get an instance of the {@link Inflector} class.
     *
     * @return object
     */
    
public static function instance()
    {
        return new 
StandardInflector();
    }

    
/**
     * Turn a string into its camelized version.
     *
     * @param string $s string to convert
     * @return string
     */
    
public function camelize($s)
    {
        
$s preg_replace('/[_-]+/','_',trim($s));
        
$s str_replace(' ''_'$s);

        
$camelized '';

        for (
$i=0,$n=strlen($s); $i<$n; ++$i)
        {
            if (
$s[$i] == '_' && $i+$n)
                
$camelized .= strtoupper($s[++$i]);
            else
                
$camelized .= $s[$i];
        }

        
$camelized trim($camelized,' _');

        if (
strlen($camelized) > 0)
            
$camelized[0] = strtolower($camelized[0]);

        return 
$camelized;
    }

    
/**
     * Determines if a string contains all uppercase characters.
     *
     * @param string $s string to check
     * @return bool
     */
    
public static function is_upper($s)
    {
        return (
strtoupper($s) === $s);
    }

    
/**
     * Determines if a string contains all lowercase characters.
     *
     * @param string $s string to check
     * @return bool
     */
    
public static function is_lower($s)
    {
        return (
strtolower($s) === $s);
    }

    
/**
     * Convert a camelized string to a lowercase, underscored string.
     *
     * @param string $s string to convert
     * @return string
     */
    
public function uncamelize($s)
    {
        
$normalized '';

        for (
$i=0,$n=strlen($s); $i<$n; ++$i)
        {
            if (
ctype_alpha($s[$i]) && self::is_upper($s[$i]))
                
$normalized .= '_' strtolower($s[$i]);
            else
                
$normalized .= $s[$i];
        }
        return 
trim($normalized,' _');
    }

    
/**
     * Convert a string with space into a underscored equivalent.
     *
     * @param string $s string to convert
     * @return string
     */
    
public function underscorify($s)
    {
        return 
preg_replace(array('/[_\- ]+/','/([a-z])([A-Z])/'),array('_','\\1_\\2'),trim($s));
    }

    public function 
keyify($class_name)
    {
        return 
strtolower($this->underscorify(denamespace($class_name))) . '_id';
    }

    abstract function 
variablize($s);
}

/**
 * @package ActiveRecord
 */
class StandardInflector extends Inflector
{
    public function 
tableize($s) { return Utils::pluralize(strtolower($this->underscorify($s))); }
    public function 
variablize($s) { return str_replace(array('-',' '),array('_','_'),strtolower(trim($s))); }
}
?>