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