Commit 60ef4fb5 authored by Sylvain's avatar Sylvain

Ajout d'un Helper Class

parent 4fb9010d
......@@ -107,7 +107,7 @@ La fonction `array_to_string()` converti un tableau en string
$array = ['id', 'name', 'value'];
$string = array_to_string($array);
// "id, name, value"
// "['id', 'name', 'value']"
```
#### `string_to_float()`
......
......@@ -12,6 +12,9 @@
}
],
"autoload": {
"psr-4": {
"Goldenscarab\\Modulus\\Helpers\\": "src/Helpers/"
},
"files": [
"src/convert.php",
"src/divers.php",
......
<?php
namespace Goldenscarab\Modulus\Helpers;
class Dynamik
{
private $source;
public function __construct($source)
{
$this->source = $source;
}
/**
* Création d'une valeur depuis une condition
*
* @param array $data
* $structure =
* [
* 'value' => ':clef.de.recherche',
* 'operator' => '==',
* 'comparator' => 'valeur de comparaison',
* 'true' => 'valeur si vrai',
* 'false' => 'valeur si faux'
* ]
* @return Mixte La valeur
*/
public function getConditionalValue(array $data)
{
$val = $this->getValue($data['value']);
$operator = $data['operator'];
$true = $data['true'];
$comparator = $data['comparator'];
$false = $data['false'];
switch ($operator) {
case '==' :
$value = $val == $comparator ? $true : $false;
break;
case '>' :
$value = $val > $comparator ? $true : $false;
break;
case '<' :
$value = $val < $comparator ? $true : $false;
break;
case '!=' :
$value = $val != $comparator ? $true : $false;
break;
case '<=' :
$value = $val <= $comparator ? $true : $false;
break;
case '>=' :
$value = $val >= $comparator ? $true : $false;
break;
default :
$value = $false;
}
return $this->getValue($value);
}
/**
* Création d'une valeur depuis un template
*
* @param array $data
* $structure =
* [
* 'format' => 'le template à utiliser',
* 'args' => [':clef.de.recherche', 'Valeur fixe'],
* ]
* @return Mixte La valeur
*/
public function getFormatedValue(array $data)
{
$value = null;
$format = $data['format'];
$args = $data['args'];
// Traitement des % seuls pour éviter les conflits
$format = preg_replace('/(%)[^0-9sd.\']/i', '%%"', $format);
// Convertion en tableau si besoin
// if (!is_array($args)) {
// $args = [$args];
// }
// Préparation des arguments pour le template
$arguments = $this->getValue($args);
if (!is_array($arguments)) {
$arguments = [$arguments];
}
$value = vsprintf($format, $arguments);
return $value;
}
/**
* Récupération d'une valeur depuis une fonction callback
* $structure =
* [
* 'function' => 'function_callback',
* 'args' => [':clef.de.recherche', 'Valeur fixe'],
* ]
* @param array $data
* @return Mixte
*/
public function getCallbackValue($data)
{
$value = null;
$function = $data['function'];
$args = $data['args'];
// Traitement sur le nom de la fonction (suppression des parenthèses)
$function = preg_replace('/(\(.*)/', '', $function);
// Préparation des arguments pour la fonction
$arguments = (array) $this->getValue($args);
//dd($function, $arguments);
if (!is_array($arguments)) {
$arguments = [$arguments];
}
// if ($function == 'str_limit') {
// dd('ok');
// }
$value = call_user_func_array($function, $arguments);
return $this->getValue($value);
}
/**
* Récupération d'une valeur dynamique (en préfixant avec « : »)
* Exemple :
* :name
* :updated_at->diffForHumans()
* [ 'template' => [...]]
* [ 'condition' => [...]]
* [ 'callback' => [...]]
* date('Y')
* 'Ma valeur fixe'
*
* @param [type] $data
* @return void
*/
public function getValue($data)
{
$value = null;
if (is_array($data)) {
$values = [];
// Parcours des arguments
foreach ($data as $key => $d) {
if ($key === 'condition') {
$values[] = $this->getConditionalValue($d);
} else if ($key === 'template') {
$values[] = $this->getFormatedValue($d);
} else if ($key === 'callback') {
$values[] = $this->getCallbackValue($d);
} else {
$values[] = $this->getValue($d);
}
}
// Si une seule valeur alors on supprime le tableau
if (count($values) == 1) {
$value = $values[0];
} else {
$value = $values;
}
} else {
// Valeur dynamique
if (preg_match('/^:.*$/', $data)) {
$data = trim($data, ':');
$value = mixte_get($this->source, $data);
} else {
$value = $data;
}
}
return $value;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment