Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Here's the base sanitizer:

<?php

namespace FooProject\Internal\Sanitizers;

abstract class BaseSanitizer
{
    /**
     * An array of sanitizer methods to be
     * executed.
     *
     * @var array
     */
    protected $sanitizers = [];

    /**
     * Trigger the sanitization process by
     * iterating the sanitizers array and
     * mutating our data array.
     *
     * @param  array $data
     * @return array
     */
    public function sanitize($data)
    {
        // Iterate all of the sanitizer methods.
        foreach ($this->sanitizers as $sanitizer) {
            $method = 'sanitize'.$sanitizer;
            // If the sanitization method exists, call it
            // to mutate our data set.
            if (method_exists($this, $method)) {
                $data = call_user_func([$this, $method], $data);
            }
        }
        return $data;
    }
}

and here's an example:

<?php

namespace FooProject\Internal\Sanitizers;

class UsersSanitizer extends BaseSanitizer
{
    /**
     * An array of sanitizer methods to be
     * executed.
     *
     * @var array
     */
    protected $sanitizers = ['Email'];

    /**
     * Sanitize an email address.
     *
     * @param  array $data
     * @return FooProject\Internal\Sanitizer\UserSanitizer
     */
    public function sanitizeEmail($data)
    {
        if (isset($data['email'])) {
            $data['email'] = strtolower($data['email']);
        }
        return $this;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment