6 public static $options = array(
11 'title' => 'Website URL',
16 'title' => 'Browser width',
23 'title' => 'Browser height',
33 'title' => 'Screenshot width',
40 'title' => 'Screenshot height',
47 'title' => 'Screenshot format',
49 'type' => array('png', 'jpg', 'pdf'),
52 'title' => 'Screenshot mode',
53 'default' => 'screen',
54 'type' => array('screen', 'page'),
58 public $values = array();
62 * Parses an array of options, validates them and writes them into
65 * @param array $arValues Array of options, e.g. $_GET
67 public function parse($arValues)
69 foreach (static::$options as $name => $arOption) {
70 $this->values[$name] = $arOption['default'];
71 if (!isset($arValues[$name])) {
74 if ($arOption['type'] == 'url') {
75 $this->values[$name] = $this->validateUrl($arValues[$name]);
76 } else if ($arOption['type'] == 'int') {
77 $this->values[$name] = $this->validateInt(
78 $arValues[$name], $arOption['min'], $arOption['max']
80 } else if (gettype($arOption['type']) == 'array') {
81 $this->values[$name] = $this->validateArray(
82 $arValues[$name], $arOption['type']
85 throw new \InvalidArgumentException(
86 'Unsupported option type: ' . $arOption['type']
89 unset($arValues[$name]);
92 if (count($arValues) > 0) {
93 throw new \InvalidArgumentException(
94 'Unsupported parameter: ' . implode(', ', array_keys($arValues))
98 $this->calcPageSize();
101 protected function calcPageSize()
103 if ($this->values['smode'] == 'page') {
107 if ($this->values['sheight'] !== null) {
108 $this->values['bheight'] = intval(
109 $this->values['bwidth'] / $this->values['swidth']
110 * $this->values['sheight']
112 } else if ($this->values['bheight'] !== null) {
113 $this->values['sheight'] = intval(
114 $this->values['swidth'] / $this->values['bwidth']
115 * $this->values['bheight']
118 //no height set. use 4:3
119 $this->values['sheight'] = $this->values['swidth'] / 4 * 3;
120 $this->values['bheight'] = $this->values['bwidth'] / 4 * 3;
124 protected function validateArray($value, $options)
126 if (array_search($value, $options) === false) {
127 throw new \InvalidArgumentException(
128 'Invalid value ' . $value . '.'
129 . ' Allowed: ' . implode(', ', $options)
135 protected function validateInt($value, $min, $max)
137 if (!is_numeric($value)) {
138 throw new \InvalidArgumentException(
139 'Value must be a number'
142 $value = (int) $value;
144 throw new \InvalidArgumentException(
145 'Value must be at least ' . $min
149 throw new \InvalidArgumentException(
150 'Value may be up to ' . $min
156 protected function validateUrl($url)
158 $parts = parse_url($url);
159 if ($parts === false) {
160 throw new \InvalidArgumentException('Invalid URL');
162 if (!isset($parts['scheme'])) {
163 throw new \InvalidArgumentException('URL scheme missing');
165 if (!isset($parts['host'])) {
166 throw new \InvalidArgumentException('URL host missing');