Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
Helpers
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Modulus
Helpers
Commits
60ef4fb5
Commit
60ef4fb5
authored
Apr 17, 2020
by
Sylvain
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ajout d'un Helper Class
parent
4fb9010d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
192 additions
and
1 deletion
+192
-1
README.md
README.md
+1
-1
composer.json
composer.json
+3
-0
src/Helpers/Dynamik.php
src/Helpers/Dynamik.php
+188
-0
No files found.
README.md
View file @
60ef4fb5
...
...
@@ -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()`
...
...
composer.json
View file @
60ef4fb5
...
...
@@ -12,6 +12,9 @@
}
],
"autoload"
:
{
"psr-4"
:
{
"Goldenscarab\\Modulus\\Helpers\\"
:
"src/Helpers/"
},
"files"
:
[
"src/convert.php"
,
"src/divers.php"
,
...
...
src/Helpers/Dynamik.php
0 → 100644
View file @
60ef4fb5
<?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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment