* @package tao */ class tao_helpers_translation_POUtils { // --- ASSOCIATIONS --- // --- ATTRIBUTES --- // --- OPERATIONS --- /** * Short description of method sanitize * * @access public * @author Jerome Bogaerts, * @param string string * @param boolean reverse * @return string */ public static function sanitize($string, $reverse = false) { $returnValue = (string) ''; if ($reverse) { $smap = ['"', "\n", "\t", "\r"]; $rmap = ['\\"', '\\n"' . "\n" . '"', '\\t', '\\r']; $returnValue = (string) str_replace($smap, $rmap, $string); } else { $smap = ['/"\s+"/', '/\\\\n/', '/\\\\r/', '/\\\\t/', '/\\\"/']; $rmap = ['', "\n", "\r", "\t", '"']; $returnValue = (string) preg_replace($smap, $rmap, $string); } return (string) $returnValue; } /** * Unserialize PO message comments. * * @access public * @author Jerome Bogaerts, * @param string annotations The PO message comments. * @return array */ public static function unserializeAnnotations($annotations) { $returnValue = []; $matches = []; $encoding = self::getApplicationHelper()->getDefaultEncoding(); if (preg_match_all('/(#[\.\:,\|]{0,1}\s+(?:[^\\n]*))/', $annotations, $matches) !== false) { if (isset($matches[1]) && count($matches[1]) > 0) { foreach ($matches[1] as $match) { $match = trim($match); $matchLen = mb_strlen($match, $encoding); $annotationId = null; $annotationValue = null; switch (mb_substr($match, 1, 1, $encoding)) { case "\t": case ' ': // Translator comment. $annotationId = tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS; $annotationValue = mb_substr($match, 2, $matchLen - 2, $encoding); break; case '.': $annotationId = tao_helpers_translation_POTranslationUnit::EXTRACTED_COMMENTS; $annotationValue = mb_substr($match, 3, $matchLen - 3, $encoding); break; case ':': $annotationId = tao_helpers_translation_POTranslationUnit::REFERENCE; $annotationValue = mb_substr($match, 3, $matchLen - 3, $encoding); break; case ',': $annotationId = tao_helpers_translation_POTranslationUnit::FLAGS; $annotationValue = mb_substr($match, 3, $matchLen - 3, $encoding); break; case '|': if (($pos = mb_strpos($match, 'msgid_plural', 0, $encoding)) !== false) { $pos += mb_strlen('msgid_plural', $encoding) + 1; $annotationId = tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGID_PLURAL; $annotationValue = mb_substr($match, $pos, $matchLen - $pos, $encoding); } elseif (($pos = mb_strpos($match, 'msgid', 0, $encoding)) !== false) { $pos += mb_strlen('msgid', $encoding) + 1; $annotationId = tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGID; $annotationValue = mb_substr($match, $pos, $matchLen - $pos, $encoding); } elseif (($pos = mb_strpos($match, 'msgctxt', 0, $encoding)) !== false) { $pos += mb_strlen('msgctxt', $encoding) + 1; $annotationId = tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGCTXT; $annotationValue = mb_substr($match, $pos, $matchLen - $pos, $encoding); } break; } if ($annotationId != null && $annotationValue != null) { if (!isset($returnValue[$annotationId])) { $returnValue[$annotationId] = $annotationValue; } else { $returnValue[$annotationId] .= "\n${annotationValue}"; } } } } } else { throw new tao_helpers_translation_TranslationException("An error occured while unserializing annotations '${annotations}'."); } return (array) $returnValue; } /** * Serialize an array of annotations in a PO compliant comments format. * * @access public * @author Jerome Bogaerts, * @param array annotations An array of annotations where keys are annotation identifiers and values are annotation values. * @return string */ public static function serializeAnnotations($annotations) { $returnValue = (string) ''; // Buffer will contain each line of the serialized PO comment block. $buffer = []; foreach ($annotations as $name => $value) { $prefix = null; switch ($name) { case tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS: $prefix = '#'; break; case tao_helpers_translation_POTranslationUnit::EXTRACTED_COMMENTS: $prefix = '#.'; break; case tao_helpers_translation_POTranslationUnit::REFERENCE: $prefix = '#:'; break; case tao_helpers_translation_POTranslationUnit::FLAGS: $prefix = '#,'; break; case tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGID: case tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGID_PLURAL: case tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGCTXT: $prefix = '#|'; break; } if ($prefix !== null) { // We have a PO compliant annotation that we have to serialize. foreach (explode("\n", $value) as $v) { $buffer[] = "${prefix} ${v}"; } } } // Glue the annotation lines in a single PO comment block. $returnValue = implode("\n", $buffer); return (string) $returnValue; } /** * Append a flag to an existing PO comment flag value. * * @access public * @author Jerome Bogaerts, * @param string comment A PO flag comment value in which you have to add the new flag. * @param string flag The flag to add to the existing $comment. * @return string */ public static function addFlag($comment, $flag) { $returnValue = (string) ''; $returnValue = $comment; $flag = trim($flag); $encoding = self::getApplicationHelper()->getDefaultEncoding(); if (mb_strpos($returnValue, $flag, 0, $encoding) === false) { $returnValue .= ((mb_strlen($returnValue, $encoding) > 0) ? " ${flag}" : $flag); } return (string) $returnValue; } // @TODO: Required to be able to mock tao extension constants in tests. // Remove after injecting ApplicationService as a dependency. private static function getApplicationHelper() { return ServiceManager::getServiceManager()->get(ApplicationService::SERVICE_ID); } }