1 <?php
2 interface phpMorphy_GrammemsProvider_Interface {
3 function getGrammems($partOfSpeech);
4 }
5
6 class phpMorphy_GrammemsProvider_Decorator implements phpMorphy_GrammemsProvider_Interface {
7 protected $inner;
8
9 function __construct(phpMorphy_GrammemsProvider_Interface $inner) {
10 $this->inner = $inner;
11 }
12
13 function getGrammems($partOfSpeech) {
14 return $this->inner->getGrammems($partOfSpeech);
15 }
16 }
17
18 abstract class phpMorphy_GrammemsProvider_Base implements phpMorphy_GrammemsProvider_Interface {
19 protected
20 $all_grammems,
21 $grammems = array();
22
23 function __construct() {
24 $this->all_grammems = $this->flatizeArray($this->getAllGrammemsGrouped());
25 }
26
27 abstract function getAllGrammemsGrouped();
28
29 function includeGroups($partOfSpeech, $names) {
30 $grammems = $this->getAllGrammemsGrouped();
31 $names = array_flip((array)$names);
32
33 foreach(array_keys($grammems) as $key) {
34 if(!isset($names[$key])) {
35 unset($grammems[$key]);
36 }
37 }
38
39 $this->grammems[$partOfSpeech] = $this->flatizeArray($grammems);
40
41 return $this;
42 }
43
44 function excludeGroups($partOfSpeech, $names) {
45 $grammems = $this->getAllGrammemsGrouped();
46
47 foreach((array)$names as $key) {
48 unset($grammems[$key]);
49 }
50
51 $this->grammems[$partOfSpeech] = $this->flatizeArray($grammems);
52
53 return $this;
54 }
55
56 function resetGroups($partOfSpeech) {
57 unset($this->grammems[$partOfSpeech]);
58 return $this;
59 }
60
61 function resetGroupsForAll() {
62 $this->grammems = array();
63 return $this;
64 }
65
66 static function flatizeArray($array) {
67 return call_user_func_array('array_merge', $array);
68 }
69
70 function getGrammems($partOfSpeech) {
71 if(isset($this->grammems[$partOfSpeech])) {
72 return $this->grammems[$partOfSpeech];
73 } else {
74 return $this->all_grammems;
75 }
76 }
77 }
78
79 class phpMorphy_GrammemsProvider_Empty extends phpMorphy_GrammemsProvider_Base {
80 function getAllGrammemsGrouped() {
81 return array();
82 }
83
84 function getGrammems($partOfSpeech) {
85 return false;
86 }
87 }
88
89 abstract class phpMorphy_GrammemsProvider_ForFactory extends phpMorphy_GrammemsProvider_Base {
90 protected
91 $encoded_grammems;
92
93 function __construct($encoding) {
94 $this->encoded_grammems = $this->encodeGrammems($this->getGrammemsMap(), $encoding);
95
96 parent::__construct();
97 }
98
99 abstract function getGrammemsMap();
100
101 function getAllGrammemsGrouped() {
102 return $this->encoded_grammems;
103 }
104
105 protected function encodeGrammems($grammems, $encoding) {
106 $from_encoding = $this->getSelfEncoding();
107
108 if($from_encoding == $encoding) {
109 return $grammems;
110 }
111
112 $result = array();
113
114 foreach($grammems as $key => $ary) {
115 $new_key = iconv($from_encoding, $encoding, $key);
116 $new_value = array();
117
118 foreach($ary as $value) {
119 $new_value[] = iconv($from_encoding, $encoding, $value);
120 }
121
122 $result[$new_key] = $new_value;
123 }
124
125 return $result;
126 }
127 }
128
129 class phpMorphy_GrammemsProvider_Factory {
130 protected static $included = array();
131
132 static function create(phpMorphy $morphy) {
133 $locale = $GLOBALS['__phpmorphy_strtolower']($morphy->getLocale());
134
135 if(!isset(self::$included[$locale])) {
136 $file_name = PHPMORPHY_DIR . "/langs_stuff/$locale.php";
137 $class = "phpMorphy_GrammemsProvider_$locale";
138
139 if(is_readable($file_name)) {
140 require($file_name);
141
142 if(!class_exists($class)) {
143 throw new phpMorphy_Exception("Class '$class' not found in '$file_name' file");
144 }
145
146 self::$included[$locale] = call_user_func(array($class, 'instance'), $morphy);
147 } else {
148 self::$included[$locale] = new phpMorphy_GrammemsProvider_Empty($morphy);
149 }
150 }
151
152
153 return self::$included[$locale];
154 }
155 }
156
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-