Update transformation lib structure

This commit is contained in:
Matej Bačo
2025-02-14 10:01:27 +01:00
parent 81bfa5c48d
commit 75320db428
6 changed files with 120 additions and 25 deletions
+5 -3
View File
@@ -450,9 +450,11 @@ function router(App $utopia, Database $dbForPlatform, callable $getProjectDB, Sw
// Branded banner for previews
$transformation = new Transformation(new Preview($executionResponse['body']));
if ($type === 'deployment' && $transformation->isValid($executionResponse['headers'])) {
$transformation->transform();
$transformation = new Transformation();
$transformation->addAdapter(new Preview());
$transformation->setInput($executionResponse['body']);
$transformation->setTraits($executionResponse['headers']);
if ($type === 'deployment' && $transformation->transform()) {
$executionResponse['body'] = $transformation->getOutput();
foreach ($executionResponse['headers'] as $key => $value) {
+14 -7
View File
@@ -4,22 +4,29 @@ namespace Appwrite\Transformation;
abstract class Adapter
{
protected mixed $input;
protected mixed $output;
public function __construct(protected mixed $input)
public function __construct()
{
}
public function setInput(mixed $input): self
{
$this->input = $input;
return $this;
}
public function getOutput(): mixed
{
return $this->output;
}
/**
* @param array<mixed> $traits
*/
abstract public function isValid(array $traits): bool;
abstract public function transform(): bool;
public function getOutput(): mixed
{
return $this->output;
}
abstract public function transform(): void;
}
@@ -0,0 +1,26 @@
<?php
namespace Appwrite\Transformation\Adapter;
use Appwrite\Transformation\Adapter;
class Mock extends Adapter
{
/**
* @param array<mixed> $traits Mock traits
*/
public function isValid(array $traits): bool
{
if ($traits['mock'] === true) {
return true;
}
return false;
}
public function transform(): void
{
$this->output = $this->input;
$this->output = "Mock: " . $this->output;
}
}
@@ -27,7 +27,7 @@ class Preview extends Adapter
return false;
}
public function transform(): bool
public function transform(): void
{
$this->output = $this->input;
@@ -197,7 +197,5 @@ class Preview extends Adapter
EOT;
$this->output .= $banner;
return true;
}
}
+51 -5
View File
@@ -4,25 +4,71 @@ namespace Appwrite\Transformation;
class Transformation
{
public function __construct(protected Adapter $adapter)
/**
* @var array<Adapter> $adapters
*/
protected array $adapters;
/**
* @var array<mixed> $traits
*/
protected array $traits;
protected mixed $input;
protected mixed $output;
public function __construct(array $adapters = [])
{
$this->adapters = $adapters;
}
/**
* @param array<mixed> $traits
*/
public function isValid(array $traits): bool
public function setTraits(array $traits): self
{
return $this->adapter->isValid($traits);
$this->traits = $traits;
return $this;
}
public function setInput(mixed $input): self
{
$this->input = $input;
return $this;
}
public function addAdapter(Adapter $adapter): self
{
$this->adapters[] = $adapter;
return $this;
}
/**
* @param array<mixed> $traits
*/
public function transform(): bool
{
return $this->adapter->transform();
foreach ($this->adapters as $adapter) {
if (!$adapter->isValid($this->traits)) {
return false;
}
}
$output = $this->input;
foreach ($this->adapters as $adapter) {
$adapter->setInput($output);
$adapter->transform();
$output = $adapter->getOutput();
}
$this->output = $output;
return true;
}
public function getOutput(): mixed
{
return $this->adapter->getOutput();
return $this->output;
}
}
@@ -2,6 +2,7 @@
namespace Tests\Unit\Transformation;
use Appwrite\Transformation\Adapter\Mock;
use Appwrite\Transformation\Adapter\Preview;
use Appwrite\Transformation\Transformation;
use PHPUnit\Framework\TestCase;
@@ -10,18 +11,33 @@ class TransformationTest extends TestCase
{
public function testPreview(): void
{
$transformer = new Transformation(new Preview('Hello world'));
$input = "Hello world";
$this->assertFalse($transformer->isValid([]));
$this->assertFalse($transformer->isValid(['content-type' => 'text/plain']));
$this->assertFalse($transformer->isValid(['content-type' => 'tExT/HtML']));
$this->assertTrue($transformer->isValid(['content-type' => 'text/html']));
$this->assertTrue($transformer->isValid(['content-TYPE' => 'text/html']));
$this->assertTrue($transformer->isValid(['content-TYPE' => 'text/plain, text/html; charset=utf-8']));
$transformer = new Transformation([new Preview()]);
$transformer->addAdapter(new Mock());
$transformer->setInput($input);
$transformer->setTraits([]);
$this->assertFalse($transformer->transform());
$transformer->setTraits(['mock' => true]);
$this->assertFalse($transformer->transform());
$transformer->setTraits(['mock' => true, 'content-type' => 'text/plain']);
$this->assertFalse($transformer->transform());
$transformer->setTraits(['mock' => true, 'content-type' => 'tExT/HtML']);
$this->assertFalse($transformer->transform());
$transformer->setTraits(['mock' => false, 'content-type' => 'text/plain, text/html; charset=utf-8']);
$this->assertFalse($transformer->transform());
$transformer->setTraits(['mock' => true, 'content-type' => 'text/plain, text/html; charset=utf-8']);
$this->assertTrue($transformer->transform());
$this->assertStringContainsString("Hello world", $transformer->getOutput());
$this->assertStringContainsString("Preview by", $transformer->getOutput());
$this->assertStringContainsString("Mock:", $transformer->getOutput());
}
}