auraphp / Aura.Filter

Validate and sanitize arrays and objects.
MIT License
159 stars 33 forks source link

Alternative approach without factory #85

Closed harikt closed 9 years ago

harikt commented 9 years ago

Hi all,

I would like to discuss if anyone like this idea.

Currently the way of creating the Filter is via a FilterFactory and call the newSubjectFilter .

What I feel is we could improve the usage with something like

$filter = new Aura\Filter\SubjectFilter();

and in the class it can automatically add the dependencies if not passed.

class SubjectFilter
{
    public function __construct(
        ValidateSpec $validate_spec = null,
        SanitizeSpec $sanitize_spec = null,
        FailureCollection $failures = null
    ) {
        $this->validate_spec = $validate_spec ?: new ValidateSpec(new ValidateLocator());
        $this->sanitize_spec = $sanitize_spec ?: new SanitizeSpec(new SanitizeLocator());
        $this->proto_failures = $failures ?: new FailureCollection();
        $this->init();
    }
}

This helps to use Di if they are using it and if not passing their own or alternatively without anything if there is no other custom rule dependency.

I can make a PR if the idea seems good.

Thank you.

harikt commented 9 years ago

Same applies for ValueFilter

pmjones commented 9 years ago

No, we keep object creation separate from object use.