1 : <?php
2 :
3 : /**
4 : * Dictionaries of Old French and Latin
5 : *
6 : * PHP 5
7 : *
8 : * @category DicFro
9 : * @package View
10 : * @subpackage Helper
11 : * @author Michel Corne <mcorne@yahoo.com>
12 : * @copyright 2008-2010 Michel Corne
13 : * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 (GPLv3)
14 : * @link http://www.micmap.org/dicfro
15 : */
16 :
17 : require_once 'View/Helper/Base.php';
18 :
19 : /**
20 : * Verbs View Helper
21 : *
22 : * @category DicFro
23 : * @package View
24 : * @subpackage Helper
25 : * @author Michel Corne <mcorne@yahoo.com>
26 : * @copyright 2008-2010 Michel Corne
27 : * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 (GPLv3)
28 : */
29 :
30 : class View_Helper_Verbs extends View_Helper_Base
31 : {
32 :
33 : /**
34 : * Verb form template
35 : */
36 : const FORM_TPL = '%s %s, %s';
37 :
38 : /**
39 : * Infinitive template
40 : */
41 : const INFINITIVE_TPL = '%s, %s';
42 :
43 : /**
44 : * List of persons
45 : * @var array
46 : */
47 : public $person = array(
48 : '1:' => '(je)',
49 : '2:' => '(tu)',
50 : '3:' => '(il)',
51 : '4:' => '(nous)',
52 : '5:' => '(vous)',
53 : '6:' => '(ils)',
54 : );
55 :
56 : /**
57 : *
58 : * Person numbers
59 : * @var array
60 : */
61 : public $personNumbers;
62 :
63 : /**
64 : *
65 : * Person texts
66 : * @var array
67 : */
68 : public $personTexts;
69 :
70 : /**
71 : * List of tenses
72 : * @var array
73 : */
74 : public $tenses = array(
75 : 'inf.' => 'Infinitif',
76 : 'ind.prés.' => 'Indicatif Présent',
77 : 'ind.impf.' => 'Indicatif Imparfait',
78 : 'pass.simp.' => 'Indicatif Passé simple',
79 : 'pass.arch.' => 'Indicatif Passé archaïque',
80 : 'futur' => 'Indicatif Futur',
81 : 'subj.prés.' => 'Subjonctif Présent',
82 : 'subj.impf.' => 'Subjonctif Imparfait',
83 : 'cond.' => 'Conditionnel',
84 : 'impé.' => 'Impératif',
85 : 'part.prés.' => 'Participe Présent',
86 : 'part.pass.' => 'Participe Passé',
87 : );
88 :
89 : /**
90 : * Cleans with original verb
91 : *
92 : * @param string $original the orginal verb
93 : * @return string the clean verb
94 : */
95 : public function cleanOriginalVerb($original)
96 : {
97 2 : return str_replace('*', '', $original);
98 : }
99 :
100 : /**
101 : * Converts the person
102 : *
103 : * @param string $person the person to convert, ex. "1:"
104 : * @return string the converted person, ex. "(je)"
105 : */
106 : public function convertPersons($person)
107 : {
108 3 : $person .= ':';
109 :
110 3 : return isset($this->person[$person])?
111 3 : $this->person[$person] :
112 3 : "(?)";
113 : }
114 :
115 : /**
116 : * Converts the tense
117 : *
118 : * @param string $tense the tense to convert, ex. "inf."
119 : * @return string the converted tense, ex. "infinitive"
120 : */
121 : public function convertTense($tense)
122 : {
123 2 : return isset($this->tenses[$tense])?
124 2 : $this->tenses[$tense] :
125 2 : $tense . ' (?)';
126 : }
127 :
128 : /**
129 : * Extracts the composed or similar (model) verbs
130 : *
131 : * @param boolean $isComposed extracts composed verbs if true, similar verbs if false
132 : * @return array the list of verbs by model
133 : */
134 : public function extractComposedVerbs($isComposed)
135 : {
136 1 : $this->view->composedVerbs or $this->view->composedVerbs = array();
137 1 : $verbs = array();
138 :
139 1 : foreach($this->view->composedVerbs as $verb) {
140 1 : if ($verb['composed'] == $isComposed) {
141 1 : $original = $this->cleanOriginalVerb($verb['original']);
142 1 : $verbs[$verb['infinitive']][] = array('value' => $original, 'text' => $original);
143 : }
144 1 : }
145 :
146 1 : return $verbs;
147 : }
148 :
149 : /**
150 : * Extracts identified verbs
151 : *
152 : * @return array the identified verbs
153 : */
154 : public function extractIdentifiedVerbs()
155 : {
156 1 : $this->view->identifiedVerbs or $this->view->identifiedVerbs = array();
157 1 : $verbs = array();
158 :
159 1 : foreach($this->view->identifiedVerbs as $verb) {
160 1 : $verbs[$verb['infinitive']][] = array(
161 1 : 'value' => $verb['infinitive'],
162 1 : 'text' => $this->formatVerbForm($verb),
163 : );
164 1 : }
165 :
166 1 : return $verbs;
167 : }
168 :
169 : /**
170 : * Extracts infinitives
171 : *
172 : * @param array $verbs the list of infinitives
173 : * @return string the list of infinitives
174 : */
175 : public function extractInfinitives($verbs)
176 : {
177 1 : return implode(', ', array_keys($verbs));
178 : }
179 :
180 : /**
181 : * Formats the verb form
182 : *
183 : * @param array $verb the verb details
184 : * @return mixed the formatted verb
185 : */
186 : public function formatVerbForm($verb)
187 : {
188 2 : return $verb['person']?
189 2 : sprintf(self::FORM_TPL, $this->convertPersons($verb['person']), $verb['original'], $verb['tense']) :
190 2 : sprintf(self::INFINITIVE_TPL, $verb['original'], $verb['tense']);
191 : }
192 :
193 : /**
194 : * Returns the conjugation tables
195 : *
196 : * @return array the conjugation tables
197 : */
198 : public function getConjugationTables()
199 : {
200 1 : $verbs = array();
201 :
202 1 : foreach($this->view->tcaf as $verb) {
203 : // extracts homonyms, ex "NOIIER (necare)" and "NOIIER (negare)"
204 1 : $homonym = $verb['original'];
205 1 : isset($verbs[$homonym]) or $verbs[$homonym] = $this->presetTenses();
206 :
207 1 : $verbs[$homonym][$verb['tense']] = array(
208 1 : 'tense' => $this->convertTense($verb['tense']),
209 1 : 'conjugation' => $this->replacePersons($verb['conjugation']),
210 : );
211 1 : }
212 :
213 1 : return array_map('array_filter', $verbs);
214 : }
215 :
216 : /**
217 : * Helper initialization
218 : */
219 : public function init()
220 : {
221 11 : $this->personNumbers = array_keys($this->person);
222 11 : $this->personTexts = array_values($this->person);
223 11 : }
224 :
225 : /**
226 : * Presets tenses
227 : *
228 : * @return array the tenses as keys with emtpy values
229 : */
230 : public function presetTenses()
231 : {
232 2 : return array_fill_keys(array_keys($this->tenses), null);
233 : }
234 :
235 : /**
236 : * Replaces person information
237 : *
238 : * @param array $conjugation the conjugation table
239 : * @return array the conjugation table
240 : */
241 : public function replacePersons($conjugation)
242 : {
243 2 : return str_replace($this->personNumbers, $this->personTexts, $conjugation);
244 : }
|