feat: add Liquid filters 'sample', 'days_ago'

This commit is contained in:
Benjamin Nussbaum 2025-08-27 21:18:22 +02:00
parent 2eee024b36
commit f38ac778f1
5 changed files with 117 additions and 2 deletions

View file

@ -63,4 +63,19 @@ class Data extends FiltersProvider
return $grouped;
}
/**
* Return a random element from an array
*
* @param array $array The array to sample from
* @return mixed A random element from the array
*/
public function sample(array $array): mixed
{
if (empty($array)) {
return null;
}
return $array[array_rand($array)];
}
}

View file

@ -0,0 +1,25 @@
<?php
namespace App\Liquid\Filters;
use Keepsuit\Liquid\Filters\FiltersProvider;
use Carbon\Carbon;
/**
* Data filters for Liquid templates
*/
class Date extends FiltersProvider
{
/**
* Calculate a date that is a specified number of days in the past
*
* @param int|string $num The number of days to subtract
* @return string The date in Y-m-d format
*/
public function days_ago(int|string $num): string
{
$days = (int) $num;
return Carbon::now()->subDays($days)->toDateString();
}
}