getFData() & getFieldInFormat(): code cleaning

This commit is contained in:
Benjamin Renard 2023-05-17 17:11:50 +02:00
parent 2d108c8b08
commit 7b7ff7e629
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -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.
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @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.
*
* @return string La chaine formatée
*/
function getFData($format,$data,$meth=NULL) {
$unique=false;
/*
* Format : %{[key name][:A][:B][! ou _][~][%}}
* *
* Extracted fields * Extracted fields
* - 0 : full string '%{...}' * - 0 : full string '%{...}'
* - 1 : key name * - 1/key : key name
* - 2 : :A * - 2 : :A
* - 3 : A * - 3/substr_a : A
* - 4 : :B * - 4 : :B
* - 5 : B * - 5/substr_b : B
* - 6 : "!" / "_" / "~" / "%" * - 6/modifiers : "!" / "_" / "~" / "%"
*/ */
$expr="/%[{(]([A-Za-z0-9]+)(\:(-?[0-9])+)?(\:(-?[0-9]+))?([\!\_~%]*)[})]/"; define(
'GETFDATA_REGEX',
"/%[{(](?P<key>[A-Za-z0-9]+)(\:(?P<substr_a>-?[0-9])+)?(\:(?P<substr_b>[0-9]+))?(?P<modifiers>[\!\_~%]*)[})]/"
);
/**
* Build LSformat string
*
* This function build and return a LSformat string from specified format and provided data.
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @param string $format The LSformat string
* @param string|object|array<string|object> $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 The builded LSformat string
*/
function getFData($format, $data, $meth=NULL) {
$unique = false;
if(!is_array($format)) { if(!is_array($format)) {
$format=array($format); $format = array($format);
$unique=true; $unique = true;
} }
for($i=0;$i<count($format);$i++) { for($i=0; $i<count($format); $i++) {
if(is_array($data)) { if(is_array($data)) {
if ($meth==NULL) { if (is_null($meth)) {
while (preg_match($expr,$format[$i],$ch)) { while (preg_match(GETFDATA_REGEX, $format[$i], $ch)) {
if (!isset($data[$ch[1]])) { if (!isset($data[$ch['key']])) {
$val = ''; $val = '';
} }
elseif (is_array($data[$ch[1]])) { elseif (is_array($data[$ch['key']])) {
$val = $data[$ch[1]][0]; $val = $data[$ch['key']][0];
} }
else { else {
$val = $data[$ch[1]]; $val = $data[$ch['key']];
} }
$val=_getFData_extractAndModify($val,$ch); $val = _getFData_extractAndModify($val, $ch);
$format[$i]=str_replace($ch[0],$val,$format[$i]); $format[$i] = str_replace($ch[0], $val, $format[$i]);
} }
} }
else { else {
while (preg_match($expr,$format[$i],$ch)) { while (preg_match(GETFDATA_REGEX, $format[$i], $ch)) {
if (method_exists($data[$ch[1]],$meth)) { if (method_exists($data[$ch['key']], $meth)) {
$value = $data[$ch[1]] -> $meth(); $value = $data[$ch['key']] -> $meth();
if (is_array($value)) { if (is_array($value)) {
$value = $value[0]; $value = $value[0];
} }
$value=_getFData_extractAndModify($value,$ch); $value = _getFData_extractAndModify($value, $ch);
$format[$i]=str_replace($ch[0],$value,$format[$i]); $format[$i] = str_replace($ch[0], $value, $format[$i]);
} }
else { else {
LSerror :: addErrorCode('fct_getFData_01',array('meth' => $meth,'obj' => $ch[1])); LSerror :: addErrorCode(
'fct_getFData_01',
array('meth' => $meth, 'obj' => $ch['key'])
);
break; break;
} }
} }
} }
} }
elseif (is_object($data)) { elseif (is_object($data)) {
if ($meth==NULL) { if (is_null($meth)) {
while (preg_match($expr,$format[$i],$ch)) { while (preg_match(GETFDATA_REGEX, $format[$i], $ch)) {
$value = $data -> $ch[1]; $value = $data -> $ch['key'];
if (is_array($value)) { if (is_array($value)) {
$value = $value[0]; $value = $value[0];
} }
$value=_getFData_extractAndModify($value,$ch); $value = _getFData_extractAndModify($value, $ch);
$format[$i]=str_replace($ch[0],$value,$format[$i]); $format[$i] = str_replace($ch[0], $value, $format[$i]);
} }
} }
else { else {
while (preg_match($expr,$format[$i],$ch)) { while (preg_match(GETFDATA_REGEX, $format[$i], $ch)) {
if (method_exists($data,$meth)) { if (method_exists($data, $meth)) {
$value = $data -> $meth($ch[1]); $value = $data -> $meth($ch['key']);
if (is_array($value)) { if (is_array($value)) {
$value = $value[0]; $value = $value[0];
} }
$value=_getFData_extractAndModify($value,$ch); $value = _getFData_extractAndModify($value, $ch);
$format[$i]=str_replace($ch[0],$value,$format[$i]); $format[$i] = str_replace($ch[0], $value, $format[$i]);
} }
else { 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; break;
} }
} }
} }
} }
else { else {
while (preg_match($expr,$format[$i],$ch)) { while (preg_match(GETFDATA_REGEX, $format[$i], $ch)) {
$val=_getFData_extractAndModify($data,$ch); $val = _getFData_extractAndModify($data, $ch);
$format[$i]=str_replace($ch[0],$val,$format[$i]); $format[$i] = str_replace($ch[0], $val, $format[$i]);
} }
} }
} }
@ -131,100 +142,95 @@ function getFData($format,$data,$meth=NULL) {
return $format; return $format;
} }
function _getFData_extractAndModify($data,$ch) { function _getFData_extractAndModify($data, $ch) {
/* /*
* Format : %{[key name][:A][:B][-][! ou _][~][%}} * Handle substring parameters (substr_a & substr_b)
*
* Extracted fields
* - 0 : full string '%{...}'
* - 1 : key name
* - 2 : :A
* - 3 : A
* - 4 : :B
* - 5 : B
* - 6 : "!" / "_" / "~" / "%"
*/ */
// If A // If A
if($ch[3]!="") { if($ch['substr_a']) {
// If A and B // If A and B
if ($ch[5]!="") { if ($ch['substr_b']) {
// If A and B=0 // If A and B=0
if ($ch[5]==0) { if ($ch['substr_b'] == 0) {
// If A<0 and B=0 // If A<0 and B=0
if ($ch[3]<0) { if ($ch['substr_a'] < 0) {
$s=strlen((string)$data)-(-1*$ch[3]); $s = mb_strlen(strval($data)) - (-1 * $ch['substr_a']);
$l=strlen((string)$data); $l = mb_strlen(strval($data));
} }
// If A >= 0 and B // If A >= 0 and B
else { else {
$s=$ch[3]; $s = $ch['substr_a'];
$l=strlen((string)$data); $l = mb_strlen(strval($data));
} }
} }
// If A and B > 0 // 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 // If A < 0 and B > 0 or A >= 0 and B > 0
$s=$ch[3]; $s = $ch['substr_a'];
$l=$ch[5]; $l = $ch['substr_b'];
} }
// If A and B < 0 // If A and B < 0
else { else {
// If A < 0 and B < 0 // If A < 0 and B < 0
if ($ch[3]<0) { if ($ch['substr_a'] < 0) {
$s=$ch[5]; $s = $ch['substr_b'];
$l=false; $l = false;
} }
// If A >= 0 and B < 0 // If A >= 0 and B < 0
else { else {
$s=$ch[3]+$ch[5]; $s = $ch['substr_a'] + $ch['substr_b'];
$l=abs($ch[5]); $l = abs($ch['substr_b']);
} }
} }
} }
// If only A // If only A
else { else {
if ($ch[3]<0) { if ($ch['substr_a'] < 0) {
$s=$ch[3]; $s = $ch['substr_a'];
$l=false; $l = false;
} }
else { else {
$s=0; $s = 0;
$l=$ch[3]; $l = $ch['substr_a'];
} }
} }
if ($l==false) { if ($l == false) {
$val=mb_substr((string)$data,$s); $val = mb_substr(strval($data), $s);
} }
else { else {
$val=mb_substr((string)$data,$s, abs($l)); $val = mb_substr(strval($data), $s, abs($l));
} }
} }
else { else {
try { try {
$val=strval($data); $val = strval($data);
} }
catch (Exception $e) { catch (Exception $e) {
$val=_('[not string value]'); $val = _('[not string value]');
} }
} }
if ($ch[6]) { /*
* Handle modifiers
*/
if ($ch['modifiers']) {
# Without Accent # Without Accent
if (strpos($ch[6], '~')!==false) { if (strpos($ch['modifiers'], '~') !== false) {
$val = withoutAccents($val); $val = withoutAccents($val);
} }
# Upper / Lower case # Upper / Lower case
if (strpos($ch[6], '!')!==false) { if (strpos($ch['modifiers'], '!') !== false) {
$val=mb_strtoupper($val); $val = mb_strtoupper($val);
} }
elseif (strpos($ch[6], '_')!==false) { elseif (strpos($ch['modifiers'], '_') !== false) {
$val=mb_strtolower($val); $val = mb_strtolower($val);
} }
# Escape HTML entities # Escape HTML entities
if (strpos($ch[6], '%')!==false) { if (strpos($ch['modifiers'], '%') !== false) {
$val = htmlentities($val); $val = htmlentities($val);
} }
} }
@ -233,11 +239,10 @@ function _getFData_extractAndModify($data,$ch) {
} }
function getFieldInFormat($format) { function getFieldInFormat($format) {
$fields=array(); $fields = array();
$expr='/%[{(]([A-Za-z0-9]+)(\:(-?[0-9])+)?(\:(-?[0-9]+))?(-)?(\!|\_)?(~)?(%)?[})]/'; while (preg_match(GETFDATA_REGEX, $format, $ch)) {
while (preg_match($expr,$format,$ch)) { $fields[] = $ch['key'];
$fields[]=$ch[1]; $format = str_replace($ch[0], '', $format);
$format=str_replace($ch[0],'',$format);
} }
return $fields; return $fields;
} }