Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laminas Filter Laravel Package

laminas/laminas-filter

A collection of reusable data filters for PHP apps. Provides string and numeric normalization, file and HTML filters, and a plugin manager to compose filter chains. Useful for sanitizing and transforming input consistently across Laminas and other frameworks.

View on GitHub
Deep Wiki
Context7

Refactoring Custom Filters away from AbstractFilter

For this example, we will define a filter that when given a boolean, one of 2 specific, configurable values will be returned.

To begin with, here is the implementation that extended from AbstractFilter:

use Laminas\Filter\AbstractFilter;

class MyFilter extends AbstractFilter
{
    public function filter($value)
    {
        if (! is_bool($value)) {
            return $value;
        }
        
        return $value
            ? $this->getTrueValue()
            : $this->getFalseValue();
    }
    
    public function getTrueValue() {
        return $this->options['true_value']
    }
    
    public function getFalseValue() {
        return $this->options['false_value']
    }
    
    public function setTrueValue(string $value) {
        $this->options['true_value'] = $value;
    }
    
    public function setFalseValue(string $value) {
        $this->options['false_value'] = $value;
    }
}

In the above example, it is assumed that at some point, AbstractFilter::setOptions() would be called to provide options at runtime. This behaviour should no longer be relied upon and options should be passed directly to your filter constructor so that it cannot be constructed in an invalid state.

Furthermore, setters and getters for options are generally unnecessary and are typically added out of convention or a desire to unit test the option values in some way. We encourage you to drop these option setters and getters and instead test your filters by asserting expected outputs from a wide range of inputs whilst varying options to assure desired behaviour.

Here is an example of the above class refactored for laminas-filter v3:

use Laminas\Filter\FilterInterface;

final class MyFilter implements FilterInterface
{
    private readonly string $trueValue;
    private readonly string $falseValue;
    
    public function __construct(array $options)
    {
        // Validation of $options omitted for brevity
        $this->trueValue = $options['true_value'];
        $this->falseValue = $options['false_value'];
    }
    
    public function filter(mixed $value): mixed
    {
        if (! is_bool($value)) {
            return $value;
        }
        
        return $value
            ? $this->trueValue
            : $this->falseValue;
    }
    
    public function __invoke(mixed $value): mixed {
        return $this->filter($value);
    }
}
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai