common.php 4.09 KB
Newer Older
Яков's avatar
first  
Яков committed
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
interface phpMorphy_GrammemsProvider_Interface {
    function getGrammems($partOfSpeech);
}

class phpMorphy_GrammemsProvider_Decorator implements phpMorphy_GrammemsProvider_Interface {
    protected $inner;

    function __construct(phpMorphy_GrammemsProvider_Interface $inner) {
        $this->inner = $inner;
    }

    function getGrammems($partOfSpeech) {
        return $this->inner->getGrammems($partOfSpeech);
    }
}

abstract class phpMorphy_GrammemsProvider_Base implements phpMorphy_GrammemsProvider_Interface {
    protected
        $all_grammems,
        $grammems = array();

    function __construct() {
        $this->all_grammems = $this->flatizeArray($this->getAllGrammemsGrouped());
    }

    abstract function getAllGrammemsGrouped();

    function includeGroups($partOfSpeech, $names) {
        $grammems = $this->getAllGrammemsGrouped();
        $names = array_flip((array)$names);

        foreach(array_keys($grammems) as $key) {
            if(!isset($names[$key])) {
                unset($grammems[$key]);
            }
        }

        $this->grammems[$partOfSpeech] = $this->flatizeArray($grammems);

        return $this;
    }

    function excludeGroups($partOfSpeech, $names) {
        $grammems = $this->getAllGrammemsGrouped();

        foreach((array)$names as $key) {
            unset($grammems[$key]);
        }

        $this->grammems[$partOfSpeech] = $this->flatizeArray($grammems);

        return $this;
    }

    function resetGroups($partOfSpeech) {
        unset($this->grammems[$partOfSpeech]);
        return $this;
    }

    function resetGroupsForAll() {
        $this->grammems = array();
        return $this;
    }

    static function flatizeArray($array) {
        return call_user_func_array('array_merge', $array);
    }

    function getGrammems($partOfSpeech) {
        if(isset($this->grammems[$partOfSpeech])) {
            return $this->grammems[$partOfSpeech];
        } else {
            return $this->all_grammems;
        }
    }
}

class phpMorphy_GrammemsProvider_Empty extends phpMorphy_GrammemsProvider_Base {
    function getAllGrammemsGrouped() {
        return array();
    }

    function getGrammems($partOfSpeech) {
        return false;
    }
}

abstract class phpMorphy_GrammemsProvider_ForFactory extends phpMorphy_GrammemsProvider_Base {
    protected
        $encoded_grammems;

    function __construct($encoding) {
        $this->encoded_grammems = $this->encodeGrammems($this->getGrammemsMap(), $encoding);

        parent::__construct();
    }

    abstract function getGrammemsMap();

    function getAllGrammemsGrouped() { 
        return $this->encoded_grammems;
    } 

    protected function encodeGrammems($grammems, $encoding) {
        $from_encoding = $this->getSelfEncoding();

        if($from_encoding == $encoding) {
            return $grammems;
        }

        $result = array();

        foreach($grammems as $key => $ary) {
            $new_key = iconv($from_encoding, $encoding, $key);
            $new_value = array();

            foreach($ary as $value) {
                $new_value[] = iconv($from_encoding, $encoding, $value);
            }

            $result[$new_key] = $new_value;
        }

        return $result;
    }
}

class phpMorphy_GrammemsProvider_Factory {
    protected static $included = array();

    static function create(phpMorphy $morphy) {
        $locale = $GLOBALS['__phpmorphy_strtolower']($morphy->getLocale());
        
        if(!isset(self::$included[$locale])) {
            $file_name = PHPMORPHY_DIR . "/langs_stuff/$locale.php";
            $class = "phpMorphy_GrammemsProvider_$locale";

            if(is_readable($file_name)) {
                require($file_name);

                if(!class_exists($class)) {
                    throw new phpMorphy_Exception("Class '$class' not found in '$file_name' file");
                }
                
                self::$included[$locale] = call_user_func(array($class, 'instance'), $morphy);
            } else {
                self::$included[$locale] = new phpMorphy_GrammemsProvider_Empty($morphy);
            }
        }


        return self::$included[$locale];
    }
}