protected function _commonHandling() { try { $this->_oString->setLocale($this->_sLocale); } catch (\Exception $oException) { throw ExceptionType::runtime("Impossible de mettre à jour la locale à cause d'une exception lancée.", Exception::FROM_HANDLER, $oException); } return $this->_oString; }
/** * Convertit la chaîne de caractères dans l'encodage renseigné ou bien retourne le type d'encodage utilisé. * Attention : la modification de l'encodage n'est pas fiable à 100%. * * @todo gerer exception * @param string $sEncodeTo Si ce paramètre est absent, la méthode retourne le type d'encodage courant. Dans l'autre cas, il convertit la chaîne dans l'encodage renseigné. * @return Encoding * @throws Exception */ public function __construct($sEncodeTo) { // S'il y a des segments en cours de traitement $iProcessingPointsNb = Point::processingPointsCount(); if ($iProcessingPointsNb) { throw ExceptionType::runtime("Il n'est pas possible de convertir la sous-chaîne de caractères pointée par la méthode Aouka\\Text\\Manipulator::Point(). Il faut d'abord sortir de toutes les sous-chaînes (qui sont au nombre de {$iProcessingPointsNb}) via la méthode Aouka\\Text\\Manipulator::EndFocus()", Exception::FROM_HANDLER); } if (!is_string($sEncodeTo)) { throw ExceptionType::invalidArgument("Argument #1 doit être une chaîne de caractères ou NULL.", Exception::FROM_HANDLER); } $sEncodingDestination = Encoding::isValidEncoding($sEncodeTo); if (!$sEncodingDestination) { throw ExceptionType::domain("Argument #1 n'est pas un encodage valide. Les encodages supportés sont décrits sur http://www.php.net/manual/fr/function.mb-list-encodings.php.", Exception::FROM_HANDLER); } $this->_sEncodingDestination = $sEncodingDestination; }
/** * Mémorise l'état de la chaîne de caractères après exécution de toutes les méthodes invoquées précédemment. * Toutes les méthodes appelées par la suite seront exécutées à partir de cet état. L'intérêt est de limiter l'exécution systématique de méthodes gourmandes. * Attention : cette méthode ne peut pas être appelée quand le mode debug est activé. * * @return \Aouka\Text\Manipulator * @throws \Exception */ public function memorize() { if ($this->_bDebug) { throw ExceptionType::runtime("Impossible de mémoriser l'état de la chaîne de caractères quand le mode debug est activé.", Exception::FROM_MANIPULATOR); } $sResult = $this->get(); $this->_removeHandlers(); $this->_oString->setString($sResult); return $this; }
protected function _treatResponse($sJson) { // decode the response data $aResponse = json_decode($sJson, true); // ensure the returned data is valid if (!is_array($aResponse) || !isset($aResponse['data'])) { throw ExceptionType::outOfBounds("Impossible de récupérer la réponse de l'API.", Exception::FROM_INTERPRETOR); } $sDataType = $this->_bDetectLanguage ? 'detections' : 'translations'; // ensure the returned data is valid if (!isset($aResponse['data'][$sDataType])) { throw ExceptionType::outOfBounds("Impossible de trouver la clé {$sDataType} dans le tableau retourné par l'API.", Exception::FROM_INTERPRETOR); } if (!is_array($aResponse['data'][$sDataType])) { throw ExceptionType::unexpectedValue("La valeur retournée par l'API n'est pas un tableau, comme attendu.", Exception::FROM_INTERPRETOR); } if ($sDataType === 'translations') { // loop over the translations and return the first one. // if you wanted to handle multiple translations in a single call // you would need to modify how this returns data foreach ($aResponse['data'][$sDataType] as $aTranslation) { return $aTranslation['translatedText']; } } else { if (!is_array($aResponse['data']['detections'][0][0])) { throw ExceptionType::unexpectedValue("La valeur des détections retournée par l'API n'est pas un tableau, comme attendu.", Exception::FROM_INTERPRETOR); } else { if (!isset($aResponse['data']['detections'][0][0]['language'])) { throw ExceptionType::runtime("Aucune langue n'a pu être détectée.", Exception::FROM_INTERPRETOR); } } return $aResponse['data']['detections'][0][0]['language']; } // assume failure since success would've returned just above throw ExceptionType::runtime("L'appel à l'API a échoué.", Exception::FROM_INTERPRETOR); }