Skip to content

Commit

Permalink
Change the formatter method
Browse files Browse the repository at this point in the history
  • Loading branch information
tahapaksu committed Mar 1, 2021
1 parent 9d9f022 commit a3b1252
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/Contracts/CellFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Paksuco\Table\Contracts;

interface CellFormatter
{
public static function format($field, $row);
}
12 changes: 7 additions & 5 deletions src/Formatters/CheckboxFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Paksuco\Table\Formatters;

class CheckboxFormatter
use Paksuco\Table\Contracts\CellFormatter;

class CheckboxFormatter implements CellFormatter
{
public static function format($value)
public static function format($field, $row)
{
if (!!$value) {
return "<i class='fa fa-check text-green-400'></i>";
if (!!$row[$field]) {
return "<i class='text-green-400 fa fa-check'></i>";
}
return "<i class='fa fa-times text-red-400'></i>";
return "<i class='text-red-400 fa fa-times'></i>";
}
}
14 changes: 14 additions & 0 deletions src/Formatters/CurrencyFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Paksuco\Table\Formatters;

use Paksuco\Table\Contracts\CellFormatter;
use Paksuco\Currency\Facades\Currency;

class CurrencyFormatter implements CellFormatter
{
public static function format($field, $row)
{
return Currency::find($row["{$field}_currency_id"])->format($row[$field]);
}
}
8 changes: 5 additions & 3 deletions src/Formatters/SerializedFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Paksuco\Table\Formatters;

class SerializedFormatter
use Paksuco\Table\Contracts\CellFormatter;

class SerializedFormatter implements CellFormatter
{
public static function format($value)
public static function format($field, $row)
{
return "<pre class='break-words whitespace-pre-wrap' style='word-break: break-word'>" . var_export(json_decode($value), true) . "</pre>";
return "<pre class='break-words whitespace-pre-wrap' style='word-break: break-word'>" . var_export(json_decode($row[$field]), true) . "</pre>";
}
}
7 changes: 5 additions & 2 deletions src/Formatters/StringFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace Paksuco\Table\Formatters;

use Illuminate\Support\Facades\App;
use Paksuco\Table\Contracts\CellFormatter;

class StringFormatter
class StringFormatter implements CellFormatter
{
public static function format($value)
public static function format($field, $row)
{
$value = $row[$field];

if (substr($value, 0, 1) == "{") {
$json = json_decode(stripslashes($value), true);
if (is_array($json)) {
Expand Down
4 changes: 2 additions & 2 deletions views/components/rows.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@if($field["type"] == "field")
@php $formatter = "\\Paksuco\\Table\\Formatters\\" . ucfirst($field["format"]). "Formatter"; @endphp
@if(class_exists($formatter))
-->{!! $formatter::format($row[$field["name"]]) !!}<!--
-->{!! $formatter::format($field["name"], $row) !!}<!--
@else
-->{{$row[$field["name"]]}}<!--
@endif
Expand All @@ -50,7 +50,7 @@
@if($field["type"] == "field")
@php $formatter = "Paksuco\\Table\\Formatters\\" . $field["format"]. "Formatter"; @endphp
@if(class_exists($formatter))
-->{!! $formatter::format($row[$field["name"]]) !!}<!--
-->{!! $formatter::format($field["name"] ,$row) !!}<!--
@else
-->{{$row[$field["name"]]}}<!--
@endif
Expand Down

0 comments on commit a3b1252

Please sign in to comment.