diff --git a/src/includes/functions.php b/src/includes/functions.php index 09574e54..abd32e12 100644 --- a/src/includes/functions.php +++ b/src/includes/functions.php @@ -20,108 +20,119 @@ ******************************************************************************/ -/** - * Construction d'une chaine formatée +/* + * LSFormat : %{[key name][:A][:B][-][! ou _][~][%}} * - * Cette fonction retourne la valeur d'une chaine formatée selon le format - * et les données passés en paramètre. + * Extracted fields + * - 0 : full string '%{...}' + * - 1/key : key name + * - 2 : :A + * - 3/substr_a : A + * - 4 : :B + * - 5/substr_b : B + * - 6/modifiers : "!" / "_" / "~" / "%" + */ +define( + 'GETFDATA_REGEX', + "/%[{(](?P[A-Za-z0-9]+)(\:(?P-?[0-9])+)?(\:(?P[0-9]+))?(?P[\!\_~%]*)[})]/" +); + +/** + * Build LSformat string + * + * This function build and return a LSformat string from specified format and provided data. * * @author Benjamin Renard * - * @param string $format Format de la chaine - * @param string $format Les données pour composés la chaine - * Ce paramètre peut être un tableau de string, une string, - * une tableau d'objet ou un objet. - * @param string $format Le nom de la methode de/des objet(s) à appeler pour - * obtenir la valeur de remplacement dans la chaine formatée. + * @param string $format The LSformat string + * @param string|object|array $data Data to build the string + * This parameter could be a string, an object or an array of string or object. + * @param string $meth The name to the method of objet(s) to call to get the replacement value for a + * specified key * - * @return string La chaine formatée + * @return string The builded LSformat string */ -function getFData($format,$data,$meth=NULL) { - $unique=false; - /* - * Format : %{[key name][:A][:B][! ou _][~][%}} - * - * Extracted fields - * - 0 : full string '%{...}' - * - 1 : key name - * - 2 : :A - * - 3 : A - * - 4 : :B - * - 5 : B - * - 6 : "!" / "_" / "~" / "%" - */ - $expr="/%[{(]([A-Za-z0-9]+)(\:(-?[0-9])+)?(\:(-?[0-9]+))?([\!\_~%]*)[})]/"; +function getFData($format, $data, $meth=NULL) { + $unique = false; if(!is_array($format)) { - $format=array($format); - $unique=true; + $format = array($format); + $unique = true; } - for($i=0;$i $meth(); + while (preg_match(GETFDATA_REGEX, $format[$i], $ch)) { + if (method_exists($data[$ch['key']], $meth)) { + $value = $data[$ch['key']] -> $meth(); if (is_array($value)) { $value = $value[0]; } - $value=_getFData_extractAndModify($value,$ch); - $format[$i]=str_replace($ch[0],$value,$format[$i]); + $value = _getFData_extractAndModify($value, $ch); + $format[$i] = str_replace($ch[0], $value, $format[$i]); } else { - LSerror :: addErrorCode('fct_getFData_01',array('meth' => $meth,'obj' => $ch[1])); + LSerror :: addErrorCode( + 'fct_getFData_01', + array('meth' => $meth, 'obj' => $ch['key']) + ); break; } } } } elseif (is_object($data)) { - if ($meth==NULL) { - while (preg_match($expr,$format[$i],$ch)) { - $value = $data -> $ch[1]; + if (is_null($meth)) { + while (preg_match(GETFDATA_REGEX, $format[$i], $ch)) { + $value = $data -> $ch['key']; if (is_array($value)) { $value = $value[0]; } - $value=_getFData_extractAndModify($value,$ch); - $format[$i]=str_replace($ch[0],$value,$format[$i]); + $value = _getFData_extractAndModify($value, $ch); + $format[$i] = str_replace($ch[0], $value, $format[$i]); } } else { - while (preg_match($expr,$format[$i],$ch)) { - if (method_exists($data,$meth)) { - $value = $data -> $meth($ch[1]); + while (preg_match(GETFDATA_REGEX, $format[$i], $ch)) { + if (method_exists($data, $meth)) { + $value = $data -> $meth($ch['key']); if (is_array($value)) { $value = $value[0]; } - $value=_getFData_extractAndModify($value,$ch); - $format[$i]=str_replace($ch[0],$value,$format[$i]); + $value = _getFData_extractAndModify($value, $ch); + $format[$i] = str_replace($ch[0], $value, $format[$i]); } else { - LSerror :: addErrorCode(0,getFData(_("Function 'getFData' : The method %{meth} of the object %{obj} doesn't exist."),array('meth' => $meth,'obj' => get_class($data)))); + LSerror :: addErrorCode( + 0, + getFData( + _("Function 'getFData' : The method %{meth} of the object %{obj} doesn't exist."), + array('meth' => $meth, 'obj' => get_class($data)) + ) + ); break; } } } } else { - while (preg_match($expr,$format[$i],$ch)) { - $val=_getFData_extractAndModify($data,$ch); - $format[$i]=str_replace($ch[0],$val,$format[$i]); + while (preg_match(GETFDATA_REGEX, $format[$i], $ch)) { + $val = _getFData_extractAndModify($data, $ch); + $format[$i] = str_replace($ch[0], $val, $format[$i]); } } } @@ -131,100 +142,95 @@ function getFData($format,$data,$meth=NULL) { return $format; } -function _getFData_extractAndModify($data,$ch) { +function _getFData_extractAndModify($data, $ch) { /* - * Format : %{[key name][:A][:B][-][! ou _][~][%}} - * - * Extracted fields - * - 0 : full string '%{...}' - * - 1 : key name - * - 2 : :A - * - 3 : A - * - 4 : :B - * - 5 : B - * - 6 : "!" / "_" / "~" / "%" + * Handle substring parameters (substr_a & substr_b) */ + // If A - if($ch[3]!="") { + if($ch['substr_a']) { // If A and B - if ($ch[5]!="") { + if ($ch['substr_b']) { // If A and B=0 - if ($ch[5]==0) { + if ($ch['substr_b'] == 0) { // If A<0 and B=0 - if ($ch[3]<0) { - $s=strlen((string)$data)-(-1*$ch[3]); - $l=strlen((string)$data); + if ($ch['substr_a'] < 0) { + $s = mb_strlen(strval($data)) - (-1 * $ch['substr_a']); + $l = mb_strlen(strval($data)); } // If A >= 0 and B else { - $s=$ch[3]; - $l=strlen((string)$data); + $s = $ch['substr_a']; + $l = mb_strlen(strval($data)); } } // If A and B > 0 - elseif ($ch[5]>0) { + elseif ($ch['substr_b'] > 0) { // If A < 0 and B > 0 or A >= 0 and B > 0 - $s=$ch[3]; - $l=$ch[5]; + $s = $ch['substr_a']; + $l = $ch['substr_b']; } // If A and B < 0 else { // If A < 0 and B < 0 - if ($ch[3]<0) { - $s=$ch[5]; - $l=false; + if ($ch['substr_a'] < 0) { + $s = $ch['substr_b']; + $l = false; } // If A >= 0 and B < 0 else { - $s=$ch[3]+$ch[5]; - $l=abs($ch[5]); + $s = $ch['substr_a'] + $ch['substr_b']; + $l = abs($ch['substr_b']); } } } // If only A else { - if ($ch[3]<0) { - $s=$ch[3]; - $l=false; + if ($ch['substr_a'] < 0) { + $s = $ch['substr_a']; + $l = false; } else { - $s=0; - $l=$ch[3]; + $s = 0; + $l = $ch['substr_a']; } } - if ($l==false) { - $val=mb_substr((string)$data,$s); + if ($l == false) { + $val = mb_substr(strval($data), $s); } else { - $val=mb_substr((string)$data,$s, abs($l)); + $val = mb_substr(strval($data), $s, abs($l)); } } else { try { - $val=strval($data); + $val = strval($data); } catch (Exception $e) { - $val=_('[not string value]'); + $val = _('[not string value]'); } } - if ($ch[6]) { + /* + * Handle modifiers + */ + if ($ch['modifiers']) { # Without Accent - if (strpos($ch[6], '~')!==false) { + if (strpos($ch['modifiers'], '~') !== false) { $val = withoutAccents($val); } # Upper / Lower case - if (strpos($ch[6], '!')!==false) { - $val=mb_strtoupper($val); + if (strpos($ch['modifiers'], '!') !== false) { + $val = mb_strtoupper($val); } - elseif (strpos($ch[6], '_')!==false) { - $val=mb_strtolower($val); + elseif (strpos($ch['modifiers'], '_') !== false) { + $val = mb_strtolower($val); } # Escape HTML entities - if (strpos($ch[6], '%')!==false) { + if (strpos($ch['modifiers'], '%') !== false) { $val = htmlentities($val); } } @@ -233,11 +239,10 @@ function _getFData_extractAndModify($data,$ch) { } function getFieldInFormat($format) { - $fields=array(); - $expr='/%[{(]([A-Za-z0-9]+)(\:(-?[0-9])+)?(\:(-?[0-9]+))?(-)?(\!|\_)?(~)?(%)?[})]/'; - while (preg_match($expr,$format,$ch)) { - $fields[]=$ch[1]; - $format=str_replace($ch[0],'',$format); + $fields = array(); + while (preg_match(GETFDATA_REGEX, $format, $ch)) { + $fields[] = $ch['key']; + $format = str_replace($ch[0], '', $format); } return $fields; }