Skip to content

Commit

Permalink
Merge pull request #20 from keepsuit/drop-to-array
Browse files Browse the repository at this point in the history
Added toArray method to drop
  • Loading branch information
cappuc authored May 10, 2024
2 parents 4da9449 + a0900ad commit 32f2288
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Drop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions tests/Integration/DropTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down

0 comments on commit 32f2288

Please sign in to comment.