Advertisement
dragonbe

filter input with validation on models

Oct 3rd, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.58 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Application_Model_User
  4.  *
  5.  * Contains the minimum requirements to identify a specific user to the system.
  6.  *
  7.  * @category    Prozf
  8.  * @package     Application_Model
  9.  *
  10.  */
  11. class Application_Model_User
  12. {
  13.     /**
  14.      * @var     int The ID of a user
  15.      */
  16.     protected $_id;
  17.     /**
  18.      * @var     string The full name of a user
  19.      */
  20.     protected $_fullname;
  21.     /**
  22.      * @var     string The email address of a user
  23.      */
  24.     protected $_email;
  25.     /**
  26.      * @var     string The password of a user
  27.      */
  28.     protected $_password;
  29.     /**
  30.      * @var     array List of filters used on the individual properties
  31.      */
  32.     protected $_filters;
  33.     /**
  34.      * @var     array List of validators used on the individual properties
  35.      */
  36.     protected $_validators;
  37.     /**
  38.      * Constructor class for this User
  39.      *
  40.      * @param   null|string|array $fullname
  41.      * @param   null|string $email
  42.      * @param   null|string $password
  43.      */
  44.     public function __construct($fullname = null, $email = null, $password = null)
  45.     {
  46.         $this->_filters = array (
  47.             'id'        => new Zend_Filter_Int(),
  48.             'fullname'  => array (
  49.                 new Zend_Filter_StringTrim(),
  50.                 new Zend_Filter_Alnum(true),
  51.                 new Zend_Filter_StripTags(),
  52.             ),
  53.             'email'     => array (
  54.                 new Zend_Filter_StringTrim(),
  55.                 new Zend_Filter_StringToLower(),
  56.                 new Zend_Filter_StripTags(),
  57.             ),
  58.         );
  59.         $this->_validators = array (
  60.             'id' => array (
  61.                 new Zend_Validate_Int(),
  62.                 new Zend_Validate_GreaterThan(-1),
  63.             ),
  64.             'fullname' => array (
  65.                 new Prozf_Validate_Mwop(),
  66.                 new Zend_Validate_StringLength(array ('min' => 4, 'max' => 50)),
  67.             ),
  68.             'email'    => array (
  69.                 'EmailAddress',
  70.                 new Zend_Validate_StringLength(array ('min' => 4, 'max' => 50)),
  71.             ),
  72.             'password' => array (
  73.                 new Prozf_Validate_PasswordStrength(
  74.                     Prozf_Validate_PasswordStrength::PASSWD_MEDIUM),
  75.                 new Zend_Validate_StringLength(array ('min' => 4)),
  76.             ),
  77.         );
  78.         if (null !== $fullname && (is_array($fullname) || $fullname instanceof Zend_Db_Table_Row)) {
  79.             $this->populate($fullname);
  80.         }
  81.         if (null !== $fullname && is_string($fullname)) {
  82.             $this->setFullname($fullname);
  83.         }
  84.         if (null !== $fullname && is_string($email)) {
  85.             $this->setEmail($email);
  86.         }
  87.         if (null !== $password && is_string($password)) {
  88.             $this->setPassword($password);
  89.         }
  90.     }
  91.     /**
  92.      * Sets the sequence ID for a User
  93.      *
  94.      * @param   int $id
  95.      * @throws  UnexpectedValueException
  96.      * @return  Application_Model_User
  97.      */
  98.     public function setId($id)
  99.     {
  100.         $input = new Zend_Filter_Input($this->_filters, $this->_validators);
  101.         $input->setData(array ('id' => $id));
  102.        
  103.         if (!$input->isValid('id')) {
  104.             throw new UnexpectedValueException('Wrong value for ID');
  105.         }
  106.         $this->_id = (int) $input->id;
  107.         return $this;
  108.     }
  109.     /**
  110.      * Retrieves the sequence ID from a User
  111.      *
  112.      * @return  int
  113.      */
  114.     public function getId()
  115.     {
  116.         return $this->_id;
  117.     }
  118.     /**
  119.      * Sets the full name for a User
  120.      *
  121.      * @param   string $fullname
  122.      * @throws  UnexpectedValueException
  123.      * @return  Application_Model_User
  124.      */
  125.     public function setFullname($fullname)
  126.     {
  127.         $input = new Zend_Filter_Input($this->_filters, $this->_validators);
  128.         $input->setData(array ('fullname' => $fullname));
  129.        
  130.         if (!$input->isValid('fullname')) {
  131.             throw new UnexpectedValueException('Wrong value for full name');
  132.         }
  133.         $this->_fullname = $input->fullname;
  134.         return $this;
  135.     }
  136.     /**
  137.      * Retrieves the full name from a User
  138.      *
  139.      * @return  string
  140.      */
  141.     public function getFullname()
  142.     {
  143.         return $this->_fullname;
  144.     }
  145.     /**
  146.      * Sets the email address for a User
  147.      *
  148.      * @param   string $email
  149.      * @throws  UnexpectedValueException
  150.      * @return  Application_Model_User
  151.      */
  152.     public function setEmail($email)
  153.     {
  154.         $input = new Zend_Filter_Input($this->_filters, $this->_validators);
  155.         $input->setData(array ('email' => $email));
  156.        
  157.         if (!$input->isValid('email')) {
  158.             throw new UnexpectedValueException('Wrong value for email');
  159.         }
  160.        
  161.         $this->_email = $input->email;
  162.         return $this;
  163.     }
  164.     /**
  165.      * Retrieves the email address from a User
  166.      *
  167.      * @return  string
  168.      */
  169.     public function getEmail()
  170.     {
  171.         return $this->_email;
  172.     }
  173.     /**
  174.      * Sets the password for a User
  175.      *
  176.      * @param   string $password
  177.      * @throws  UnexpectedValueException
  178.      * @return  Application_Model_User
  179.      */
  180.     public function setPassword($password)
  181.     {
  182.         $input = new Zend_Filter_Input($this->_filters, $this->_validators);
  183.         $input->setData(array ('password' => $password));
  184.        
  185.         if (!$input->isValid('password')) {
  186.             throw new UnexpectedValueException('Wrong value for password');
  187.         }
  188.        
  189.         $this->_password = $input->password;
  190.         return $this;
  191.     }
  192.     /**
  193.      * Retrieve the password from a User
  194.      */
  195.     public function getPassword()
  196.     {
  197.         return $this->_password;
  198.     }
  199.     /**
  200.      * Populates this User profile with an array
  201.      *
  202.      * @param   array $data
  203.      * @return  Application_Model_User
  204.      */
  205.     public function populate($data)
  206.     {
  207.         if (is_array($data)) {
  208.             $data = new ArrayObject($data, ArrayObject::ARRAY_AS_PROPS);
  209.         }
  210.         if (isset ($data->id)) $this->setId($data->id);
  211.         if (isset ($data->fullname)) $this->setFullname($data->fullname);
  212.         if (isset ($data->email)) $this->setEmail($data->email);
  213.         if (isset ($data->password)) $this->setPassword($data->password);
  214.         return $this;
  215.     }
  216.     /**
  217.      * Converts this User data into an array
  218.      *
  219.      * @return  array
  220.      */
  221.     public function toArray()
  222.     {
  223.         return array (
  224.             'id'       => $this->getId(),
  225.             'fullname' => $this->getFullname(),
  226.             'email'    => $this->getEmail(),
  227.             'password' => $this->getPassword(),
  228.         );
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement