Skip to content

Instantly share code, notes, and snippets.

@JesseObrien
Forked from daylerees/gist:8539215
Created January 21, 2014 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JesseObrien/8539457 to your computer and use it in GitHub Desktop.
Save JesseObrien/8539457 to your computer and use it in GitHub Desktop.

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