-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from pxlrbt/feature/ignore-formatting
Ignore formatting
- Loading branch information
Showing
9 changed files
with
239 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace pxlrbt\FilamentExcel\Exports\Concerns; | ||
|
||
use Closure; | ||
use pxlrbt\FilamentExcel\Columns\Column; | ||
|
||
trait CanIgnoreFormatting | ||
{ | ||
public Closure|array|bool $ignoreFormattingOnColumns = false; | ||
|
||
public function ignoreFormatting(Closure|array|bool $columns = true): static | ||
{ | ||
$this->ignoreFormattingOnColumns = $columns; | ||
|
||
return $this; | ||
} | ||
|
||
protected function shouldIgnoreFormattingForColumn(Column $column): bool | ||
{ | ||
$shouldIgnore = $this->evaluate($this->ignoreFormattingOnColumns, [ | ||
'column' => $column, | ||
]); | ||
|
||
if (is_bool($shouldIgnore)) { | ||
return $shouldIgnore; | ||
} | ||
|
||
return in_array($column->getName(), $shouldIgnore); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace pxlrbt\FilamentExcel\Exports\Formatters; | ||
|
||
class ArrayFormatter implements FormatterInterface | ||
{ | ||
public function __construct( | ||
public string $delimiter = ',' | ||
) | ||
{ | ||
// | ||
} | ||
|
||
public function shouldApply($state): bool | ||
{ | ||
return is_array($state); | ||
} | ||
|
||
public function format($state): string | ||
{ | ||
return implode( | ||
$this->delimiter, | ||
array_map(fn ($value) => app(Formatter::class)->format($value), $state) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace pxlrbt\FilamentExcel\Exports\Formatters; | ||
|
||
use UnitEnum; | ||
|
||
class EnumFormatter implements FormatterInterface | ||
{ | ||
public function shouldApply($state): bool | ||
{ | ||
return function_exists('enum_exists') && $state instanceof UnitEnum; | ||
} | ||
|
||
public function format($state): string | ||
{ | ||
return $state->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace pxlrbt\FilamentExcel\Exports\Formatters; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
class Formatter | ||
{ | ||
static protected $formatters = [ | ||
ArrayFormatter::class, | ||
EnumFormatter::class, | ||
ObjectFormatter::class, | ||
]; | ||
|
||
/** | ||
* @param mixed $state | ||
* @return mixed | ||
*/ | ||
public function format($state) | ||
{ | ||
if (is_string($state)) { | ||
return $state; | ||
} | ||
|
||
$formatter = $this->getFormatters() | ||
->firstWhere(function ($formatter) use ($state) { | ||
return $formatter->shouldApply($state); | ||
}); | ||
|
||
return $formatter?->format($state) ?? $state; | ||
} | ||
|
||
private function getFormatters(): Collection | ||
{ | ||
return collect(static::$formatters)->map( | ||
fn ($formatter) => app($formatter) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace pxlrbt\FilamentExcel\Exports\Formatters; | ||
|
||
interface FormatterInterface | ||
{ | ||
public function shouldApply($state): bool; | ||
public function format($state): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace pxlrbt\FilamentExcel\Exports\Formatters; | ||
|
||
class ObjectFormatter implements FormatterInterface | ||
{ | ||
public function shouldApply($state): bool | ||
{ | ||
return is_object($state) && method_exists($state, '__toString'); | ||
} | ||
|
||
public function format($state): string | ||
{ | ||
return $state->__toString(); | ||
} | ||
} |