View Single Post
Old 05-17-2009, 05:27 PM   #5 (permalink)
allworknoplay
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
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.

I just started re-coding my class based on Sal's suggestion. I'll take a look at yours since it builds off of his suggestion.

Quickly glancing it, I think I do get it...

allworknoplay is offline  
Reply With Quote