/** * Définit les expressions à envelopper. * * @param string|array $mPhrasesToWrap Expressions * @throws \Exception */ protected function _setPhrases($mPhrasesToWrap) { if (is_string($mPhrasesToWrap)) { $this->_mPhrases = $mPhrasesToWrap; } else { if ($mPhrasesToWrap && is_array($mPhrasesToWrap)) { $this->_mPhrases = $mPhrasesToWrap; $this->_mPhrases = ArrayHelper::flatten($mPhrasesToWrap); } else { throw ExceptionType::invalidArgument("Argument #1 doit être une chaîne de caractères ou un tableau.", Exception::FROM_HANDLER); } } }
/** * Insère dans chaque texte de Wrap::_aTagTexts un prependice et/ou un appendice, si la position de ces derniers s'y trouve. * * @param array $aPhrasePos Tableau des positions des prependices et appendices de la forme :<br/> * Array([0] => Array(prependice => 2, appendice => 9), [1] => Array(prependice => 15, appendice => 23)) */ protected function _insertWrappers($aPhrasePos) { $aTextStats = $this->_textStats($this->_aTagTexts['texts']); // On applatit le tableau des positions. // Ainsi, les éléments pairs correspondent aux positions des prependices // et les éléments impairs aux appendices $aTagPositions = ArrayHelper::flatten($aPhrasePos); $aInsertions = array(); $iLastTextLength = 0; foreach ($aTextStats as $iTextIndex => $aTextStat) { foreach ($aTagPositions as $iKey => &$iPos) { if ($aTextStat[0] <= $iPos && $iPos <= $aTextStat[1]) { if (!isset($aInsertions[$iTextIndex])) { $aInsertions[$iTextIndex] = array(); } if (!isset($aInsertions[$iTextIndex][$iPos - $iLastTextLength])) { $aInsertions[$iTextIndex][$iPos - $iLastTextLength] = !($iKey % 2) ? $this->_sPrependice : $this->_sAppendice; } else { $aInsertions[$iTextIndex][$iPos - $iLastTextLength] .= !($iKey % 2) ? $this->_sPrependice : $this->_sAppendice; } unset($aTagPositions[$iKey]); } } $iLastTextLength = $aTextStat[1]; } foreach ($aInsertions as $iTextIndex => $aInsertion) { $aPos = array_keys($aInsertion); $aInsertion = array_values($aInsertion); for ($i = count($aPos) - 1; $i >= 0; $i--) { $this->_aTagTexts['texts'][$iTextIndex] = substr_replace($this->_aTagTexts['texts'][$iTextIndex], $aInsertion[$i], $aPos[$i], 0); } } }
protected function _getAnchorPos($aRegexPositions, $aAnchor) { $bNegativeAnchor = $aAnchor['offset'] < 0; $iAbsAnchorPos = abs($aAnchor['offset']); // Liste des index des élements de $aRegexPositions['regex_pos'] qui répondent à la regex $aElemRegexIndexes = $aRegexPositions['stats']['regex_pos']['matching_regex_indexes']; // Si la position recherchée est, dans l'absolu, supérieure au nombre d'éléments regex // alors c'est que l'ancre est extérieure à la chaîne if (count($aElemRegexIndexes) < $iAbsAnchorPos) { return null; } // Si la position recherchée est négative, la recherche commence à la fin du tableau if ($bNegativeAnchor) { $iAnchorIndex = array_reverse($aElemRegexIndexes)[$iAbsAnchorPos - 1]; } else { $iAnchorIndex = $aElemRegexIndexes[$iAbsAnchorPos - 1]; } // On renvoie les positions de début et de fin de l'ancre dans la chaîne de caractères. $aAnchorPositions = ArrayHelper::flatten($aRegexPositions['regex_pos'][$iAnchorIndex]['pos']['string']); return array($aAnchorPositions[0], end($aAnchorPositions)); }