ddc9bdd86301f7a9741e2a6ae7c23000d484121f
[phancap.git] / src / phancap / Options.php
1 <?php
2 namespace phancap;
3
4 class Options
5 {
6     public static $options = array(
7         /**
8          * Browser settings
9          */
10         'url' => array(
11             'title'     => 'Website URL',
12             'default'   => null,
13             'type'      => 'url',
14             'required'  => true,
15         ),
16         'bwidth' => array(
17             'title'   => 'Browser width',
18             'default' => 1024,
19             'type'    => 'int',
20             'min'     => 16,
21             'max'     => 2048,
22         ),
23         'bheight' => array(
24             'title'   => 'Browser height',
25             'default' => null,
26             'type'    => 'int',
27             'min'     => 16,
28             'max'     => 8192,
29         ),
30         /**
31          * Screenshot settings
32          */
33         'swidth' => array(
34             'title'   => 'Screenshot width',
35             'default' => null,
36             'type'    => 'int',
37             'min'     => 16,
38             'max'     => 8192,
39         ),
40         'sheight' => array(
41             'title'   => 'Screenshot height',
42             'default' => null,
43             'type'    => 'int',
44             'min'     => 16,
45             'max'     => 8192,
46         ),
47         'sformat' => array(
48             'title'   => 'Screenshot format',
49             'default' => 'png',
50             'type'    => array('png', 'jpg', 'pdf'),
51         ),
52         'smode' => array(
53             'title'   => 'Screenshot mode',
54             'default' => 'screen',
55             'type'    => array('screen', 'page'),
56         ),
57         /**
58          * Authentication
59          */
60         'atimestamp' => array(
61             'title'   => 'Timestamp the request has been generated',
62             'default' => null,
63             'type'    => 'skip',
64         ),
65         'atoken' => array(
66             'title'   => 'Access token (user name)',
67             'default' => null,
68             'type'    => 'skip',
69         ),
70         'asignature' => array(
71             'title'   => 'Access signature',
72             'default' => null,
73             'type'    => 'skip',
74         ),
75     );
76
77     public $values = array();
78
79
80     /**
81      * Parses an array of options, validates them and writes them into
82      * $this->values.
83      *
84      * @param array $arValues Array of options, e.g. $_GET
85      */
86     public function parse($arValues)
87     {
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'
94                     );
95                 }
96                 continue;
97             }
98
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']
104                 );
105             } else if (gettype($arOption['type']) == 'array') {
106                 $this->values[$name] = $this->validateArray(
107                     $arValues[$name], $arOption['type']
108                 );
109             } else if ($arOption['type'] != 'skip') {
110                 throw new \InvalidArgumentException(
111                     'Unsupported option type: ' . $arOption['type']
112                 );
113             }
114             unset($arValues[$name]);
115         }
116
117         if (count($arValues) > 0) {
118             throw new \InvalidArgumentException(
119                 'Unsupported parameter: ' . implode(', ', array_keys($arValues))
120             );
121         }
122
123         $this->calcPageSize();
124     }
125
126     protected function calcPageSize()
127     {
128         if ($this->values['swidth'] === null) {
129             $this->values['swidth'] = $this->values['bwidth'];
130         }
131         if ($this->values['smode'] == 'page') {
132             return;
133         }
134
135         if ($this->values['sheight'] !== null) {
136             $this->values['bheight'] = intval(
137                 $this->values['bwidth'] / $this->values['swidth']
138                 * $this->values['sheight']
139             );
140         } else if ($this->values['bheight'] !== null) {
141             $this->values['sheight'] = intval(
142                 $this->values['swidth'] / $this->values['bwidth']
143                 * $this->values['bheight']
144             );
145         } else {
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;
149         }
150     }
151
152     protected function validateArray($value, $options)
153     {
154         if (array_search($value, $options) === false) {
155             throw new \InvalidArgumentException(
156                 'Invalid value ' . $value . '.'
157                 . ' Allowed: ' . implode(', ', $options)
158             );
159         }
160         return $value;
161     }
162
163     protected function validateInt($value, $min, $max)
164     {
165         if (!is_numeric($value)) {
166             throw new \InvalidArgumentException(
167                 'Value must be a number'
168             );
169         }
170         $value = (int) $value;
171         if ($value < $min) {
172             throw new \InvalidArgumentException(
173                 'Value must be at least ' . $min
174             );
175         }
176         if ($value > $max) {
177             throw new \InvalidArgumentException(
178                 'Value may be up to ' . $min
179             );
180         }
181         return $value;
182     }
183
184     protected function validateUrl($url)
185     {
186         $parts = parse_url($url);
187         if ($parts === false) {
188             throw new \InvalidArgumentException('Invalid URL');
189         }
190         if (!isset($parts['scheme'])) {
191             throw new \InvalidArgumentException('URL scheme missing');            
192         }
193         if (!isset($parts['host'])) {
194             throw new \InvalidArgumentException('URL host missing');            
195         }
196         return $url;
197     }
198 }
199 ?>