Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 109 |
|
0.00% |
0 / 23 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_translation_TranslationFile | |
0.00% |
0 / 109 |
|
0.00% |
0 / 23 |
2070 | |
0.00% |
0 / 1 |
| setAnnotations | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAnnotations | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| addAnnotation | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| removeAnnotation | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| getAnnotation | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getSourceLanguage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getTargetLanguage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getTranslationUnits | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| setSourceLanguage | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setTargetLanguage | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setTranslationUnits | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addTranslationUnit | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| removeTranslationUnit | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| __toString | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| addTranslationUnits | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| hasSameSource | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| hasSameTarget | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| sortBySource | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
42 | |||
| sortByTarget | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getBySource | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getByTarget | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| count | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * A translation file represents the translation of a file, software, item, ... |
| 27 | * contains a list of Translation Units a source language and a target language. |
| 28 | * File can be read and written by TranslationFileReader & TranslationFileWriter |
| 29 | * |
| 30 | * @access public |
| 31 | * @author Jerome Bogaerts |
| 32 | * @package tao |
| 33 | * @see tao_model_classes_TranslationUnit tao_model_classes_TranslationFileReader |
| 34 | * tao_model_classes_TranslationFileWriter |
| 35 | * @since 2.2 |
| 36 | * @version 1.0 |
| 37 | */ |
| 38 | class tao_helpers_translation_TranslationFile implements tao_helpers_translation_Annotable |
| 39 | { |
| 40 | /** |
| 41 | * Short description of attribute sourceLanguage |
| 42 | * |
| 43 | * @access private |
| 44 | * @var string |
| 45 | */ |
| 46 | private $sourceLanguage = ''; |
| 47 | |
| 48 | /** |
| 49 | * Short description of attribute targetLanguage |
| 50 | * |
| 51 | * @access private |
| 52 | * @var string |
| 53 | */ |
| 54 | private $targetLanguage = ''; |
| 55 | |
| 56 | /** |
| 57 | * Short description of attribute translationUnits |
| 58 | * |
| 59 | * @access private |
| 60 | * @var array |
| 61 | */ |
| 62 | private $translationUnits = []; |
| 63 | |
| 64 | /** |
| 65 | * Ascending sort case-sensitive |
| 66 | * |
| 67 | * @access public |
| 68 | * @var int |
| 69 | */ |
| 70 | public const SORT_ASC = 1; |
| 71 | |
| 72 | /** |
| 73 | * Descending sort case-sensitive |
| 74 | * |
| 75 | * @access public |
| 76 | * @var int |
| 77 | */ |
| 78 | public const SORT_DESC = 2; |
| 79 | |
| 80 | /** |
| 81 | * Ascending sort case-insensitive |
| 82 | * |
| 83 | * @access public |
| 84 | * @var int |
| 85 | */ |
| 86 | public const SORT_ASC_I = 3; |
| 87 | |
| 88 | /** |
| 89 | * Descending sort case-insensitive. |
| 90 | * |
| 91 | * @access public |
| 92 | * @var int |
| 93 | */ |
| 94 | public const SORT_DESC_I = 4; |
| 95 | |
| 96 | /** |
| 97 | * The annotations bound to this translation file. |
| 98 | * |
| 99 | * @access private |
| 100 | * @var array |
| 101 | */ |
| 102 | private $annotations = []; |
| 103 | |
| 104 | /** |
| 105 | * Sets the collection of annotations bound to this Translation Object. |
| 106 | * |
| 107 | * @access public |
| 108 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 109 | * @param array $annotations An associative array of annotations where keys are the annotation names and values are |
| 110 | * annotation values. |
| 111 | * @return void |
| 112 | */ |
| 113 | public function setAnnotations($annotations) |
| 114 | { |
| 115 | $this->annotations = $annotations; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns an associative array that represents a collection of annotations |
| 120 | * keys are annotation names and values annotation values. |
| 121 | * |
| 122 | * @access public |
| 123 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 124 | * @return array |
| 125 | */ |
| 126 | public function getAnnotations() |
| 127 | { |
| 128 | $returnValue = []; |
| 129 | |
| 130 | $returnValue = $this->annotations; |
| 131 | |
| 132 | return (array) $returnValue; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Adds an annotation with a given name and value. |
| 137 | * If value is not provided, |
| 138 | * annotation will be taken into account as a flag. |
| 139 | * |
| 140 | * @access public |
| 141 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 142 | * @param string name The name of the annotation to add. |
| 143 | * @param string value The value of the annotation to add. |
| 144 | * @return void |
| 145 | */ |
| 146 | public function addAnnotation($name, $value = '') |
| 147 | { |
| 148 | $this->annotations[$name] = $value; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Removes an annotation for a given annotation name. |
| 153 | * |
| 154 | * @access public |
| 155 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 156 | * @param string name The name of the annotation to remove. |
| 157 | * @return void |
| 158 | */ |
| 159 | public function removeAnnotation($name) |
| 160 | { |
| 161 | if (isset($this->annotations[$name])) { |
| 162 | unset($this->annotations[$name]); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Get an annotation for a given annotation name. |
| 168 | * Returns an associative |
| 169 | * where keys are 'name' and 'value'. |
| 170 | * |
| 171 | * @access public |
| 172 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 173 | * @param string name |
| 174 | * @return array |
| 175 | */ |
| 176 | public function getAnnotation($name) |
| 177 | { |
| 178 | $returnValue = []; |
| 179 | |
| 180 | if (isset($this->annotations[$name])) { |
| 181 | $returnValue = [ |
| 182 | 'name' => $name, |
| 183 | 'value' => $this->annotations[$name] |
| 184 | ]; |
| 185 | } else { |
| 186 | $returnValue = null; |
| 187 | } |
| 188 | |
| 189 | return (array) $returnValue; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Creates a new instance of TranslationFile for a specific source and |
| 194 | * language. |
| 195 | * |
| 196 | * @access public |
| 197 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 198 | * @return mixed |
| 199 | */ |
| 200 | public function __construct() |
| 201 | { |
| 202 | $this->setSourceLanguage(tao_helpers_translation_Utils::getDefaultLanguage()); |
| 203 | $this->setTargetLanguage(tao_helpers_translation_Utils::getDefaultLanguage()); |
| 204 | $this->setTranslationUnits([]); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Gets the source language. |
| 209 | * |
| 210 | * @access public |
| 211 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 212 | * @return string |
| 213 | */ |
| 214 | public function getSourceLanguage() |
| 215 | { |
| 216 | $returnValue = (string) ''; |
| 217 | |
| 218 | return $this->sourceLanguage; |
| 219 | |
| 220 | return (string) $returnValue; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Gets the target language. |
| 225 | * |
| 226 | * @access public |
| 227 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 228 | * @return string |
| 229 | */ |
| 230 | public function getTargetLanguage() |
| 231 | { |
| 232 | $returnValue = (string) ''; |
| 233 | |
| 234 | return $this->targetLanguage; |
| 235 | |
| 236 | return (string) $returnValue; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Gets the collection of Translation Units representing the |
| 241 | * |
| 242 | * @access public |
| 243 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 244 | * @return array |
| 245 | */ |
| 246 | public function getTranslationUnits() |
| 247 | { |
| 248 | $returnValue = []; |
| 249 | |
| 250 | return $this->translationUnits; |
| 251 | |
| 252 | return (array) $returnValue; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Sets the source language. |
| 257 | * |
| 258 | * @access public |
| 259 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 260 | * @param string sourceLanguage |
| 261 | * @return mixed |
| 262 | */ |
| 263 | public function setSourceLanguage($sourceLanguage) |
| 264 | { |
| 265 | $this->sourceLanguage = $sourceLanguage; |
| 266 | $this->addAnnotation('sourceLanguage', $sourceLanguage); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Sets the target language. |
| 271 | * |
| 272 | * @access public |
| 273 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 274 | * @param |
| 275 | * string targetLanguage |
| 276 | * @return mixed |
| 277 | */ |
| 278 | public function setTargetLanguage($targetLanguage) |
| 279 | { |
| 280 | $this->targetLanguage = $targetLanguage; |
| 281 | $this->addAnnotation('targetLanguage', $targetLanguage); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Sets the collection of TranslationUnits representing the file. |
| 286 | * |
| 287 | * @access public |
| 288 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 289 | * @param array translationUnits |
| 290 | * @return mixed |
| 291 | */ |
| 292 | public function setTranslationUnits($translationUnits) |
| 293 | { |
| 294 | $this->translationUnits = $translationUnits; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Adds a TranslationUnit instance to the file. |
| 299 | * It is appenned at the end of |
| 300 | * collection. |
| 301 | * |
| 302 | * @access public |
| 303 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 304 | * @param TranslationUnit translationUnit |
| 305 | * @return mixed |
| 306 | */ |
| 307 | public function addTranslationUnit(tao_helpers_translation_TranslationUnit $translationUnit) |
| 308 | { |
| 309 | |
| 310 | // If the translation unit exists, we replace the target with the new one if it exists. |
| 311 | foreach ($this->getTranslationUnits() as $tu) { |
| 312 | if ($tu->getSource() == $translationUnit->getSource()) { |
| 313 | // If we are here, it means that this TU is being overriden by |
| 314 | // another one having the same source... |
| 315 | // |
| 316 | // Let's make sure we don't override the existing one with an empty target! |
| 317 | if ($translationUnit->getTarget() !== '') { |
| 318 | $tu->setTarget($translationUnit->getTarget()); |
| 319 | $tu->setAnnotations($translationUnit->getAnnotations()); |
| 320 | } |
| 321 | |
| 322 | return; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | // If we are here, it means that this TU does not exist. |
| 327 | $translationUnit->setSourceLanguage($this->getSourceLanguage()); |
| 328 | $translationUnit->setTargetLanguage($this->getTargetLanguage()); |
| 329 | array_push($this->translationUnits, $translationUnit); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Removes a given TranslationUnit from the collection of TranslationUnits |
| 334 | * the file. |
| 335 | * |
| 336 | * @access public |
| 337 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 338 | * @param TranslationUnit translationUnit |
| 339 | * @return mixed |
| 340 | */ |
| 341 | public function removeTranslationUnit(tao_helpers_translation_TranslationUnit $translationUnit) |
| 342 | { |
| 343 | $tus = $this->getTranslationUnits(); |
| 344 | for ($i = 0; $i < count($tus); $i++) { |
| 345 | if ($tus[$i] === $translationUnit) { |
| 346 | unset($tus[$i]); |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | throw new tao_helpers_translation_TranslationException('Cannot remove Translation Unit. Not Found.'); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Short description of method __toString |
| 356 | * |
| 357 | * @access public |
| 358 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 359 | * @return string |
| 360 | */ |
| 361 | public function __toString() |
| 362 | { |
| 363 | $returnValue = (string) ''; |
| 364 | |
| 365 | $returnValue = $this->getSourceLanguage() . '->' . $this->getTargetLanguage() . ':'; |
| 366 | foreach ($this->getTranslationUnits() as $tu) { |
| 367 | $returnValue .= $tu; |
| 368 | } |
| 369 | |
| 370 | return (string) $returnValue; |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Adds a set of TranslationUnits to the existing set of TranslationUnits |
| 375 | * in the TranslationFile. |
| 376 | * No duplicate entries will be made based on the |
| 377 | * of the translation units. |
| 378 | * |
| 379 | * @access public |
| 380 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 381 | * @param array translationUnits |
| 382 | * @return mixed |
| 383 | */ |
| 384 | public function addTranslationUnits($translationUnits) |
| 385 | { |
| 386 | foreach ($translationUnits as $tu) { |
| 387 | $this->addTranslationUnit($tu); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Short description of method hasSameSource |
| 393 | * |
| 394 | * @access public |
| 395 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 396 | * @param TranslationUnit translationUnit |
| 397 | * @return boolean |
| 398 | */ |
| 399 | public function hasSameSource(tao_helpers_translation_TranslationUnit $translationUnit) |
| 400 | { |
| 401 | $returnValue = (bool) false; |
| 402 | |
| 403 | foreach ($this->getTranslationUnits() as $tu) { |
| 404 | if ($tu->hasSameTranslationUnitSource($translationUnit)) { |
| 405 | $returnValue = true; |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | return (bool) $returnValue; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Short description of method hasSameTarget |
| 415 | * |
| 416 | * @access public |
| 417 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 418 | * @param TranslationUnit translationUnit |
| 419 | * @return boolean |
| 420 | */ |
| 421 | public function hasSameTarget(tao_helpers_translation_TranslationUnit $translationUnit) |
| 422 | { |
| 423 | $returnValue = (bool) false; |
| 424 | |
| 425 | foreach ($this->getTranslationUnits() as $tu) { |
| 426 | if ($tu->hasSameTranslationUnitTarget($translationUnit)) { |
| 427 | $returnValue = true; |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | return (bool) $returnValue; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Sorts and returns the TranslationUnits by Source with a specified sorting |
| 437 | * |
| 438 | * @access public |
| 439 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 440 | * @param int sortingType |
| 441 | * @return array |
| 442 | */ |
| 443 | public function sortBySource($sortingType) |
| 444 | { |
| 445 | $returnValue = $this->getTranslationUnits(); |
| 446 | switch ($sortingType) { |
| 447 | case self::SORT_ASC: |
| 448 | $cmpFunction = function ($a, $b) { |
| 449 | return strcmp($a->getSource(), $b->getSource()); |
| 450 | }; |
| 451 | break; |
| 452 | case self::SORT_ASC_I: |
| 453 | $cmpFunction = function ($a, $b) { |
| 454 | return strcmp(mb_strtolower($a->getSource(), "UTF-8"), mb_strtolower($b->getSource(), "UTF-8")); |
| 455 | }; |
| 456 | break; |
| 457 | case self::SORT_DESC: |
| 458 | $cmpFunction = function ($a, $b) { |
| 459 | return - 1 * strcmp($a->getSource(), $b->getSource()); |
| 460 | }; |
| 461 | break; |
| 462 | case self::SORT_DESC_I: |
| 463 | $cmpFunction = function ($a, $b) { |
| 464 | return - 1 * strcmp( |
| 465 | mb_strtolower($a->getSource(), "UTF-8"), |
| 466 | mb_strtolower($b->getSource(), "UTF-8") |
| 467 | ); |
| 468 | }; |
| 469 | break; |
| 470 | default: |
| 471 | throw new common_Exception('Unknown sortingType ' . $sortingType); |
| 472 | } |
| 473 | usort($returnValue, $cmpFunction); |
| 474 | return $returnValue; |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Sorts and returns the TranslationUnits by Target with a specified sorting |
| 479 | * |
| 480 | * @access public |
| 481 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 482 | * @param int sortingType |
| 483 | * @return array |
| 484 | */ |
| 485 | public function sortByTarget($sortingType) |
| 486 | { |
| 487 | $returnValue = []; |
| 488 | |
| 489 | throw new tao_helpers_translation_TranslationException("Not yet implemtented."); |
| 490 | |
| 491 | return (array) $returnValue; |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Short description of method getBySource |
| 496 | * |
| 497 | * @access public |
| 498 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 499 | * @param TranslationUnit translationUnit |
| 500 | * @return tao_helpers_translation_TranslationUnit |
| 501 | */ |
| 502 | public function getBySource(tao_helpers_translation_TranslationUnit $translationUnit) |
| 503 | { |
| 504 | $returnValue = null; |
| 505 | |
| 506 | foreach ($this->getTranslationUnits() as $tu) { |
| 507 | if ($tu->hasSameTranslationUnitSource($translationUnit)) { |
| 508 | $returnValue = $tu; |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | return $returnValue; |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * Short description of method getByTarget |
| 518 | * |
| 519 | * @access public |
| 520 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 521 | * @param TranslationUnit translationUnit |
| 522 | * @return tao_helpers_translation_TranslationUnit |
| 523 | */ |
| 524 | public function getByTarget(tao_helpers_translation_TranslationUnit $translationUnit) |
| 525 | { |
| 526 | $returnValue = null; |
| 527 | |
| 528 | foreach ($this->getTranslationUnits() as $tu) { |
| 529 | if ($tu->hasSameTranslationUnitTarget($translationUnit)) { |
| 530 | $returnValue = $tu; |
| 531 | break; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | return $returnValue; |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Counts the TranslationUnits within the TranslationFile. |
| 540 | * |
| 541 | * @access public |
| 542 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 543 | * @return int |
| 544 | */ |
| 545 | public function count() |
| 546 | { |
| 547 | return count($this->getTranslationUnits()); |
| 548 | } |
| 549 | } |