1 : <?php
2 : /**
3 : * Roman de Renart
4 : *
5 : * PHP version 5
6 : *
7 : * @category Rdr
8 : * @package Edit
9 : * @author Michel Corne <mcorne@yahoo.com>
10 : * @copyright 2010 Michel Corne
11 : * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
12 : * @link http://roman-de-renart.blogspot.com/
13 : * @version SVN: $Id$
14 : */
15 :
16 : require_once 'Episode.php';
17 :
18 : /**
19 : * Completing the questions on the translation of an episode
20 : *
21 : * @category Rdr
22 : * @package Edit
23 : * @author Michel Corne <mcorne@yahoo.com>
24 : * @copyright 2010 Michel Corne
25 : * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
26 : */
27 :
28 : class Questions extends Episode
29 : {
30 : /**
31 : * The error message reported when the old French piece of the question is missing
32 : */
33 : const ERR_NO_FRO = 'missing old French piece for question';
34 :
35 : /**
36 : * The message reported when there are no questions
37 : */
38 : const MSG_NO_QUESTIONS = 'There are no questions.';
39 :
40 : /**
41 : * The error message reported when the translation piece of the question is missing
42 : */
43 : const ERR_NO_TRANSLATION = 'missing translation piece for question';
44 :
45 : /**
46 : * The error message reported when the question cannot be parsed
47 : */
48 : const ERR_PARSE_QUESTION = 'cannot parse question';
49 :
50 : /**
51 : * The format of a question
52 : */
53 : const QUESTION_FMT = '%s : « %s » = « %s » ?';
54 :
55 : /**
56 : * The old format of a question
57 : */
58 : const QUESTION_FMT2 = '%s : "%s" = "%s" ?'; // TEMPORARY !!!
59 :
60 :
61 : /**
62 : * The keys of the columns used for questions
63 : * @var array
64 : */
65 : public static $questionKeys = array(
66 : Episode::COL_QUESTIONS_TEXT,
67 : Episode::COL_FRO_NUMBERS,
68 : Episode::COL_QUESTIONS_FRO,
69 : Episode::COL_QUESTIONS_TRANS,
70 : );
71 :
72 : /**
73 : * Creates the questions of an episode
74 : *
75 : * @return string the list of questions or a message if there is no questions
76 : */
77 : public function makeQuestions()
78 : {
79 : // reads the rows
80 1 : $this->readSheet();
81 1 : $rows = $this->readRows();
82 :
83 : // creates the questions
84 1 : $questions = $this->arrayMap('questionToString', $rows);
85 :
86 : // writes the questions in another column
87 1 : $this->writeColumn($questions, Episode::COL_QUESTIONS_TEXT);
88 1 : $this->writeSheet();
89 :
90 : // reports the questions
91 1 : $result = $this->questionsToString($questions) or $result = self::MSG_NO_QUESTIONS;
92 :
93 1 : return $result;
94 : }
95 :
96 : /**
97 : * Converts a question to string
98 : *
99 : * @param array $row the row containg the question details
100 : * @return string a string with the question
101 : */
102 : public function questionToString($row)
103 : {
104 :
105 2 : if ($row[Episode::COL_QUESTIONS_FRO] and $row[Episode::COL_QUESTIONS_TRANS]) {
106 : // both old French and translation pieces are present
107 2 : $question = sprintf(self::QUESTION_FMT,
108 2 : $row[Episode::COL_FRO_NUMBERS],
109 2 : $row[Episode::COL_QUESTIONS_FRO],
110 2 : $row[Episode::COL_QUESTIONS_TRANS]);
111 :
112 2 : } else if (!$row[Episode::COL_QUESTIONS_FRO] and $row[Episode::COL_QUESTIONS_TRANS]) {
113 : // the old French piece is missing
114 1 : $this->abort(self::ERR_NO_FRO);
115 :
116 2 : } else if ($row[Episode::COL_QUESTIONS_FRO] and !$row[Episode::COL_QUESTIONS_TRANS]) {
117 : // the translation piece is missing
118 1 : $this->abort(self::ERR_NO_TRANSLATION);
119 :
120 2 : } else if (true) { // "if (true)" added to pass 100% test code coverage: PHPUnit bug?
121 : // there is no question
122 2 : $question = '';
123 2 : }
124 :
125 2 : return $question;
126 : }
127 :
128 : /**
129 : * Converts the questions into a string for reporting
130 : *
131 : * @param array $questions the array of questions
132 : * @return string a string with the questions
133 : */
134 : public function questionsToString($questions)
135 : {
136 2 : $questions = $this->arrayToString($questions);
137 :
138 2 : return preg_replace('~([:=]) ~', "$1\n", $questions);
139 : }
140 :
141 : /**
142 : * Parses a question
143 : *
144 : * @param string $question the question to parse
145 : * @return mixed the question details
146 : */
147 : public static function toCsv($question)
148 : {
149 3 : $base = new Base;
150 :
151 3 : $pattern = $base->completePattern(self::QUESTION_FMT);
152 : // $pattern = $base->completePattern(self::QUESTION_FMT2); // TEMPORARY !!!
153 3 : $cells = $base->match($pattern, $question, self::ERR_PARSE_QUESTION, Questions::$questionKeys, true);
154 :
155 3 : return array_map('trim', $cells);
156 : }
|