From 6d36f57d35451df9f40e324a04237f85346155fa Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 25 Nov 2023 12:38:18 +1100 Subject: [PATCH 1/2] Working on export decoration --- app/Export/CSV/PaymentExport.php | 16 ++-- app/Export/Decorators/Decorator.php | 100 +++++++++++++++++---- app/Export/Decorators/PaymentDecorator.php | 5 +- 3 files changed, 95 insertions(+), 26 deletions(-) diff --git a/app/Export/CSV/PaymentExport.php b/app/Export/CSV/PaymentExport.php index c92fc5983f34..425375295821 100644 --- a/app/Export/CSV/PaymentExport.php +++ b/app/Export/CSV/PaymentExport.php @@ -11,14 +11,15 @@ namespace App\Export\CSV; -use App\Libraries\MultiDB; +use App\Utils\Ninja; +use League\Csv\Writer; use App\Models\Company; use App\Models\Payment; -use App\Transformers\PaymentTransformer; -use App\Utils\Ninja; -use Illuminate\Database\Eloquent\Builder; +use App\Libraries\MultiDB; use Illuminate\Support\Facades\App; -use League\Csv\Writer; +use App\Transformers\PaymentTransformer; +use Illuminate\Database\Eloquent\Builder; +use App\Export\Decorators\Decorator; class PaymentExport extends BaseExport { @@ -28,13 +29,16 @@ class PaymentExport extends BaseExport public Writer $csv; + private Decorator $decorator; + public function __construct(Company $company, array $input) { $this->company = $company; $this->input = $input; $this->entity_transformer = new PaymentTransformer(); + $this->decorator = new Decorator(); } - + private function init(): Builder { diff --git a/app/Export/Decorators/Decorator.php b/app/Export/Decorators/Decorator.php index 0a55e60ed149..970151dae739 100644 --- a/app/Export/Decorators/Decorator.php +++ b/app/Export/Decorators/Decorator.php @@ -24,28 +24,90 @@ use App\Models\Product; use App\Models\Project; use App\Models\PurchaseOrder; use App\Models\RecurringInvoice; +use App\Export\Decorators\DecoratorInterface; -class Decorator { +class Decorator implements DecoratorInterface{ - public function __invoke(mixed $entity, string $key) + public $entity; + + public function __construct() { - return match($entity){ - ($entity instanceof Client) => $value = (new ClientDecorator($entity, $key))->transform(), - ($entity instanceof Payment) => $value = (new PaymentDecorator($entity, $key))->transform(), - ($entity instanceof Invoice) => $value = (new InvoiceDecorator($entity, $key))->transform(), - ($entity instanceof RecurringInvoice) => $value = (new RecurringInvoiceDecorator($entity, $key))->transform(), - ($entity instanceof Credit) => $value = (new CreditDecorator($entity, $key))->transform(), - ($entity instanceof Quote) => $value = (new QuoteDecorator($entity, $key))->transform(), - ($entity instanceof Task) => $value = (new TaskDecorator($entity, $key))->transform(), - ($entity instanceof Expense) => $value = (new ExpenseDecorator($entity, $key))->transform(), - ($entity instanceof Project) => $value = (new ProjectDecorator($entity, $key))->transform(), - ($entity instanceof Product) => $value = (new ProductDecorator($entity, $key))->transform(), - ($entity instanceof Vendor) => $value = (new VendorDecorator($entity, $key))->transform(), - ($entity instanceof PurchaseOrder) => $value = (new PurchaseOrderDecorator($entity, $key))->transform(), - default => $value = '', - }; - - return $value; } + public function transform(): string + { + return 'Decorator'; + } + + public function invoice(): InvoiceDecorator + { + return new InvoiceDecorator(); + } + + public function client(): ClientDecorator + { + return new ClientDecorator(); + } + + public function payment(): PaymentDecorator + { + return new PaymentDecorator(); + } + + public function credit(): CreditDecorator + { + return new CreditDecorator(); + } + + public function vendor(): VendorDecorator + { + return new VendorDecorator(); + } + + public function expense(): ExpenseDecorator + { + return new ExpenseDecorator(); + } + + public function product(): ProductDecorator + { + return new ProductDecorator(); + } + + public function project(): ProjectDecorator + { + return new ProjectDecorator(); + } + + public function task(): TaskDecorator + { + return new TaskDecorator(); + } + + public function quote(): QuoteDecorator + { + return new QuoteDecorator(); + } + + public function recurring_invoice(): RecurringInvoiceDecorator + { + return new RecurringInvoiceDecorator(); + } + + public function purchase_order(): PurchaseOrderDecorator + { + return new PurchaseOrderDecorator(); + } + + public function setEntity($entity): self + { + $this->entity = $entity; + + return $this; + } + + public function getEntity(): mixed + { + return $this->entity; + } } diff --git a/app/Export/Decorators/PaymentDecorator.php b/app/Export/Decorators/PaymentDecorator.php index 9a287d4a0816..ae0fea96c4e5 100644 --- a/app/Export/Decorators/PaymentDecorator.php +++ b/app/Export/Decorators/PaymentDecorator.php @@ -11,10 +11,13 @@ namespace App\Export\Decorators; -class PaymentDecorator implements DecoratorInterface{ +use App\Models\Payment; + +class PaymentDecorator extends Decorator implements DecoratorInterface{ public function transform(): string { return 'Payment Decorator'; } + } \ No newline at end of file From c35716f26e84dca26520d66200806947c7c9452a Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 25 Nov 2023 12:45:17 +1100 Subject: [PATCH 2/2] Working on export decoration --- app/Export/CSV/PaymentExport.php | 2 ++ app/Export/Decorators/Decorator.php | 9 ++++++++- app/Export/Decorators/DecoratorInterface.php | 2 +- app/Export/Decorators/PaymentDecorator.php | 9 ++++++++- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/app/Export/CSV/PaymentExport.php b/app/Export/CSV/PaymentExport.php index 425375295821..86ee229bfd5f 100644 --- a/app/Export/CSV/PaymentExport.php +++ b/app/Export/CSV/PaymentExport.php @@ -117,6 +117,8 @@ class PaymentExport extends BaseExport } elseif (array_key_exists($key, $transformed_entity)) { $entity[$key] = $transformed_entity[$key]; } else { + + // $entity[$key] = $this->decorator->transform($key, $payment); $entity[$key] = $this->resolveKey($key, $payment, $this->entity_transformer); } diff --git a/app/Export/Decorators/Decorator.php b/app/Export/Decorators/Decorator.php index 970151dae739..fd8918fe7cd0 100644 --- a/app/Export/Decorators/Decorator.php +++ b/app/Export/Decorators/Decorator.php @@ -34,7 +34,7 @@ class Decorator implements DecoratorInterface{ { } - public function transform(): string + public function transform(string $key, mixed $entity): string { return 'Decorator'; } @@ -110,4 +110,11 @@ class Decorator implements DecoratorInterface{ { return $this->entity; } + + public function getKeyPart(int $index, string $key): string + { + $parts = explode('.', $key ?? ''); + + return $parts[$index]; + } } diff --git a/app/Export/Decorators/DecoratorInterface.php b/app/Export/Decorators/DecoratorInterface.php index a19b231f8fda..60225d257c8d 100644 --- a/app/Export/Decorators/DecoratorInterface.php +++ b/app/Export/Decorators/DecoratorInterface.php @@ -12,5 +12,5 @@ namespace App\Export\Decorators; interface DecoratorInterface { - public function transform(): string; + public function transform(string $key, mixed $entity): string; } diff --git a/app/Export/Decorators/PaymentDecorator.php b/app/Export/Decorators/PaymentDecorator.php index ae0fea96c4e5..82f619fdd0f4 100644 --- a/app/Export/Decorators/PaymentDecorator.php +++ b/app/Export/Decorators/PaymentDecorator.php @@ -15,9 +15,16 @@ use App\Models\Payment; class PaymentDecorator extends Decorator implements DecoratorInterface{ - public function transform(): string + private $key = 'payment'; + + public function transform(string $key, $payment): string { + $index = $this->getKeyPart(0,$key); + + // match($index) return 'Payment Decorator'; } + + } \ No newline at end of file