Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
],
"require": {
"php": "^8.1",
"filament/filament": "^3.0",
"filament/filament": "^4.0",
"spatie/laravel-activitylog": "^4.10",
"spatie/laravel-package-tools": "^1.15.0"
},
Expand Down
4 changes: 2 additions & 2 deletions src/Actions/ActivitylogAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Closure;
use Filament\Actions\Action;
use Filament\Support\Enums\MaxWidth;
use Filament\Support\Enums\Width;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\View\View;
Expand Down Expand Up @@ -61,7 +61,7 @@ protected function setUp(): void
'enableComments' => $this->getEnableComments(),
]))
->modalHeading(__('filament-activitylog::activitylog.action.heading'))
->modalWidth(MaxWidth::ScreenSmall)
->modalWidth(Width::ScreenSmall)
->modalSubmitAction(false)
->modalCancelAction(false);
}
Expand Down
28 changes: 8 additions & 20 deletions src/AttributeTable/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,28 @@

namespace Swis\Filament\Activitylog\AttributeTable;

use Illuminate\Contracts\Support\Htmlable;
use Stringable;

/** @phpstan-consistent-constructor */
class Attribute
{
protected string $key;

/**
* @var string|Stringable
*/
protected $value;
protected string | Htmlable | Stringable $value;

/**
* @var string|Stringable|null
*/
protected $oldValue = null;
protected null | string | Htmlable | Stringable $oldValue = null;

protected string $label;

public function __construct(string $key, string | Stringable $value, string $label)
public function __construct(string $key, string | Htmlable | Stringable $value, string $label)
{
$this->key = $key;
$this->value = $value;
$this->label = $label;
}

public static function make(string $key, string | Stringable $value, string $label): static
public static function make(string $key, string | Htmlable | Stringable $value, string $label): static
{
return new static($key, $value, $label);
}
Expand All @@ -38,27 +33,20 @@ public function getKey(): string
return $this->key;
}

/**
* @return string|Stringable
*/
public function getValue(): mixed
public function getValue(): string | Htmlable | Stringable
{
return $this->value;
}

/**
* @return string|Stringable|null
*/
public function getOldValue()
public function getOldValue(): null | string | Htmlable | Stringable
{
return $this->oldValue;
}

/**
* @param string|Stringable $oldValue
* @return $this
*/
public function withOldValue($oldValue): static
public function withOldValue(null | string | Htmlable | Stringable $oldValue): static
{
$this->oldValue = $oldValue;

Expand Down
21 changes: 11 additions & 10 deletions src/AttributeTable/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Swis\Filament\Activitylog\AttributeTable;

use Closure;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Collection;
use Stringable;
use Swis\Filament\Activitylog\AttributeTable\Contracts\LabelProvider;
Expand All @@ -13,22 +14,22 @@
class Builder
{
/**
* @var array<int, array<array-key, \Closure|\Swis\Filament\Activitylog\AttributeTable\Contracts\ValueFormatter>>
* @var array<int, array<array-key, Closure|ValueFormatter>>
*/
protected array $valueFormatters = [];

/**
* @var array<array-key, \Closure|\Swis\Filament\Activitylog\AttributeTable\Contracts\ValueFormatter>|null
* @var array<array-key, Closure|ValueFormatter>|null
*/
protected ?array $sortedValueFormatters = null;

/**
* @var array<int, array<array-key, \Closure|\Swis\Filament\Activitylog\AttributeTable\Contracts\LabelProvider>>
* @var array<int, array<array-key, Closure|LabelProvider>>
*/
protected array $labelProviders = [];

/**
* @var array<array-key, \Closure|\Swis\Filament\Activitylog\AttributeTable\Contracts\LabelProvider>|null
* @var array<array-key, Closure|LabelProvider>|null
*/
protected ?array $sortedLabelProviders = null;

Expand Down Expand Up @@ -74,7 +75,7 @@ public function registerValueFormatter(Closure | ValueFormatter $valueFormatter,
/**
* Get the registered value formatters in order of priority.
*
* @return array<array-key, \Closure|\Swis\Filament\Activitylog\AttributeTable\Contracts\ValueFormatter>
* @return array<array-key, Closure|ValueFormatter>
*/
protected function getSortedValueFormatters(): array
{
Expand All @@ -98,13 +99,13 @@ protected function getSortedValueFormatters(): array
*
* @param array<string, mixed> $attributes
*/
protected function executeValueFormatter(Closure | ValueFormatter $valueFormatter, mixed $value, string $key, array $attributes, string $recordClass): null | Stringable | string
protected function executeValueFormatter(Closure | ValueFormatter $valueFormatter, mixed $value, string $key, array $attributes, string $recordClass): null | Htmlable | Stringable | string
{
if ($valueFormatter instanceof ValueFormatter) {
return $valueFormatter->formatAttributeTableValue($this, $value, $key, $attributes, $recordClass);
}

/** @var null | Stringable | string $result */
/** @var null | Htmlable | Stringable | string $result */
$result = app()->call($valueFormatter, [
'builder' => $this,
'value' => $value,
Expand All @@ -124,7 +125,7 @@ protected function executeValueFormatter(Closure | ValueFormatter $valueFormatte
*
* @param array<string, mixed> $attributes
*/
public function formatValue(mixed $value, string $key, array $attributes, string $recordClass): Stringable | string
public function formatValue(mixed $value, string $key, array $attributes, string $recordClass): Htmlable | Stringable | string
{
foreach ($this->getSortedValueFormatters() as $formatter) {
$formattedValue = $this->executeValueFormatter($formatter, $value, $key, $attributes, $recordClass);
Expand Down Expand Up @@ -168,7 +169,7 @@ public function registerLabelProvider(Closure | LabelProvider $labelProvider, in
/**
* Get the registered label providers in order of priority.
*
* @return array<array-key, \Closure|\Swis\Filament\Activitylog\AttributeTable\Contracts\LabelProvider>
* @return array<array-key, Closure|LabelProvider>
*/
protected function getSortedLabelProviders(): array
{
Expand Down Expand Up @@ -228,7 +229,7 @@ public function getLabel(string $key, string $recordClass): string
*
* @param array<string, mixed> $newAttributes
* @param ?array<string, mixed> $oldAttributes
* @return \Illuminate\Support\Collection<string, Attribute>
* @return Collection<string, Attribute>
*/
public function buildAttributes(string $recordClass, array $newAttributes, ?array $oldAttributes = null): Collection
{
Expand Down
3 changes: 2 additions & 1 deletion src/AttributeTable/Contracts/ValueFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Swis\Filament\Activitylog\AttributeTable\Contracts;

use Illuminate\Contracts\Support\Htmlable;
use Stringable;
use Swis\Filament\Activitylog\AttributeTable\Builder;

Expand All @@ -10,5 +11,5 @@ interface ValueFormatter
/**
* @param array<string, mixed> $attributes
*/
public function formatAttributeTableValue(Builder $builder, mixed $value, string $key, array $attributes, string $recordClass): Stringable | string | null;
public function formatAttributeTableValue(Builder $builder, mixed $value, string $key, array $attributes, string $recordClass): Htmlable | Stringable | string | null;
}
19 changes: 19 additions & 0 deletions src/AttributeTable/LabelProviders/BaseLabelProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Swis\Filament\Activitylog\AttributeTable\LabelProviders;

use Filament\Support\Components\Component;
use Swis\Filament\Activitylog\AttributeTable\Contracts\LabelProvider;

abstract class BaseLabelProvider extends Component implements LabelProvider
{
protected string $evaluationIdentifier = 'labelProvider';

public static function make(): static
{
$static = app(static::class);
$static->configure();

return $static;
}
}
3 changes: 1 addition & 2 deletions src/AttributeTable/LabelProviders/HeadlineProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace Swis\Filament\Activitylog\AttributeTable\LabelProviders;

use Illuminate\Support\Str;
use Swis\Filament\Activitylog\AttributeTable\Contracts\LabelProvider;

class HeadlineProvider implements LabelProvider
class HeadlineProvider extends BaseLabelProvider
{
public function getAttributeTableLabel(string $key, string $recordClass): ?string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Swis\Filament\Activitylog\AttributeTable\Contracts\LabelProvider;

class ModelSpecificProvider implements LabelProvider
class ModelSpecificProvider extends BaseLabelProvider
{
public function getAttributeTableLabel(string $key, string $recordClass): ?string
{
Expand Down
19 changes: 19 additions & 0 deletions src/AttributeTable/ValueFormatters/BaseValueFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Swis\Filament\Activitylog\AttributeTable\ValueFormatters;

use Filament\Support\Components\Component;
use Swis\Filament\Activitylog\AttributeTable\Contracts\ValueFormatter;

abstract class BaseValueFormatter extends Component implements ValueFormatter
{
protected string $evaluationIdentifier = 'valueFormatter';

public static function make(): static
{
$static = app(static::class);
$static->configure();

return $static;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Swis\Filament\Activitylog\AttributeTable\ValueFormatters;

use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\Relation;
use Stringable;
use Swis\Filament\Activitylog\AttributeTable\Builder;
use Swis\Filament\Activitylog\AttributeTable\Contracts\ModelRelationFinder;
use Swis\Filament\Activitylog\AttributeTable\Contracts\ValueFormatter;

class BelongsToRelationFormatter implements ValueFormatter
class BelongsToRelationFormatter extends BaseValueFormatter
{
protected ModelRelationFinder $modelRelationFinder;

Expand All @@ -19,7 +19,7 @@ public function __construct(ModelRelationFinder $modelRelationFinder)
$this->modelRelationFinder = $modelRelationFinder;
}

public function formatAttributeTableValue(Builder $builder, mixed $value, string $key, array $attributes, string $recordClass): Stringable | string | null
public function formatAttributeTableValue(Builder $builder, mixed $value, string $key, array $attributes, string $recordClass): Htmlable | Stringable | string | null
{
if (empty($value) || ! is_scalar($value)) {
return null;
Expand Down
6 changes: 3 additions & 3 deletions src/AttributeTable/ValueFormatters/BoolFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Swis\Filament\Activitylog\AttributeTable\ValueFormatters;

use Illuminate\Contracts\Support\Htmlable;
use Stringable;
use Swis\Filament\Activitylog\AttributeTable\Builder;
use Swis\Filament\Activitylog\AttributeTable\Contracts\ValueFormatter;

class BoolFormatter implements ValueFormatter
class BoolFormatter extends BaseValueFormatter
{
public function formatAttributeTableValue(Builder $builder, mixed $value, string $key, array $attributes, string $recordClass): Stringable | string | null
public function formatAttributeTableValue(Builder $builder, mixed $value, string $key, array $attributes, string $recordClass): Htmlable | Stringable | string | null
{
if (! is_bool($value)) {
return null;
Expand Down
26 changes: 20 additions & 6 deletions src/AttributeTable/ValueFormatters/DateCastFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@
namespace Swis\Filament\Activitylog\AttributeTable\ValueFormatters;

use Carbon\CarbonImmutable;
use Closure;
use DateTimeInterface;
use Filament\Tables\Table;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;
use Stringable;
use Swis\Filament\Activitylog\AttributeTable\Builder;
use Swis\Filament\Activitylog\AttributeTable\Contracts\ValueFormatter;

class DateCastFormatter implements ValueFormatter
class DateCastFormatter extends BaseValueFormatter
{
public function formatAttributeTableValue(Builder $builder, mixed $value, string $key, array $attributes, string $recordClass): Stringable | string | null
protected string | Closure $dateDisplayFormat = 'M j, Y';

public function dateDisplayFormat(string | Closure $format): static
{
$this->dateDisplayFormat = $format;

return $this;
}

public function getDateDisplayFormat(): string
{
return $this->evaluate($this->dateDisplayFormat);
}

public function formatAttributeTableValue(Builder $builder, mixed $value, string $key, array $attributes, string $recordClass): Htmlable | Stringable | string | null
{
if (! is_a($recordClass, Model::class, true)) {
return null;
Expand All @@ -29,11 +43,11 @@ public function formatAttributeTableValue(Builder $builder, mixed $value, string
}

if ($value instanceof DateTimeInterface) {
return CarbonImmutable::instance($value)->format(Table::$defaultDateDisplayFormat);
return CarbonImmutable::instance($value)->format($this->getDateDisplayFormat());
}

if (is_string($value) || is_int($value) || is_float($value)) {
return CarbonImmutable::parse($value)->format(Table::$defaultDateDisplayFormat);
return CarbonImmutable::parse($value)->format($this->getDateDisplayFormat());
}

return null;
Expand Down
Loading