方便的模型內消毒

在基礎模型中設定便捷方法

namespace Base;

class Model extends \Phalcon\Mvc\Model
{
    public function sanitize($attr, $filterName)
    {
        $filter = $this->getDI()->get('filter');
        $this->$attr = $filter->sanitize($this->$attr, $filterName);
    }
}

然後像這樣使用

class User extends \Base\Model
{
    public function beforeValidation()
    {
        $this->sanitize('id', 'int');
        // input $this->id: 123abc
        // output: 123

        $this->sanitize('email', 'email');
        // input $this->email: youre(-)mail@dom/ain.com
        // output: youremail@domain.com
        
        $this->sanitize('wage', 'float');
        // input $this->wage: +1234ab.56cd
        // output: 1234.56

        $this->sanitize('name', 'string');
        // input $this->name: <john>
        // output: john
    }
}