Browse Source

Transform errors into known keys

Joachim M. Giæver 2 years ago
parent
commit
83c6db45b1
4 changed files with 93 additions and 8 deletions
  1. 2 1
      composer.json
  2. 46 1
      composer.lock
  3. 4 3
      src/AbstractPostSerializable.php
  4. 41 3
      src/Client.php

+ 2 - 1
composer.json

@@ -19,6 +19,7 @@
         }
     },
     "require": {
-        "symfony/http-client": "^5.1"
+        "symfony/http-client": "^5.1",
+        "haydenpierce/class-finder": "^0.4.3"
     }
 }

+ 46 - 1
composer.lock

@@ -4,8 +4,53 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "bcf9e00a58e257b19da296bdad96f18b",
+    "content-hash": "2521bbb643add09e0485ac1cd69b4467",
     "packages": [
+        {
+            "name": "haydenpierce/class-finder",
+            "version": "0.4.3",
+            "source": {
+                "type": "git",
+                "url": "https://gitlab.com/hpierce1102/ClassFinder.git",
+                "reference": "d6c68f386c764674c59ee336d1dfca35aada5f90"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://gitlab.com/api/v4/projects/hpierce1102%2FClassFinder/repository/archive.zip?sha=d6c68f386c764674c59ee336d1dfca35aada5f90",
+                "reference": "d6c68f386c764674c59ee336d1dfca35aada5f90",
+                "shasum": ""
+            },
+            "require": {
+                "ext-json": "*",
+                "php": ">=5.3"
+            },
+            "require-dev": {
+                "mikey179/vfsstream": "^1.6",
+                "phpunit/phpunit": "~9.0"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "HaydenPierce\\ClassFinder\\": "src/",
+                    "HaydenPierce\\ClassFinder\\UnitTest\\": "test/unit"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Hayden Pierce",
+                    "email": "hayden@haydenpierce.com"
+                }
+            ],
+            "description": "A library that can provide of a list of classes in a given namespace",
+            "support": {
+                "issues": "https://gitlab.com/api/v4/projects/7670051/issues"
+            },
+            "time": "2021-01-05T20:50:49+00:00"
+        },
         {
             "name": "psr/container",
             "version": "1.1.1",

+ 4 - 3
src/AbstractPostSerializable.php

@@ -2,9 +2,7 @@
 
 namespace DNB;
 
-use Reflection;
 use ReflectionClass;
-use ReflectionFunction;
 
 abstract class AbstractPostSerializable implements PostSerializableInterface {
     public static function fromPostParams(array $post): PostSerializableInterface {
@@ -34,10 +32,13 @@ abstract class AbstractPostSerializable implements PostSerializableInterface {
             if ($short)
                 $vars[$param->getName()] = !($param->isOptional() || $param->allowsNull());
             else
-                $vars[sprintf('%s-%s', strtolower(basename(str_replace('\\', DIRECTORY_SEPARATOR, static::class))), $param->getName())] = !($param->isOptional() || $param->allowsNull());
+                $vars[sprintf('%s-%s', static::namespace(), $param->getName())] = !($param->isOptional() || $param->allowsNull());
         }
 
         return $vars;
     }
 
+    public static function namespace(): string {
+        return strtolower(basename(str_replace('\\', DIRECTORY_SEPARATOR, static::class)));
+    }
 }

+ 41 - 3
src/Client.php

@@ -2,6 +2,7 @@
 
 namespace DNB;
 
+use HaydenPierce\ClassFinder\ClassFinder;
 use Symfony\Component\HttpClient\HttpClient;
 use Symfony\Contracts\HttpClient\HttpClientInterface;
 
@@ -70,12 +71,27 @@ final class Client {
         ]);
     }
 
+    private function findInNamespace(string $ns): ?string {
+        $classes = array_filter(ClassFinder::getClassesInNamespace(
+            dirname(str_replace('\\', DIRECTORY_SEPARATOR, self::class))
+        ), function (string $class) {
+            return class_implements($class, AbstractPostSerializable::class) && strpos($class, AbstractPostSerializable::class) !== 0;
+        });
+        var_dump($classes);
+
+        $key = array_search(strtolower($ns), array_map(function (string $class): string {
+            return $class::namespace();
+        }, $classes));
+
+        return $key ? $classes[$key] : null;
+    }
+
     public function orderProspectus(
         Prospectus $prospectus
     ): bool {
         $ret = $this->getAuthClient()->request(
             "POST",
-            'https://uat-process-externalpart.azurewebsites.net/api/v1/autoprospect/orderprospect', [
+            $this->getUrl($this->clientConfig->get()->endpoint, '/api/v1/autoprospect/orderprospect'), [
                 'json' => $prospectus->toPostParams(),
             ]
         );
@@ -83,8 +99,30 @@ final class Client {
             $ret->getContent();
             return true;
         } catch (\Exception $e) {
-            $content = $ret->getContent(false);
-            throw new \InvalidArgumentException(!empty($content) ? $content : $e->getMessage(), $e->getCode(), $e);
+            $content = json_decode($ret->getContent(false), true);
+
+            if (json_last_error() == JSON_ERROR_NONE) {
+                foreach ($content as $key => $value) {
+                    $scope = explode('.', $key);
+
+                    if (sizeof($scope) <= 1)
+                        continue;
+
+                    $ns = $this->findInNamespace($scope[0]);
+
+                    foreach (array_keys($ns::getPostParams()) as $param) {
+                        if (strpos(strtolower($param), strtolower($scope[1])) == strlen($ns::namespace())+1) {
+                            unset($content[$key]);
+                            $content[$param] = $value;
+                        }
+                    }
+
+                }
+                $content = json_encode($content);
+            } else
+                $content = $e->getMessage();
+
+            throw new \InvalidArgumentException($content, $e->getCode(), $e);
         }
 
         return false;