瀏覽代碼

make ObjectUtils::hash static

Olivier Massot 1 年之前
父節點
當前提交
2a99af23bd
共有 2 個文件被更改,包括 4 次插入4 次删除
  1. 1 1
      src/EventListener/OnKernelRequestPreRead.php
  2. 3 3
      src/Service/Utils/ObjectUtils.php

+ 1 - 1
src/EventListener/OnKernelRequestPreRead.php

@@ -57,7 +57,7 @@ class OnKernelRequestPreRead implements EventSubscriberInterface
                     'activityYear' => $access->getActivityYear(),
                     'historical' => $access->getHistorical(),
                 ];
-                $expectedHash = $this->objectUtils->hash($profileMask, 'sha1');
+                $expectedHash = ObjectUtils::hash($profileMask, 'sha1');
 
                 if ($expectedHash !== $profileHash) {
                     throw new \RuntimeException('Invalid profile hash');

+ 3 - 3
src/Service/Utils/ObjectUtils.php

@@ -8,17 +8,17 @@ class ObjectUtils
      * Créé un hash à partir d'un objet
      * (après l'avoir trié selon ses clés, et convertit en json sans espace)
      *
-     * @param object|array $instance
+     * @param object|array<mixed> $instance
      * @param string $algorithm
      * @return string
      */
-    public function hash(object | array $instance, string $algorithm = 'sha256'): string
+    public static function hash(object | array $instance, string $algorithm = 'sha256'): string
     {
         // Convertit l'objet en tableau associatif
         $array = (array)$instance;
         // Puis trie l'objet selon ses clés, encode en json, et hash
         ksort($array);
-        $json = json_encode($array);
+        $json = json_encode((object)$array);
         return hash($algorithm, $json);
     }
 }