protected function _formatPattern() { // Si on reçoit une chaîne de caractères, on la considère comme un pattern valide if (is_string($this->_mPattern)) { return; } // Si le pattern est un entier, c'est qu'il s'agit d'une constante // on construit alors sa regex if (is_int($this->_mPattern)) { $this->_mPattern = Regex::create($this->_mPattern); } // Si le pattern est un objet \Aouka\Regex, on construit l'expression sous sa forme littérale if ($this->_mPattern instanceof Regex) { $this->_mPattern = '`(' . $this->_mPattern->getExpression() . ')`' . $this->_m(); return; } throw ExceptionType::unexpectedValue("Le pattern fourni n'est pas correct.", Exception::FROM_HANDLER); }
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); }
/** * Détecte automatiquement l'encodage de la chaîne de caractères passé en paramètre. * * Attention : la détection automatique peut parfois échouer. * Si une chaîne ASCII est passée en paramètre, la fonction l'interprétera comme de l'UTF-8. * * @param string $sInputString Chaîne de caractères. * @return string Retourne le nom de l'encodage détecté. * @throws Aouka\Text\Exception\RuntimeException\UnexpectedValueException Si la détection échoue. */ public static function detectEncoding($sInputString) { // On détecte l'encodage $sInputStringEncoding = mb_detect_encoding($sInputString); // La détection de l'encodage peut parfois échouer // Si l'encodage réel de la chaîne ne correspond pas à l'encodage détecté automatiquement if (!mb_check_encoding($sInputString, $sInputStringEncoding)) { $bFailure = true; // On fait des tentatives avec les deux encodages les plus courants foreach (array(self::UTF_8, self::ISO_8859_1) as $sEncoding) { if (mb_check_encoding($sInputString, $sEncoding)) { $sInputStringEncoding = $sEncoding; $bFailure = false; break; } } if ($bFailure) { throw ExceptionType::unexpectedValue("La détection automatique de l'encodage de la chaîne \"{$sInputString}\" a échoué.", Exception::FROM_ENCODING); } } // L'encodage ASCII ne gère pas les caractères spéciaux (accent, cédille, ...). // Si cet encodage est détecté, on déclare l'encodage comme étant de l'UTF-8. // L'intérêt est que l'UTF-8 est compatible avec ASCII et gère les caractères spéciaux. return $sInputStringEncoding !== self::ASCII ? $sInputStringEncoding : self::UTF_8; }