sMaskName = ''; $this->sFilePath = ''; $this->sMask = ''; $this->asTags = array(); $this->asPartsSource = array(); $this->aoInstances = array(); $this->sFilePath = ''; $this->oLang = $oLang; //load file if($sFileName!='') $this->initFile($sFileName); } public static function getMaskFile($sFileName) { return self::MASK_FOLDER.$sFileName.self::MASK_EXT; } public function initFile($sFileName) { $sFilePath = self::getMaskFile(basename($sFileName)); if(file_exists($sFilePath)) { $this->sFilePath = $sFilePath; $sSource = file_get_contents($this->sFilePath); $this->initMask($sFileName, $sSource); } else { $this->addError('Fichier introuvable à l\'adresse : '.$sFilePath); } } public function initFileFromString($sSource, $sPartName='', $iInstanceNb=0) { $this->initMask($sPartName.' (from row) '.$iInstanceNb, $sSource); } private function initMask($sMaskName, $sSource) { $this->sMaskName = $sMaskName; $this->sMask = $sSource; $this->setParts(); $this->setLangTags(); } private function setParts() { while(preg_match('/\<\!-- \[PART\] (?P\S+) \[START\] --\>/u', $this->sMask, $asMatch)) { $sPartName = $asMatch['part']; $this->asPartsSource[$sPartName] = $this->getCleanPart($sPartName); $this->aoInstances[$sPartName] = array(); } } private function getCleanPart($sPartName) { $iStartPos = $this->getPartStartPos($sPartName); $iEndPos = $this->getPartEndPos($sPartName); $sPart = mb_substr($this->sMask, $iStartPos, $iEndPos-$iStartPos); $sExtendedPart = $this->getPartPattern($sPartName, self::START_TAG).$sPart. $this->getPartPattern($sPartName, self::END_TAG); $this->sMask = str_replace($sExtendedPart, $this->getPartTagPattern($sPartName), $this->sMask); return $sPart; } private function getPartStartPos($sPartName) { $sPartStartPattern = $this->getPartPattern($sPartName, self::START_TAG); return mb_strpos($this->sMask, $sPartStartPattern) + strlen($sPartStartPattern); } private function getPartEndPos($sPartName) { $sPartEndPattern = $this->getPartPattern($sPartName, self::END_TAG); return mb_strpos($this->sMask, $sPartEndPattern); } private function getPartPattern($sPartName, $sAction) { return ''; } private function getPartTagPattern($sPartName, $bMark=true) { $sPartTag = 'PART '.$sPartName; return $bMark?$this->addTagMark($sPartTag):$sPartTag; } public function addInstance($sPartName, $asTags) { $this->newInstance($sPartName); foreach($asTags as $sTagName=>$sTagValue) { $this->setInstanceTag($sPartName, $sTagName, $sTagValue); } } public function newInstance($sPartName) { //Finding the part ($oMask is a pointer) $oMask = $this->findPart($this, $sPartName); if(!$oMask) $this->addError('No part found : '.$sPartName); //Retrieving source html $sPartSource = $oMask->asPartsSource[$sPartName]; //Creating new instance $oInstance = new Mask('', $this->oLang); $oInstance->initFileFromString($sPartSource, $sPartName); $oMask->aoInstances[$sPartName][] = $oInstance; } public function setInstanceTag($sPartName, $sTagName, $sTagValue) { $oMask = $this->findPart($this, $sPartName); if(!$oMask) $this->addError('No part found : '.$sPartName); $oMask->getCurrentInstance($sPartName)->setTag($sTagName, $sTagValue); } private function findPart($oMask, $sPartName) { if(array_key_exists($sPartName, $oMask->aoInstances)) { return $oMask; } else //going deeper to find the part { foreach($oMask->aoInstances as $sLevelPartName=>$aoInstances) { if(!empty($aoInstances)) { //take last instances $oTmpMask = $this->findPart($oMask->getCurrentInstance($sLevelPartName), $sPartName); if($oTmpMask) return $oTmpMask; } } } return false; } private function getCurrentInstance($sPartName) { if(!empty($this->aoInstances[$sPartName])) { return end($this->aoInstances[$sPartName]); } else { return false; } } public function getTags($bLang=false) { $sSafeTagMark = preg_quote(self::TAG_MARK); $sSafeLangMark = preg_quote(self::LANG_PREFIX); $sPattern = '/'.$sSafeTagMark.'(?P('.$sSafeLangMark.'|)\w+)'.$sSafeTagMark.'/u'; preg_match_all($sPattern, $this->sMask, $asMatches); return array_unique(array_filter($asMatches['tag'])); } public function setTag($sTagName, $sTagValue) { //Check if tagged should be translated if(self::isLangTag($sTagValue)) { if(is_null($this->oLang)) $this->addError('Missing Lang Class. Please provide in constructor'); else $sTagValue = $this->oLang->getTranslation(mb_substr($sTagValue, 5)); } $this->asTags[$sTagName] = $sTagValue; } public function setTags($asTags) { foreach($asTags as $sTagName=>$sTagValue) $this->setTag($sTagName, $sTagValue); } /** * Default value for tags tagged as translation: Starting with lang/ */ private function setLangTags() { if($this->oLang != null) { $asTags = $this->getTags(); foreach($asTags as $sTagName) { if(self::isLangTag($sTagName)) $this->setTag($sTagName, $this->oLang->getTranslation(mb_substr($sTagName, 5))); } } } private static function isLangTag($sTag) { return (mb_substr($sTag, 0, 5) == self::LANG_PREFIX); } public function getMask() { $sCompletedMask = $this->sMask; //build parts foreach($this->aoInstances as $sPart=>$aoParts) { $sTagValue = ''; foreach($aoParts as $oInstance) { $sTagValue .= $oInstance->getMask(); } $this->setTag($this->getPartTagPattern($sPart, false), $sTagValue); } //replace tags if(!empty($this->asTags)) { $asTags = $this->addTagMark(array_keys($this->asTags)); $sCompletedMask = str_replace($asTags, $this->asTags, $sCompletedMask); } return $sCompletedMask; } private function addTagMark($oData) { return ToolBox::array_map_encapsulate($oData, self::TAG_MARK); } } ?>