Skip to content

Instantly share code, notes, and snippets.

@filhodanuvem
Created October 13, 2011 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filhodanuvem/1283977 to your computer and use it in GitHub Desktop.
Save filhodanuvem/1283977 to your computer and use it in GitHub Desktop.
Trying add templates in TwitterExcept
<?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'));
}
}
<?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