Repository: horstoeko/zugferd (php)

Issue:
[FEATURE] Add Support for Setting `SerializerBuilder` Cache Directory in `ZugferdSettings`
## Description  
Currently, this library uses the `JMS\Serializer\SerializerBuilder`, which offers the method `setCacheDir()` to define a caching directory. However, there is no mechanism in the static `ZugferdSettings` object to specify the cache directory, which could then be passed when constructing the `SerializerBuilder`.  

Adding this feature would allow users to configure the cache directory via the `ZugferdSettings` object, improving flexibility and maintainability for applications that use this library.

## Proposed Solution  
- Add a new property (e.g., `cacheDirectory`) to the `ZugferdSettings` object.
- Allow the caching directory to be passed to the `SerializerBuilder` through `setCacheDir()` when initializing it.
- Ensure backward compatibility by keeping the default behavior unchanged unless `cacheDirectory` is explicitly set.

## Example Usage  
```php
ZugferdSettings::setCacheDirectory('/path/to/cache');

// When constructing the SerializerBuilder in initSerialzer():
$this->serializerBuilder = SerializerBuilder::create();
$this->serializerBuilder->setCacheDir(ZugferdSettings::getCacheDirectory());
```

##  Benefits
- Simplifies configuration management by consolidating settings in the `ZugferdSettings` object.
- Provides developers with more control over caching behavior, especially useful in production environments.
- Enhances the library's usability by leveraging the existing static settings object.


Pull request:
[ENH] #245 Add Support for Setting SerializerBuilder Cache Directory
# Description

Currently, this library uses the JMS\Serializer\SerializerBuilder, which offers the method setCacheDir() to define a caching directory. However, there is no mechanism in the static ZugferdSettings object to specify the cache directory, which could then be passed when constructing the SerializerBuilder.

Adding this feature would allow users to configure the cache directory via the ZugferdSettings object, improving flexibility and maintainability for applications that use this library.

Fixes #245 

## Type of change

Please delete options that are not relevant.

- [X] New feature (non-breaking change which adds functionality)


Patch:
diff --git a/src/ZugferdDocument.php b/src/ZugferdDocument.php
index 1435787..9e21376 100644
--- a/src/ZugferdDocument.php
+++ b/src/ZugferdDocument.php
@@ -245,6 +245,10 @@ class ZugferdDocument
             )
         );
 
+        if (ZugferdSettings::hasSerializerCacheDirectory()) {
+            $this->serializerBuilder->setCacheDir(ZugferdSettings::getSerializerCacheDirectory());
+        }
+
         $this->serializerBuilder->addDefaultListeners();
         $this->serializerBuilder->addDefaultHandlers();
 
diff --git a/src/ZugferdSettings.php b/src/ZugferdSettings.php
index 42d4fdc..23af0d6 100644
--- a/src/ZugferdSettings.php
+++ b/src/ZugferdSettings.php
@@ -10,6 +10,7 @@
 namespace horstoeko\zugferd;
 
 use horstoeko\stringmanagement\PathUtils;
+use horstoeko\stringmanagement\StringUtils;
 
 /**
  * Class representing the general settings
@@ -85,6 +86,13 @@ class ZugferdSettings
      */
     protected static $specialDecimalPlacesMaps = [];
 
+    /**
+     * The configured cache directory for the serializer
+     *
+     * @var string
+     */
+    protected static $serializerCacheDirectory = "";
+
     /**
      * Get the number of decimals to use for amount values
      *
@@ -313,6 +321,37 @@ class ZugferdSettings
         ZugferdSettings::addSpecialDecimalPlacesMap('/rsm:CrossIndustryInvoice/rsm:SupplyChainTradeTransaction/ram:IncludedSupplyChainTradeLineItem/ram:SpecifiedLineTradeAgreement/ram:NetPriceProductTradePrice/ram:ChargeAmount', $amountDecimals);
     }
 
+    /**
+     * Set the cache directory for the internal serializer
+     *
+     * @param  string $serializerCacheDirectoty
+     * @return void
+     */
+    public static function setSerializerCacheDirectory(string $serializerCacheDirectoty): void
+    {
+        static::$serializerCacheDirectory = $serializerCacheDirectoty;
+    }
+
+    /**
+     * Returns the cache directory for the internal serializer. This might be empty
+     *
+     * @return string
+     */
+    public static function getSerializerCacheDirectory(): string
+    {
+        return static::$serializerCacheDirectory;
+    }
+
+    /**
+     * Returns true if a cache directory for the internal serializer is configured, otherwise false
+     *
+     * @return boolean
+     */
+    public static function hasSerializerCacheDirectory(): bool
+    {
+        return StringUtils::stringIsNullOrEmpty(static::$serializerCacheDirectory) === false;
+    }
+
     /**
      * Get root directory
      *


Test patch:
diff --git a/tests/testcases/SettingsTest.php b/tests/testcases/SettingsTest.php
index d2ff566..0382145 100644
--- a/tests/testcases/SettingsTest.php
+++ b/tests/testcases/SettingsTest.php
@@ -189,4 +189,20 @@ class SettingsTest extends TestCase
         $this->assertSame(6, ZugferdSettings::getSpecialDecimalPlacesMap('/rsm:CrossIndustryInvoice/rsm:SupplyChainTradeTransaction/ram:IncludedSupplyChainTradeLineItem/ram:SpecifiedLineTradeAgreement/ram:GrossPriceProductTradePrice/ram:ChargeAmount', 2));
         $this->assertSame(6, ZugferdSettings::getSpecialDecimalPlacesMap('/rsm:CrossIndustryInvoice/rsm:SupplyChainTradeTransaction/ram:IncludedSupplyChainTradeLineItem/ram:SpecifiedLineTradeAgreement/ram:NetPriceProductTradePrice/ram:ChargeAmount', 2));
     }
+
+    public function testSerializerCacheDirectory(): void
+    {
+        $this->assertSame('', ZugferdSettings::getSerializerCacheDirectory());
+        $this->assertFalse(ZugferdSettings::hasSerializerCacheDirectory());
+
+        ZugferdSettings::setSerializerCacheDirectory(sys_get_temp_dir());
+
+        $this->assertSame(sys_get_temp_dir(), ZugferdSettings::getSerializerCacheDirectory());
+        $this->assertTrue(ZugferdSettings::hasSerializerCacheDirectory());
+
+        ZugferdSettings::setSerializerCacheDirectory('');
+
+        $this->assertSame('', ZugferdSettings::getSerializerCacheDirectory());
+        $this->assertFalse(ZugferdSettings::hasSerializerCacheDirectory());
+    }
 }