View Single Post
Old 05-17-2009, 05:01 PM   #4 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Building upon Salathe's idea, I came up with the following which may well work:

php Code:
class TalkPHP_Check
{
    const NOT_EMPTY = '_isNotEmpty';
    const IS_EMAIL = '_isEmail';
   
    public function isValid($szVariable)
    {
        $bValidated = false;
        $aArgs = func_get_args();
        array_shift($aArgs);
       
        foreach ($aArgs as $szFunction)
        {
            if (!method_exists($this, $szFunction))
            {
                throw new Exception(sprintf('Method does not exist: %s', $szFunction));
            }
           
            $bValidated = $this->$szFunction($szVariable);
           
            if (!$bValidated)
            {
                return false;
            }
        }
       
        return true;
    }
   
    private function _isNotEmpty($szVariable)
    {
        if (empty($szVariable))
        {
            return false;
        }
       
        return true;
    }
   
    private function _isEmail($szVariable)
    {
        if (!preg_match('~^.+?@.+?\..+?$~i', $szVariable))
        {
            return false;
        }
       
        return true;
    }
}

Then of course to use that you would simply do:

php Code:
$szEmail = 'adam@example.com';
$pCheck = new TalkPHP_Check();
$bResult = $pCheck->isValid($szEmail, TalkPHP_Check::NOT_EMPTY, TalkPHP_Check::IS_EMAIL);
var_dump($bResult);

If you want me to explain the code, please ask! I don't know if it's understandale or not, as it is.
Attached Files
File Type: php Validation.php (1,011 Bytes, 137 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote