From 30f8d319bf17ab6b77edde4ddaff7d63c61566d0 Mon Sep 17 00:00:00 2001
From: Dave Mc Nicoll <dave.mcnicoll@cslsj.qc.ca>
Date: Wed, 1 Nov 2023 11:32:02 -0400
Subject: [PATCH] - Bugfixes, still a WIP, but mostly functionnal now !

---
 src/Adapter/Rest.php            |  6 +++---
 src/ApiRepository.php           |  2 +-
 src/Common/JsonMessageTrait.php |  2 +-
 src/Stream/JsonStream.php       |  8 +++++++-
 src/Transport/CurlTransport.php | 19 +++++++++++++++++--
 5 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/src/Adapter/Rest.php b/src/Adapter/Rest.php
index a334228..e5886f2 100644
--- a/src/Adapter/Rest.php
+++ b/src/Adapter/Rest.php
@@ -58,8 +58,8 @@ class Rest implements AdapterInterface
             case is_object($value):
                 return Ulmus::convertObject($value);
 
-            case is_array($value):
-                return json_encode($value);
+            # case is_array($value):
+            #    return json_encode($value);
 
             case is_bool($value):
                 return (int) $value;
@@ -77,4 +77,4 @@ class Rest implements AdapterInterface
     {
         return RequestBuilder::class;
     }
-}
\ No newline at end of file
+}
diff --git a/src/ApiRepository.php b/src/ApiRepository.php
index 6cbf03e..b766683 100644
--- a/src/ApiRepository.php
+++ b/src/ApiRepository.php
@@ -47,7 +47,7 @@ class ApiRepository extends \Ulmus\Repository
 
     public function save(object|array $entity, ?array $fieldsAndValue = null, bool $replace = false): bool
     {
-        $response = $this->executeRequest(Attribute\Obj\Api\Create::class, $entity->entityLoadedDataset);
+        $response = $this->executeRequest(Attribute\Obj\Api\Create::class, $entity->entityGetDataset());
 
         $entity->fromArray($response->getParsedBody());
 
diff --git a/src/Common/JsonMessageTrait.php b/src/Common/JsonMessageTrait.php
index a249b8e..c15c29e 100644
--- a/src/Common/JsonMessageTrait.php
+++ b/src/Common/JsonMessageTrait.php
@@ -9,7 +9,7 @@ trait JsonMessageTrait
 {
     public function getParsedBody(): mixed
     {
-        return JsonStream::fromJsonEncoded( $this->getBody()->getContents() )->decode();
+        return JsonStream::fromJsonEncoded( $this->getBody()->getContents() )->decode() ;
     }
 
     public function withParsedBody(mixed $data) : MessageInterface
diff --git a/src/Stream/JsonStream.php b/src/Stream/JsonStream.php
index fa5e57f..983e0b2 100644
--- a/src/Stream/JsonStream.php
+++ b/src/Stream/JsonStream.php
@@ -45,6 +45,12 @@ class JsonStream extends Stream
 
     public function decode(): mixed
     {
-        return json_decode($this->getContents(), true, $this->depth, $this->decodingOptions);
+        $content = $this->getContents();
+
+        if ($content === "") {
+            return null;
+        }
+
+        return json_decode($content, true, $this->depth, $this->decodingOptions);
     }
 }
\ No newline at end of file
diff --git a/src/Transport/CurlTransport.php b/src/Transport/CurlTransport.php
index de621f4..d117527 100644
--- a/src/Transport/CurlTransport.php
+++ b/src/Transport/CurlTransport.php
@@ -5,7 +5,7 @@ namespace Ulmus\Api\Transport;
 use Ulmus\Api\Common\{ ContentTypeEnum, MethodEnum, HttpHeaderEnum };
 use Ulmus\Api\Stream\JsonStream;
 use Ulmus\Api\Stream\Stream;
-use Psr\Http\Message\{ RequestInterface, ResponseInterface };
+use Psr\Http\Message\{RequestInterface, ResponseInterface, StreamInterface};
 use Ulmus\Api\Response\{ Response, JsonResponse };
 
 abstract class CurlTransport {
@@ -14,6 +14,8 @@ abstract class CurlTransport {
 
     public int $maximumRedirections = 10;
 
+    protected StreamInterface $stderr;
+
     public function __construct(
         public array $headers = [],
         public array $curlOptions = [],
@@ -21,6 +23,7 @@ abstract class CurlTransport {
         public ContentTypeEnum $contentType = ContentTypeEnum::Json,
         # Matching Guzzle's great user-agent syntax
         public string $userAgent = "ulmus-api/1.0 curl/{curl} php/{php}",
+        public bool $debug = false,
     ) {
         $this->userAgent = str_replace([ '{curl}', '{php}', ], [ curl_version()['version'] ?? "???", phpversion() ], $this->userAgent);
     }
@@ -59,6 +62,11 @@ abstract class CurlTransport {
     {
         $response = new Response();
 
+        if ($this->debug) {
+            $this->stderr = Stream::fromMemory();
+            $stderrResource = $this->stderr->detach();
+        }
+
         $headers = array_merge_recursive(HttpHeaderEnum::normalizeHeaderArray($headers), HttpHeaderEnum::normalizeHeaderArray($this->headers));
 
         $options += $this->curlOptions;
@@ -92,7 +100,10 @@ abstract class CurlTransport {
 
                 return strlen($headerLine);
             }
-        ];
+        ] + ( $this->debug ? [
+            CURLOPT_VERBOSE => true,
+            CURLOPT_STDERR => $stderrResource,
+        ] : [] );
 
         $ch = curl_init();
 
@@ -107,6 +118,10 @@ abstract class CurlTransport {
             throw new \Exception(implode(PHP_EOL, $errors), $errno);
         }
 
+        if ($this->debug) {
+            $this->stderr->attach($stderrResource);
+        }
+
         $response = $response->withBody( Stream::fromMemory($result) );
 
         if ( $response->hasHeader('Content-Type') && false !== strpos($response->getHeader('Content-Type')[0], 'json') ) {