@@ -2,153 +2,153 @@ |
||
2 | 2 | |
3 | 3 | class TwigGravatar extends \Twig_Extension |
4 | 4 | { |
5 | - public $baseUrl = "http://www.gravatar.com/"; |
|
6 | - public $httpsUrl = "https://secure.gravatar.com/"; |
|
5 | + public $baseUrl = "http://www.gravatar.com/"; |
|
6 | + public $httpsUrl = "https://secure.gravatar.com/"; |
|
7 | 7 | |
8 | - public $filterPrefix = "gr"; |
|
8 | + public $filterPrefix = "gr"; |
|
9 | 9 | |
10 | - private $filterOptions = array("is_safe" => array("html")); |
|
10 | + private $filterOptions = array("is_safe" => array("html")); |
|
11 | 11 | |
12 | - private $defaults = array( |
|
13 | - "404", "mm", "identicon", "monsterid", "wavatar", "retro", "blank" |
|
14 | - ); |
|
15 | - private $ratings = array( |
|
16 | - "g", "pg", "r", "x" |
|
17 | - ); |
|
12 | + private $defaults = array( |
|
13 | + "404", "mm", "identicon", "monsterid", "wavatar", "retro", "blank" |
|
14 | + ); |
|
15 | + private $ratings = array( |
|
16 | + "g", "pg", "r", "x" |
|
17 | + ); |
|
18 | 18 | |
19 | - /** |
|
20 | - * {@inheritdoc} |
|
21 | - */ |
|
22 | - public function getFilters() |
|
23 | - { |
|
24 | - return array( |
|
25 | - new \Twig_SimpleFilter($this->filterPrefix . 'Avatar', [$this, 'avatar'], $this->filterOptions), |
|
26 | - new \Twig_SimpleFilter($this->filterPrefix . 'Https', [$this, 'https'], $this->filterOptions), |
|
27 | - new \Twig_SimpleFilter($this->filterPrefix . 'Size', [$this, 'size'], $this->filterOptions), |
|
28 | - new \Twig_SimpleFilter($this->filterPrefix . 'Default', [$this, 'def'], $this->filterOptions), |
|
29 | - new \Twig_SimpleFilter($this->filterPrefix . 'Rating', [$this, 'rating'], $this->filterOptions) |
|
30 | - ); |
|
31 | - } |
|
19 | + /** |
|
20 | + * {@inheritdoc} |
|
21 | + */ |
|
22 | + public function getFilters() |
|
23 | + { |
|
24 | + return array( |
|
25 | + new \Twig_SimpleFilter($this->filterPrefix . 'Avatar', [$this, 'avatar'], $this->filterOptions), |
|
26 | + new \Twig_SimpleFilter($this->filterPrefix . 'Https', [$this, 'https'], $this->filterOptions), |
|
27 | + new \Twig_SimpleFilter($this->filterPrefix . 'Size', [$this, 'size'], $this->filterOptions), |
|
28 | + new \Twig_SimpleFilter($this->filterPrefix . 'Default', [$this, 'def'], $this->filterOptions), |
|
29 | + new \Twig_SimpleFilter($this->filterPrefix . 'Rating', [$this, 'rating'], $this->filterOptions) |
|
30 | + ); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Get a Gravatar Avatar URL |
|
35 | - * @param string $email Gravatar Email address |
|
36 | - * @return string Gravatar Avatar URL |
|
37 | - * @throws \InvalidArgumentException If $email is invalid |
|
38 | - */ |
|
39 | - public function avatar($email) |
|
40 | - { |
|
41 | - if (filter_var($email, FILTER_VALIDATE_EMAIL)) { |
|
42 | - return $this->baseUrl . "avatar/" . $this->generateHash($email); |
|
43 | - } else { |
|
44 | - throw new InvalidArgumentException("The avatar filter must be passed a valid Email address"); |
|
45 | - } |
|
46 | - } |
|
33 | + /** |
|
34 | + * Get a Gravatar Avatar URL |
|
35 | + * @param string $email Gravatar Email address |
|
36 | + * @return string Gravatar Avatar URL |
|
37 | + * @throws \InvalidArgumentException If $email is invalid |
|
38 | + */ |
|
39 | + public function avatar($email) |
|
40 | + { |
|
41 | + if (filter_var($email, FILTER_VALIDATE_EMAIL)) { |
|
42 | + return $this->baseUrl . "avatar/" . $this->generateHash($email); |
|
43 | + } else { |
|
44 | + throw new InvalidArgumentException("The avatar filter must be passed a valid Email address"); |
|
45 | + } |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Change a Gravatar URL to its Secure version |
|
50 | - * @param string $value URL to convert |
|
51 | - * @return string Converted URL |
|
52 | - * @throws \InvalidArgumentException If $value isn't an existing Gravatar URL |
|
53 | - */ |
|
54 | - public function https($value) |
|
55 | - { |
|
56 | - if (strpos($value, $this->baseUrl) === false) { |
|
57 | - throw new InvalidArgumentException("You can only convert existing Gravatar URLs to HTTPS"); |
|
58 | - } else { |
|
59 | - return str_replace($this->baseUrl, $this->httpsUrl, $value); |
|
60 | - } |
|
61 | - } |
|
48 | + /** |
|
49 | + * Change a Gravatar URL to its Secure version |
|
50 | + * @param string $value URL to convert |
|
51 | + * @return string Converted URL |
|
52 | + * @throws \InvalidArgumentException If $value isn't an existing Gravatar URL |
|
53 | + */ |
|
54 | + public function https($value) |
|
55 | + { |
|
56 | + if (strpos($value, $this->baseUrl) === false) { |
|
57 | + throw new InvalidArgumentException("You can only convert existing Gravatar URLs to HTTPS"); |
|
58 | + } else { |
|
59 | + return str_replace($this->baseUrl, $this->httpsUrl, $value); |
|
60 | + } |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Change the Size of a Gravatar URL |
|
65 | - * @param string $value |
|
66 | - * @param integer $px |
|
67 | - * @return string Sized Gravatar URL |
|
68 | - */ |
|
69 | - public function size($value, $px = 100) |
|
70 | - { |
|
71 | - if (!is_numeric($px) || $px < 0 || $px > 2048) { |
|
72 | - throw new InvalidArgumentException("You must pass the size filter a valid number between 0 and 2048"); |
|
73 | - } else if (strpos($value, $this->baseUrl) === false |
|
74 | - && strpos($value, $this->httpsUrl) === false |
|
75 | - ) { |
|
76 | - throw new InvalidArgumentException("You must pass the size filter an existing Gravatar URL"); |
|
77 | - } else { |
|
78 | - return $this->query($value, array("size" => $px)); |
|
79 | - } |
|
80 | - } |
|
63 | + /** |
|
64 | + * Change the Size of a Gravatar URL |
|
65 | + * @param string $value |
|
66 | + * @param integer $px |
|
67 | + * @return string Sized Gravatar URL |
|
68 | + */ |
|
69 | + public function size($value, $px = 100) |
|
70 | + { |
|
71 | + if (!is_numeric($px) || $px < 0 || $px > 2048) { |
|
72 | + throw new InvalidArgumentException("You must pass the size filter a valid number between 0 and 2048"); |
|
73 | + } else if (strpos($value, $this->baseUrl) === false |
|
74 | + && strpos($value, $this->httpsUrl) === false |
|
75 | + ) { |
|
76 | + throw new InvalidArgumentException("You must pass the size filter an existing Gravatar URL"); |
|
77 | + } else { |
|
78 | + return $this->query($value, array("size" => $px)); |
|
79 | + } |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Specify a default Image for when there is no matching Gravatar image. |
|
84 | - * @param string $value |
|
85 | - * @param string $default Defaults to Mystery Man |
|
86 | - * @param boolean $force Always load the default image |
|
87 | - * @return string Gravatar URL with a default image. |
|
88 | - */ |
|
89 | - public function def($value, $default = "mm", $force = false) |
|
90 | - { |
|
91 | - if (strpos($value, $this->baseUrl) === false && strpos($value, $this->httpsUrl) === false) { |
|
92 | - throw new InvalidArgumentException("You can only a default to existing Gravatar URLs"); |
|
93 | - } else if (!filter_var($default, FILTER_VALIDATE_URL) && !in_array($default, $this->defaults)) { |
|
94 | - throw new InvalidArgumentException("Default must be a URL or valid default"); |
|
95 | - } else if (!is_bool($force)) { |
|
96 | - throw new InvalidArgumentException("The force option for a default must be boolean"); |
|
97 | - } else { |
|
98 | - if (filter_var($default, FILTER_VALIDATE_URL)) $default = urlencode($default); |
|
99 | - $force = ($force ? "y" : "n"); |
|
100 | - return $this->query($value, array("default" => $default, "forcedefault" => $force)); |
|
101 | - } |
|
102 | - } |
|
82 | + /** |
|
83 | + * Specify a default Image for when there is no matching Gravatar image. |
|
84 | + * @param string $value |
|
85 | + * @param string $default Defaults to Mystery Man |
|
86 | + * @param boolean $force Always load the default image |
|
87 | + * @return string Gravatar URL with a default image. |
|
88 | + */ |
|
89 | + public function def($value, $default = "mm", $force = false) |
|
90 | + { |
|
91 | + if (strpos($value, $this->baseUrl) === false && strpos($value, $this->httpsUrl) === false) { |
|
92 | + throw new InvalidArgumentException("You can only a default to existing Gravatar URLs"); |
|
93 | + } else if (!filter_var($default, FILTER_VALIDATE_URL) && !in_array($default, $this->defaults)) { |
|
94 | + throw new InvalidArgumentException("Default must be a URL or valid default"); |
|
95 | + } else if (!is_bool($force)) { |
|
96 | + throw new InvalidArgumentException("The force option for a default must be boolean"); |
|
97 | + } else { |
|
98 | + if (filter_var($default, FILTER_VALIDATE_URL)) $default = urlencode($default); |
|
99 | + $force = ($force ? "y" : "n"); |
|
100 | + return $this->query($value, array("default" => $default, "forcedefault" => $force)); |
|
101 | + } |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * Specify the maximum rating for an avatar |
|
106 | - * @param string $value |
|
107 | - * @param string $rating Expects g,pg,r or x |
|
108 | - * @return string Gravatar URL with a rating specified |
|
109 | - */ |
|
110 | - public function rating($value, $rating = "g") |
|
111 | - { |
|
112 | - if (strpos($value, $this->baseUrl) === false && strpos($value, $this->httpsUrl) === false) { |
|
113 | - throw new InvalidArgumentException("You can only add a rating to an existing Gravatar URL"); |
|
114 | - } else if (!in_array(strtolower($rating), $this->ratings)) { |
|
115 | - throw new InvalidArgumentException("Rating must be g,pg,r or x"); |
|
116 | - } else { |
|
117 | - return $this->query($value, array("rating" => $rating)); |
|
118 | - } |
|
119 | - } |
|
104 | + /** |
|
105 | + * Specify the maximum rating for an avatar |
|
106 | + * @param string $value |
|
107 | + * @param string $rating Expects g,pg,r or x |
|
108 | + * @return string Gravatar URL with a rating specified |
|
109 | + */ |
|
110 | + public function rating($value, $rating = "g") |
|
111 | + { |
|
112 | + if (strpos($value, $this->baseUrl) === false && strpos($value, $this->httpsUrl) === false) { |
|
113 | + throw new InvalidArgumentException("You can only add a rating to an existing Gravatar URL"); |
|
114 | + } else if (!in_array(strtolower($rating), $this->ratings)) { |
|
115 | + throw new InvalidArgumentException("Rating must be g,pg,r or x"); |
|
116 | + } else { |
|
117 | + return $this->query($value, array("rating" => $rating)); |
|
118 | + } |
|
119 | + } |
|
120 | 120 | |
121 | 121 | |
122 | - /** |
|
123 | - * Generate the Hashed email address |
|
124 | - * @param string $email |
|
125 | - * @return string Hashed email address |
|
126 | - */ |
|
127 | - public function generateHash($email) |
|
128 | - { |
|
129 | - return md5(strtolower(trim($email))); |
|
130 | - } |
|
122 | + /** |
|
123 | + * Generate the Hashed email address |
|
124 | + * @param string $email |
|
125 | + * @return string Hashed email address |
|
126 | + */ |
|
127 | + public function generateHash($email) |
|
128 | + { |
|
129 | + return md5(strtolower(trim($email))); |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * Generate the query string |
|
134 | - * @param string $string |
|
135 | - * @param array $addition Array of what parameters to add |
|
136 | - * @return string |
|
137 | - */ |
|
138 | - private function query($string, array $addition) |
|
139 | - { |
|
140 | - foreach ($addition as $name => $value) { |
|
141 | - $string .= (strpos($string, "?") === FALSE ? "?" : "&") . $name . "=" . $value; |
|
142 | - } |
|
143 | - return $string; |
|
144 | - } |
|
132 | + /** |
|
133 | + * Generate the query string |
|
134 | + * @param string $string |
|
135 | + * @param array $addition Array of what parameters to add |
|
136 | + * @return string |
|
137 | + */ |
|
138 | + private function query($string, array $addition) |
|
139 | + { |
|
140 | + foreach ($addition as $name => $value) { |
|
141 | + $string .= (strpos($string, "?") === FALSE ? "?" : "&") . $name . "=" . $value; |
|
142 | + } |
|
143 | + return $string; |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * Get the Extension name |
|
148 | - * @return string |
|
149 | - */ |
|
150 | - public function getName() |
|
151 | - { |
|
152 | - return 'TwigGravatar'; |
|
153 | - } |
|
146 | + /** |
|
147 | + * Get the Extension name |
|
148 | + * @return string |
|
149 | + */ |
|
150 | + public function getName() |
|
151 | + { |
|
152 | + return 'TwigGravatar'; |
|
153 | + } |
|
154 | 154 | } |
155 | 155 | \ No newline at end of file |
@@ -22,11 +22,11 @@ discard block |
||
22 | 22 | public function getFilters() |
23 | 23 | { |
24 | 24 | return array( |
25 | - new \Twig_SimpleFilter($this->filterPrefix . 'Avatar', [$this, 'avatar'], $this->filterOptions), |
|
26 | - new \Twig_SimpleFilter($this->filterPrefix . 'Https', [$this, 'https'], $this->filterOptions), |
|
27 | - new \Twig_SimpleFilter($this->filterPrefix . 'Size', [$this, 'size'], $this->filterOptions), |
|
28 | - new \Twig_SimpleFilter($this->filterPrefix . 'Default', [$this, 'def'], $this->filterOptions), |
|
29 | - new \Twig_SimpleFilter($this->filterPrefix . 'Rating', [$this, 'rating'], $this->filterOptions) |
|
25 | + new \Twig_SimpleFilter($this->filterPrefix.'Avatar', [$this, 'avatar'], $this->filterOptions), |
|
26 | + new \Twig_SimpleFilter($this->filterPrefix.'Https', [$this, 'https'], $this->filterOptions), |
|
27 | + new \Twig_SimpleFilter($this->filterPrefix.'Size', [$this, 'size'], $this->filterOptions), |
|
28 | + new \Twig_SimpleFilter($this->filterPrefix.'Default', [$this, 'def'], $this->filterOptions), |
|
29 | + new \Twig_SimpleFilter($this->filterPrefix.'Rating', [$this, 'rating'], $this->filterOptions) |
|
30 | 30 | ); |
31 | 31 | } |
32 | 32 | |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | public function avatar($email) |
40 | 40 | { |
41 | 41 | if (filter_var($email, FILTER_VALIDATE_EMAIL)) { |
42 | - return $this->baseUrl . "avatar/" . $this->generateHash($email); |
|
42 | + return $this->baseUrl."avatar/".$this->generateHash($email); |
|
43 | 43 | } else { |
44 | 44 | throw new InvalidArgumentException("The avatar filter must be passed a valid Email address"); |
45 | 45 | } |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | private function query($string, array $addition) |
139 | 139 | { |
140 | 140 | foreach ($addition as $name => $value) { |
141 | - $string .= (strpos($string, "?") === FALSE ? "?" : "&") . $name . "=" . $value; |
|
141 | + $string .= (strpos($string, "?") === FALSE ? "?" : "&").$name."=".$value; |
|
142 | 142 | } |
143 | 143 | return $string; |
144 | 144 | } |
@@ -95,7 +95,9 @@ |
||
95 | 95 | } else if (!is_bool($force)) { |
96 | 96 | throw new InvalidArgumentException("The force option for a default must be boolean"); |
97 | 97 | } else { |
98 | - if (filter_var($default, FILTER_VALIDATE_URL)) $default = urlencode($default); |
|
98 | + if (filter_var($default, FILTER_VALIDATE_URL)) { |
|
99 | + $default = urlencode($default); |
|
100 | + } |
|
99 | 101 | $force = ($force ? "y" : "n"); |
100 | 102 | return $this->query($value, array("default" => $default, "forcedefault" => $force)); |
101 | 103 | } |