mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-13 15:07:49 +00:00
feat: add Liquid filters 'sample', 'days_ago'
This commit is contained in:
parent
2eee024b36
commit
f38ac778f1
5 changed files with 117 additions and 2 deletions
|
|
@ -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)];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
25
app/Liquid/Filters/Date.php
Normal file
25
app/Liquid/Filters/Date.php
Normal 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();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue