Skip to content
This repository has been archived by the owner on Mar 20, 2022. It is now read-only.

Commit

Permalink
Merge pull request #16 from kennyhorna/feature/bard-support-for-multi…
Browse files Browse the repository at this point in the history
…ple-fields-in-sets

Adds support for sets with multiple variables in Bard (Fixes #15)
  • Loading branch information
edalzell authored Oct 18, 2020
2 parents 8bc1296 + 8e0d3da commit 7c5b4b5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ The package will automatically register itself.
@endbard
```

If a set has multiple fields, they will be included in `$set['content']` as an array.

```blade
@bard($entry['content'])
@if($entry['type'] === 'image')
<img src="{{ $set['content']['src'] }}" alt="{{ $set['content']['alt'] }}">
@endif
@endbard
```

### Collection

```blade
Expand Down
15 changes: 12 additions & 3 deletions src/Directives/Bard.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ private function convertFromValueToArray(array $set)
{
return [
'type' => Arr::get($set, 'type'),
'content' => $this->getFirstValueAsRaw($set),
'content' => $this->getValuesAsRaw($set),
];
}

private function getFirstValueAsRaw($set)
private function getValuesAsRaw($set)
{
return Arr::first($set, fn ($value) => $value instanceof Value)->raw();
$values = collect($set)->filter(fn ($value) => $value instanceof Value);
if ($values->count() === 1) {
return $values
->first()
->raw();
}

return $values
->mapWithKeys(fn ($value, $key) => [$key => $value->raw()])
->toArray();
}
}
9 changes: 9 additions & 0 deletions tests/Unit/DirectivesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,13 @@ public function does_display_bard_correctly()

$this->assertSame($expected, $this->blade->compileString($blade));
}

/** @test */
public function does_display_bard_with_many_fields_in_set_correctly()
{
$blade = "@bard([['type' => 'image', 'content' => ['src' => 'an-image-url', 'alt' => 'An alternative text']]])";
$expected = "<?php foreach(Facades\Edalzell\Blade\Directives\Bard::handle([['type' => 'image', 'content' => ['src' => 'an-image-url', 'alt' => 'An alternative text']]]) as \$set) { ?>";

$this->assertSame($expected, $this->blade->compileString($blade));
}
}

0 comments on commit 7c5b4b5

Please sign in to comment.