6 public static $options = array(
11 'title' => 'Website URL',
17 'title' => 'Browser width',
24 'title' => 'Browser height',
34 'title' => 'Screenshot width',
41 'title' => 'Screenshot height',
48 'title' => 'Screenshot format',
50 'type' => array('png', 'jpg', 'pdf'),
53 'title' => 'Screenshot mode',
54 'default' => 'screen',
55 'type' => array('screen', 'page'),
60 'atimestamp' => array(
61 'title' => 'Timestamp the request has been generated',
66 'title' => 'Access token (user name)',
70 'asignature' => array(
71 'title' => 'Access signature',
77 public $values = array();
81 * Parses an array of options, validates them and writes them into
84 * @param array $arValues Array of options, e.g. $_GET
86 public function parse($arValues)
88 foreach (static::$options as $name => $arOption) {
89 $this->values[$name] = $arOption['default'];
90 if (!isset($arValues[$name])) {
91 if (isset($arOption['required'])) {
92 throw new \InvalidArgumentException(
93 $name . ' parameter missing'
99 if ($arOption['type'] == 'url') {
100 $this->values[$name] = $this->validateUrl($arValues[$name]);
101 } else if ($arOption['type'] == 'int') {
102 $this->values[$name] = $this->validateInt(
103 $arValues[$name], $arOption['min'], $arOption['max']
105 } else if (gettype($arOption['type']) == 'array') {
106 $this->values[$name] = $this->validateArray(
107 $arValues[$name], $arOption['type']
109 } else if ($arOption['type'] != 'skip') {
110 throw new \InvalidArgumentException(
111 'Unsupported option type: ' . $arOption['type']
114 unset($arValues[$name]);
117 if (count($arValues) > 0) {
118 throw new \InvalidArgumentException(
119 'Unsupported parameter: ' . implode(', ', array_keys($arValues))
123 $this->calcPageSize();
126 protected function calcPageSize()
128 if ($this->values['swidth'] === null) {
129 $this->values['swidth'] = $this->values['bwidth'];
131 if ($this->values['smode'] == 'page') {
135 if ($this->values['sheight'] !== null) {
136 $this->values['bheight'] = intval(
137 $this->values['bwidth'] / $this->values['swidth']
138 * $this->values['sheight']
140 } else if ($this->values['bheight'] !== null) {
141 $this->values['sheight'] = intval(
142 $this->values['swidth'] / $this->values['bwidth']
143 * $this->values['bheight']
146 //no height set. use 4:3
147 $this->values['sheight'] = $this->values['swidth'] / 4 * 3;
148 $this->values['bheight'] = $this->values['bwidth'] / 4 * 3;
152 protected function validateArray($value, $options)
154 if (array_search($value, $options) === false) {
155 throw new \InvalidArgumentException(
156 'Invalid value ' . $value . '.'
157 . ' Allowed: ' . implode(', ', $options)
163 protected function validateInt($value, $min, $max)
165 if (!is_numeric($value)) {
166 throw new \InvalidArgumentException(
167 'Value must be a number'
170 $value = (int) $value;
172 throw new \InvalidArgumentException(
173 'Value must be at least ' . $min
177 throw new \InvalidArgumentException(
178 'Value may be up to ' . $min
184 protected function validateUrl($url)
186 $parts = parse_url($url);
187 if ($parts === false) {
188 throw new \InvalidArgumentException('Invalid URL');
190 if (!isset($parts['scheme'])) {
191 throw new \InvalidArgumentException('URL scheme missing');
193 if (!isset($parts['host'])) {
194 throw new \InvalidArgumentException('URL host missing');