56 lines
1.4 KiB
PHP
56 lines
1.4 KiB
PHP
<?php
|
|
|
|
abstract class CatalogManager {
|
|
|
|
public static $MAX_SUBSTITUTIONS = 9;
|
|
|
|
abstract protected function retrieveCatalog($index);
|
|
|
|
public function getCatalog($index,&$data=null) {
|
|
// if (is_array($index))var_dump($index);
|
|
$reval = "";
|
|
if(!is_null($data) && !is_array($data)) $data = array($data);
|
|
if($index != null && trim($index) != "") {
|
|
$reval = "" . $this->retrieveCatalog($index);
|
|
if($reval != null) {
|
|
if ($data!=null) $this->tokenize($reval,$data);
|
|
}
|
|
else $reval = $index;
|
|
}
|
|
return $reval;
|
|
}
|
|
|
|
|
|
/**
|
|
* Tokenizza la stringa di catalog
|
|
*/
|
|
protected function tokenize(&$catalog,&$data) {
|
|
$matches = array();
|
|
//implementation 3
|
|
if(preg_match_all("/\{([0-9]|c[0-9])\}/",$catalog,$matches)) {
|
|
$regas = $matches[1];
|
|
if(!is_array($regas)) $regas = array($regas);
|
|
// var_dump($regas);
|
|
foreach( $regas as $idx) {
|
|
if(preg_match("/^c[0-9]$/",$idx)) {
|
|
$useidx = (int) substr($idx,1);
|
|
$useidx--;
|
|
// echo "useidx: $useidx $idx\n";
|
|
$catalog = str_replace('{'. $idx . '}',$this->getCatalog((array_key_exists($useidx,$data)?$data[$useidx]:"")),$catalog);
|
|
}
|
|
else {
|
|
$useidx = (int) $idx;
|
|
$useidx--;
|
|
// echo "useidx: $useidx $idx\n";
|
|
$catalog = str_replace('{'. $idx . '}',(array_key_exists($useidx,$data)?$data[$useidx]:""),$catalog);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
?>
|