Custom Filters

Filters must implement Centum\Interfaces\Filter\FilterInterface.

Filters only require the following methods:

  • public function filter(mixed $value): mixed

As an example, a Filter can be made to return a string as lowercase:

namespace App\Filters;

use Centum\Interfaces\Filter\FilterInterface;

class LowercaseFilter implements FilterInterface
{
    public function filter(mixed $value): mixed
    {
        return strtolower($value);
    }
}

(see also Centum\Filter\String\ToLower).

More complex Filters can be made by injecting other objects into the filter:

namespace App\Filters;

use Centum\Interfaces\Filter\FilterInterface;
use NumberFormatter;

class NumberFormatterFilter implements FilterInterface
{
    public function __construct(
        protected readonly NumberFormatter $numberFormatter
    ) {
    }

    public function filter(mixed $value): mixed
    {
        return $this->numberFormatter->format($value);
    }
}