blitz-php /
parametres
| 1 | <?php |
||||||||
| 2 | |||||||||
| 3 | /** |
||||||||
| 4 | * This file is part of BlitzPHP Parametres. |
||||||||
| 5 | * |
||||||||
| 6 | * (c) 2025 Dimitri Sitchet Tomkeu <[email protected]> |
||||||||
| 7 | * |
||||||||
| 8 | * For the full copyright and license information, please view |
||||||||
| 9 | * the LICENSE file that was distributed with this source code. |
||||||||
| 10 | */ |
||||||||
| 11 | |||||||||
| 12 | namespace BlitzPHP\Parametres\Handlers; |
||||||||
| 13 | |||||||||
| 14 | use RuntimeException; |
||||||||
| 15 | |||||||||
| 16 | abstract class BaseHandler |
||||||||
| 17 | { |
||||||||
| 18 | /** |
||||||||
| 19 | * Vérifie si ce gestionnaire a une valeur définie. |
||||||||
| 20 | */ |
||||||||
| 21 | abstract public function has(string $file, string $property, ?string $context = null): bool; |
||||||||
| 22 | |||||||||
| 23 | /** |
||||||||
| 24 | * Renvoie une seule valeur du gestionnaire, si elle est stockée. |
||||||||
| 25 | */ |
||||||||
| 26 | abstract public function get(string $file, string $property, ?string $context = null): mixed; |
||||||||
| 27 | |||||||||
| 28 | /** |
||||||||
| 29 | * Si le gestionnaire prend en charge l'enregistrement des valeurs, il DOIT surcharger cette méthode pour fournir cette fonctionnalité. |
||||||||
| 30 | * Tous les gestionnaires ne prennent pas en charge l'écriture des valeurs. |
||||||||
| 31 | * Doit lancer une RuntimeException en cas d'échec. |
||||||||
| 32 | * |
||||||||
| 33 | * @throws RuntimeException |
||||||||
| 34 | */ |
||||||||
| 35 | public function set(string $file, string $property, mixed $value = null, ?string $context = null): void |
||||||||
|
0 ignored issues
–
show
The parameter
$file is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$property is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||||
| 36 | { |
||||||||
| 37 | 2 | throw new RuntimeException('La méthode "set" n\'est pas implémentée pour le gestionnaire de paramètres actuel.'); |
|||||||
| 38 | } |
||||||||
| 39 | |||||||||
| 40 | /** |
||||||||
| 41 | * Si le gestionnaire prend en charge l'oubli de valeurs, il DOIT surcharger cette méthode pour fournir cette fonctionnalité. |
||||||||
| 42 | * Tous les gestionnaires ne prennent pas en charge l'écriture de valeurs. |
||||||||
| 43 | * Doit lancer une RuntimeException en cas d'échec. |
||||||||
| 44 | * |
||||||||
| 45 | * @throws RuntimeException |
||||||||
| 46 | */ |
||||||||
| 47 | public function forget(string $file, string $property, ?string $context = null): void |
||||||||
|
0 ignored issues
–
show
The parameter
$context is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$property is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$file is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||||
| 48 | { |
||||||||
| 49 | 2 | throw new RuntimeException('La méthode "forget" n\'est pas implémentée pour le gestionnaire de paramètres actuel.'); |
|||||||
| 50 | } |
||||||||
| 51 | |||||||||
| 52 | /** |
||||||||
| 53 | * Tous les gestionnaires DOIVENT prendre en charge l'effacement de toutes les valeurs. |
||||||||
| 54 | * |
||||||||
| 55 | * @throws RuntimeException |
||||||||
| 56 | */ |
||||||||
| 57 | public function flush(): void |
||||||||
| 58 | { |
||||||||
| 59 | 2 | throw new RuntimeException('La méthode "flush" n\'est pas implémentée pour le gestionnaire de paramètres actuel.'); |
|||||||
| 60 | } |
||||||||
| 61 | |||||||||
| 62 | /** |
||||||||
| 63 | * Prend en charge la conversion de certains types d'objets afin qu'ils puissent |
||||||||
| 64 | * être stockés en toute sécurité et réhydratés dans les fichiers de configuration. |
||||||||
| 65 | * |
||||||||
| 66 | * @return mixed|string |
||||||||
| 67 | */ |
||||||||
| 68 | protected function prepareValue(mixed $value) |
||||||||
| 69 | { |
||||||||
| 70 | if (is_bool($value)) { |
||||||||
| 71 | return (int) $value; |
||||||||
| 72 | } |
||||||||
| 73 | |||||||||
| 74 | if (is_array($value) || is_object($value)) { |
||||||||
| 75 | 2 | return serialize($value); |
|||||||
| 76 | } |
||||||||
| 77 | |||||||||
| 78 | 2 | return $value; |
|||||||
| 79 | } |
||||||||
| 80 | |||||||||
| 81 | /** |
||||||||
| 82 | * Gère certaines conversions spéciales que les données peuvent avoir été enregistrées, |
||||||||
| 83 | * telles que les booléens et les données sérialisées. |
||||||||
| 84 | * |
||||||||
| 85 | * @return bool|mixed |
||||||||
| 86 | */ |
||||||||
| 87 | protected function parseValue(mixed $value, string $type) |
||||||||
| 88 | { |
||||||||
| 89 | // Sérialisé? |
||||||||
| 90 | if ($this->isSerialized($value)) { |
||||||||
| 91 | 2 | $value = unserialize($value); |
|||||||
| 92 | } |
||||||||
| 93 | |||||||||
| 94 | 2 | settype($value, $type); |
|||||||
| 95 | |||||||||
| 96 | 2 | return $value; |
|||||||
| 97 | } |
||||||||
| 98 | |||||||||
| 99 | /** |
||||||||
| 100 | * Vérifie si un objet est sérialisé et correctement formaté. |
||||||||
| 101 | * |
||||||||
| 102 | * Tiré des fonctions de base de Wordpress. |
||||||||
| 103 | * |
||||||||
| 104 | * @param bool $strict S'il faut être strict sur la fin de la chaîne. |
||||||||
| 105 | */ |
||||||||
| 106 | protected function isSerialized(mixed $data, bool $strict = true): bool |
||||||||
| 107 | { |
||||||||
| 108 | // Si ce n'est pas une chaîne, elle n'est pas sérialisée. |
||||||||
| 109 | if (! is_string($data)) { |
||||||||
| 110 | return false; |
||||||||
| 111 | } |
||||||||
| 112 | 2 | $data = trim($data); |
|||||||
| 113 | if ('N;' === $data) { |
||||||||
| 114 | return true; |
||||||||
| 115 | } |
||||||||
| 116 | if (strlen($data) < 4) { |
||||||||
| 117 | return false; |
||||||||
| 118 | } |
||||||||
| 119 | if (':' !== $data[1]) { |
||||||||
| 120 | 2 | return false; |
|||||||
| 121 | } |
||||||||
| 122 | if ($strict) { |
||||||||
| 123 | $lastc = substr($data, -1); |
||||||||
| 124 | if (';' !== $lastc && '}' !== $lastc) { |
||||||||
| 125 | return false; |
||||||||
| 126 | } |
||||||||
| 127 | } else { |
||||||||
| 128 | $semicolon = strpos($data, ';'); |
||||||||
| 129 | $brace = strpos($data, '}'); |
||||||||
| 130 | // L'un ou l'autre ; ou } doit exister. |
||||||||
| 131 | if (false === $semicolon && false === $brace) { |
||||||||
| 132 | return false; |
||||||||
| 133 | } |
||||||||
| 134 | // Mais aucun des deux ne doit se trouver dans les X premiers caractères. |
||||||||
| 135 | if (false !== $semicolon && $semicolon < 3) { |
||||||||
| 136 | return false; |
||||||||
| 137 | } |
||||||||
| 138 | if (false !== $brace && $brace < 4) { |
||||||||
| 139 | return false; |
||||||||
| 140 | } |
||||||||
| 141 | } |
||||||||
| 142 | $token = $data[0]; |
||||||||
| 143 | |||||||||
| 144 | switch ($token) { |
||||||||
| 145 | case 's': |
||||||||
| 146 | if ($strict) { |
||||||||
| 147 | if ('"' !== substr($data, -2, 1)) { |
||||||||
| 148 | return false; |
||||||||
| 149 | } |
||||||||
| 150 | } elseif (! str_contains($data, '"')) { |
||||||||
| 151 | return false; |
||||||||
| 152 | } |
||||||||
| 153 | |||||||||
| 154 | // Ou bien tomber dans le vide. |
||||||||
| 155 | // no break |
||||||||
| 156 | case 'a': |
||||||||
| 157 | case 'O': |
||||||||
| 158 | return (bool) preg_match("/^{$token}:[0-9]+:/s", $data); |
||||||||
| 159 | |||||||||
| 160 | case 'b': |
||||||||
| 161 | case 'i': |
||||||||
| 162 | case 'd': |
||||||||
| 163 | $end = $strict ? '$' : ''; |
||||||||
| 164 | |||||||||
| 165 | return (bool) preg_match("/^{$token}:[0-9.E+-]+;{$end}/", $data); |
||||||||
| 166 | } |
||||||||
| 167 | |||||||||
| 168 | return false; |
||||||||
| 169 | } |
||||||||
| 170 | } |
||||||||
| 171 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.