From a0900add8eb3ec04a228d64956c12f09bbd20edb Mon Sep 17 00:00:00 2001 From: Fabio Capucci Date: Fri, 10 May 2024 13:03:29 +0200 Subject: [PATCH] added toArray method to drop --- src/Drop.php | 15 +++++++++++++++ tests/Integration/DropTest.php | 13 +++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Drop.php b/src/Drop.php index 2e0c791..43ab942 100644 --- a/src/Drop.php +++ b/src/Drop.php @@ -21,6 +21,21 @@ protected function liquidMethodMissing(string $name): mixed throw new UndefinedDropMethodException($name); } + public function toArray(): array + { + $result = []; + + foreach ($this->getMetadata()->properties as $property) { + $result[$property] = $this->{$property}; + } + + foreach ($this->getMetadata()->invokableMethods as $method) { + $result[$method] = $this->{$method}; + } + + return $result; + } + public function __toString(): string { return get_class($this); diff --git a/tests/Integration/DropTest.php b/tests/Integration/DropTest.php index 87f0fa8..31288df 100644 --- a/tests/Integration/DropTest.php +++ b/tests/Integration/DropTest.php @@ -107,6 +107,19 @@ expect(renderTemplate('{{ collection }}', ['collection' => new EnumerableDrop()]))->toBe(EnumerableDrop::class); }); +test('drop toArray', function () { + $context = (new \Keepsuit\Liquid\TemplateFactory())->newRenderContext(); + + $drop = new ProductDrop(); + $drop->setContext($context); + expect($drop->toArray()) + ->toHaveKeys(['productName', 'text', 'catchAll', 'context']) + ->productName->toBe('Product') + ->text->toBeInstanceOf(\Keepsuit\Liquid\Tests\Stubs\TextDrop::class) + ->catchAll->toBeInstanceOf(\Keepsuit\Liquid\Tests\Stubs\CatchAllDrop::class) + ->context->toBe($context); +}); + test('drop metadata', function () { expect(invade(new ProductDrop())->getMetadata()) ->invokableMethods->toBe(['text', 'catchAll', 'context'])