Passed
Pull Request — master (#779)
by
unknown
05:43
created

TGravatar::getUseSecureUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * TGravatar class file.
4
 *
5
 * @author Brad Anderson <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 * @package Prado\Web\UI\WebControls
9
 */
10
11
namespace Prado\Web\UI\WebControls;
12
13
use Prado\TPropertyValue;
14
use Prado\Exceptions\TInvalidDataValueException;
15
16
/**
17
 * TGravatar class.
18
 *
19
 * TGravatar extends TImage and outputs a gravatar ImageUrl based
20
 * upon an email address, size, rating, and default style of the gravatar.
21
 *
22
 * {@link setDefault} allows for various styles of gravatar:
23
 *		• mp - Mystery Person
24
 *		• identicon - identicon style
25
 *		• monsterid - monster style
26
 *		• wavatar - wavatar style
27
 *		• retro - Retra style
28
 *		• robohash - Robohash style
29
 *		• blank - a blank space
30
 *		• 404 - not found page error
31
 *		• _url - provide your own default URL (to be url encoded for you)
32
 *
33
 * A rating of the gravatar can be provided as g, pg, r, and x.
34
 *
35
 * The size must be between 1 and 512, inclusive.
36
 *
37
 * {@link TParameterizeBehavior} can be attached to TGravatar to give
38
 * default values for various properties like Default and Rating.
39
 *
40
 * See {@link https://gravatar.com} for more information.
41
 *
42
 * @author Brad Anderson <[email protected]>
43
 * @package Prado\Web\UI\WebControls
44
 * @since 4.2.0
45
 */
46
class TGravatar extends TImage
47
{
48
	public const HTTP_URL = 'http://www.gravatar.com/avatar/';
49
	public const HTTPS_URL = 'https://secure.gravatar.com/avatar/';
50
	
51
	/**
52
	 * @return string the URL to the gravatar
53
	 */
54
	public function getImageUrl()
55
	{
56
		$params = [];
57
		if ($size = $this->getSize()) {
58
			$params['s'] = $size;
59
		}
60
		if ($rating = $this->getRating()) {
61
			$params['r'] = $rating;
62
		}
63
		if ($style = $this->getDefaultImageStyle()) {
64
			$params['d'] = $style;
65
		}
66
		
67
		return ($this->getUseSecureUrl() ? self::HTTPS_URL : self::HTTP_URL) . md5(strtolower(trim($this->getEmail()))) . '?' . http_build_query($params, '', '&', PHP_QUERY_RFC3986);
68
	}
69
	
70
	/**
71
	 * @return string one of: mp, identicon, monsterid, wavatar, retro, robohash, blank, 404, _url_
72
	 */
73
	public function getDefaultImageStyle()
74
	{
75
		return $this->getViewState('default', '');
76
	}
77
	
78
	/**
79
	 * @param $default string one of: mp, identicon, monsterid, wavatar, retro, robohash, blank, 404, _url_
80
	 */
81
	public function setDefaultImageStyle($default)
82
	{
83
		$default = TPropertyValue::ensureString($default);
84
		$lowerDefault = strtolower($default);
85
		if ($valid = in_array($lowerDefault, ['mp', 'identicon', 'monsterid', 'wavatar', 'retro', 'robohash', 'blank', '404', ''])) {
86
			$default = $lowerDefault;
87
		}
88
		if (!$valid && !preg_match('/^https?:\/\//i', $default)) {
89
			throw new TInvalidDataValueException('gravatar_bad_default', $default);
90
		}
91
		$this->setViewState('default', $default, '');
92
	}
93
	
94
	/**
95
	 * @return int|string the pixel size of the gravatar, 1..512, default 80
96
	 */
97
	public function getSize()
98
	{
99
		return $this->getViewState('size', '');
100
	}
101
	
102
	/**
103
	 * @param $size int the pixel size of the gravatar, 1..512
104
	 */
105
	public function setSize($size)
106
	{
107
		$_size = TPropertyValue::ensureInteger($size);
108
		if (($_size > 512 || $_size < 1) && $size !== null && $size !== '') {
109
			throw new TInvalidDataValueException('gravatar_bad_size', $size);
110
		}
111
		$_size = ($size === null || $size === '') ? '' : $_size;
112
		$this->setViewState('size', $_size, '');
113
	}
114
	
115
	/**
116
	 * @return string the rating of the icon ['g', 'pg', 'r', 'x', ''], default ''
117
	 */
118
	public function getRating()
119
	{
120
		return $this->getViewState('rating', '');
121
	}
122
	
123
	/**
124
	 * @param $rating string the rating of the icon ['g', 'pg', 'r', 'x', '']
125
	 */
126
	public function setRating($rating)
127
	{
128
		$rating = strtolower(TPropertyValue::ensureString($rating));
129
		$rating = TPropertyValue::ensureEnum($rating, ['g', 'pg', 'r', 'x', '']);
130
		if (!$rating) {
131
			$rating = null;
132
		}
133
		$this->setViewState('rating', $rating, '');
134
	}
135
	
136
	/**
137
	 * @return string the email address associated with the gravatar icon
138
	 */
139
	public function getEmail()
140
	{
141
		return $this->getViewState('email', '');
142
	}
143
	
144
	/**
145
	 * @param $email string the email address associated with the gravatar icon
146
	 */
147
	public function setEmail($email)
148
	{
149
		$this->setViewState('email', TPropertyValue::ensureString($email), '');
150
	}
151
	
152
	/**
153
	 * @return bool whether or not to use the secure HTTPS url, defaults to the connection being used
154
	 */
155
	public function getUseSecureUrl()
156
	{
157
		return $this->getViewState('use_secure_url', $this->getRequest()->getIsSecureConnection());
158
	}
159
	
160
	/**
161
	 * @param bool $useSecureUrl whether or not to use the secure HTTPS url
162
	 */
163
	public function setUseSecureUrl($useSecureUrl)
164
	{
165
		$this->setViewState('use_secure_url', TPropertyValue::ensureBoolean($useSecureUrl));
166
	}
167
}
168