b5c96d996da3cf04842edde90c2ff26fa599b611
[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         ),
15         'bwidth' => array(
16             'title'   => 'Browser width',
17             'default' => 1024,
18             'type'    => 'int',
19             'min'     => 16,
20             'max'     => 2048,
21         ),
22         'bheight' => array(
23             'title'   => 'Browser height',
24             'default' => null,
25             'type'    => 'int',
26             'min'     => 16,
27             'max'     => 8192,
28         ),
29         /**
30          * Screenshot settings
31          */
32         'swidth' => array(
33             'title'   => 'Screenshot width',
34             'default' => 300,
35             'type'    => 'int',
36             'min'     => 16,
37             'max'     => 8192,
38         ),
39         'sheight' => array(
40             'title'   => 'Screenshot height',
41             'default' => null,
42             'type'    => 'int',
43             'min'     => 16,
44             'max'     => 8192,
45         ),
46         'sformat' => array(
47             'title'   => 'Screenshot format',
48             'default' => 'png',
49             'type'    => array('png', 'jpg', 'pdf'),
50         ),
51         'smode' => array(
52             'title'   => 'Screenshot mode',
53             'default' => 'screen',
54             'type'    => array('screen', 'page'),
55         ),
56     );
57
58     public $values = array();
59
60
61     /**
62      * Parses an array of options, validates them and writes them into
63      * $this->values.
64      *
65      * @param array $arValues Array of options, e.g. $_GET
66      */
67     public function parse($arValues)
68     {
69         foreach (static::$options as $name => $arOption) {
70             $this->values[$name] = $arOption['default'];
71             if (!isset($arValues[$name])) {
72                 continue;
73             }
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']
79                 );
80             } else if (gettype($arOption['type']) == 'array') {
81                 $this->values[$name] = $this->validateArray(
82                     $arValues[$name], $arOption['type']
83                 );
84             } else {
85                 throw new \InvalidArgumentException(
86                     'Unsupported option type: ' . $arOption['type']
87                 );
88             }
89             unset($arValues[$name]);
90         }
91
92         if (count($arValues) > 0) {
93             throw new \InvalidArgumentException(
94                 'Unsupported parameter: ' . implode(', ', array_keys($arValues))
95             );
96         }
97
98         $this->calcPageSize();
99     }
100
101     protected function calcPageSize()
102     {
103         if ($this->values['smode'] == 'page') {
104             return;
105         }
106
107         if ($this->values['sheight'] !== null) {
108             $this->values['bheight'] = intval(
109                 $this->values['bwidth'] / $this->values['swidth']
110                 * $this->values['sheight']
111             );
112         } else if ($this->values['bheight'] !== null) {
113             $this->values['sheight'] = intval(
114                 $this->values['swidth'] / $this->values['bwidth']
115                 * $this->values['bheight']
116             );
117         } else {
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;
121         }
122     }
123
124     protected function validateArray($value, $options)
125     {
126         if (array_search($value, $options) === false) {
127             throw new \InvalidArgumentException(
128                 'Invalid value ' . $value . '.'
129                 . ' Allowed: ' . implode(', ', $options)
130             );
131         }
132         return $value;
133     }
134
135     protected function validateInt($value, $min, $max)
136     {
137         if (!is_numeric($value)) {
138             throw new \InvalidArgumentException(
139                 'Value must be a number'
140             );
141         }
142         $value = (int) $value;
143         if ($value < $min) {
144             throw new \InvalidArgumentException(
145                 'Value must be at least ' . $min
146             );
147         }
148         if ($value > $max) {
149             throw new \InvalidArgumentException(
150                 'Value may be up to ' . $min
151             );
152         }
153         return $value;
154     }
155
156     protected function validateUrl($url)
157     {
158         $parts = parse_url($url);
159         if ($parts === false) {
160             throw new \InvalidArgumentException('Invalid URL');
161         }
162         if (!isset($parts['scheme'])) {
163             throw new \InvalidArgumentException('URL scheme missing');            
164         }
165         if (!isset($parts['host'])) {
166             throw new \InvalidArgumentException('URL host missing');            
167         }
168         return $url;
169     }
170 }
171 ?>