Created
October 13, 2011 11:02
-
-
Save filhodanuvem/1283977 to your computer and use it in GitHub Desktop.
Trying add templates in TwitterExcept
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Respect\Validation\Rules; | |
use Respect\Validation\Exceptions\ComponentException; | |
class Twitter extends AbstractRule | |
{ | |
private $pattern = '/^@[a-zA-Z0-9]/'; | |
private $curl = NULL; | |
public function validate($input){ | |
if(!preg_match('#^\S+$#', $input)) | |
return false; | |
if(!preg_match($this->pattern,$input)) | |
return false; | |
$this->curl = curl_init('http://api.twitter.com/1/users/show.json?screen_name='.substr($input,1)); | |
curl_setopt($this->curl,CURLOPT_HTTPGET,1); | |
curl_setopt($this->curl, CURLOPT_HEADER, 0); | |
curl_setopt($this->curl,CURLOPT_RETURNTRANSFER,1); | |
$account = json_decode(curl_exec($this->curl)); | |
return (property_exists($account,'id')); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Respect\Validation\Exceptions; | |
class TwitterException extends ValidationException | |
{ | |
const CONNECTION = 0; | |
public static $defaultTemplates = array( | |
self::MODE_DEFAULT => array( | |
self::STANDARD => '{{name}} must be a Twitter account', | |
self::CONNECTION => 'Failed connection with account {{name}}', | |
), | |
self::MODE_NEGATIVE => array( | |
self::STANDARD => '{{name}} must be a Twitter account', | |
self::CONNECTION => 'Failed connection with account {{name}}', | |
) | |
); | |
public function configure(){ /* how it works ?*/ } | |
public function chooseTemplate(){ | |
if(is_null($this->getParam('account'))) | |
return self::CONNECTION; | |
return self::STANDARD; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment