http://www.phing.info/

Source Code Coverage

Designed for use with PHPUnit, Xdebug and Phing.

Methods: 16 LOC: 530 Statements: 144
Legend: executednot executeddead code
Source file Statements Methods Total coverage
Config.php 100.0% 87.5% 98.8%
   
1
<?php
2
/**
3
 * Config.php
4
 * 08-Nov-2011
5
 *
6
 * PHP Version 5
7
 *
8
 * @category Services
9
 * @package  Services_Openstreetmap
10
 * @author   Ken Guest <kguest@php.net>
11
 * @license  BSD http://www.opensource.org/licenses/bsd-license.php
12
 * @version  Release: @package_version@
13
 * @link     Config.php
14
 */
15
16
/**
17
 * Services_Openstreetmap_Config
18
 *
19
 * @category Services
20
 * @package  Services_Openstreetmap
21
 * @author   Ken Guest <kguest@php.net>
22
 * @license  BSD http://www.opensource.org/licenses/bsd-license.php
23
 * @link     Config.php
24
 */
25
class Services_Openstreetmap_Config
26
{
27
28
    /**
29
     * Minimum version of the OSM API that is supported.
30
     * @var float
31
     * @internal
32
     */
33
    protected $minVersion = null;
34
35
    /**
36
     * Maximum version of the OSM API that is supported.
37
     * @var float
38
     * @internal
39
     */
40
    protected $maxVersion = null;
41
42
    /**
43
     * timeout, in seconds.
44
     * @var integer
45
     * @internal
46
     */
47
    protected $timeout = null;
48
49
    /**
50
     * number of elements allowed per changeset
51
     * @var integer
52
     * @internal
53
     */
54
    protected $changesetMaximumElements = null;
55
56
    /**
57
     * Maximum number of nodes per way.
58
     * @var integer
59
     * @internal
60
     */
61
    protected $waynodesMaximum = null;
62
63
    /**
64
     * Number of tracepoints per way.
65
     * @var integer
66
     * @internal
67
     */
68
    protected $tracepointsPerPage = null;
69
70
    /**
71
     * Max size of area that can be downloaded in one request.
72
     * @var float
73
     * @internal
74
     */
75
    protected $areaMaximum = null;
76
77
78
    /**
79
     * Default config settings
80
     *
81
     * @var array
82
     * @see Services_Openstreetmap::getConfig
83
     * @see Services_Openstreetmap::setConfig
84
     */
85
    protected $config = array(
86
        'adapter'      => 'HTTP_Request2_Adapter_Socket',
87
        'api_version'  => '0.6',
88
        'password'     => null,
89
        'passwordfile' => null,
90
        'server'       => 'http://api.openstreetmap.org/',
91
        'User-Agent'   => 'Services_Openstreetmap',
92
        'user'         => null,
93
        'verbose'      => false,
94
    );
95
96
    /**
97
     * Version of the [OSM] API which communications will be over.
98
     * @var string
99
     * @internal
100
     */
101
    protected $api_version = '0.6';
102
103
    /**
104
     * Server to connect to.
105
     * @var string
106
     * @internal
107
     */
108
    protected $server = 'http://api.openstreetmap.org/';
109
110
    /**
111
     * Get the value of a configuration setting - if none is set all are
112
     * returned.
113
     *
114
     * <code>
115
     * $config = $osm->getConfig();
116
     * </code>
117
     *
118
     * @param string $name name. optional.
119
     *
120
     * @return mixed  value of $name parameter, array of all configuration
121
     *                parameters if $name is not given
122
     * @throws Services_Openstreetmap_Exception If the parameter is unknown
123
     */
124
    public function getValue($name = null)
125
    {
126 83
        if (is_null($name)) {
127
            return $this->config;
128 83
        } elseif (!array_key_exists($name, $this->config)) {
129 1
            throw new Services_Openstreetmap_Exception(
130 1
                "Unknown config parameter '$name'"
131 1
            );
132 1
        }
133 83
        return $this->config[$name];
134
    }
135
136
    /**
137
     * set at least one configuration variable.
138
     *
139
     * <pre>
140
     * $osm->setConfig('user', 'fred@example.com');
141
     * $osm->setConfig(array('user' => 'fred@example.com', 'password' => 'Simples'));
142
     * $osm->setConfig('user' => 'f@example.com')->setConfig('password' => 'Sis');
143
     * </pre>
144
     *
145
     * The following parameters are available:
146
     * <ul>
147
     *  <li> 'adapter'      - adapter to use (string)</li>
148
     *  <li> 'api_version'  - Version of API to communicate via (string)</li>
149
     *  <li> 'password'     - password (string, optional)</li>
150
     *  <li> 'passwordfile' - passwordfile (string, optional)</li>
151
     *  <li> 'server'       - server to connect to (string)</li>
152
     *  <li> 'User-Agent'   - User-Agent (string)</li>
153
     *  <li> 'user'         - user (string, optional)</li>
154
     *  <li> 'verbose'      - verbose (boolean, optional)</li>
155
     * </ul>
156
     *
157
     * @param mixed $config array containing config settings
158
     * @param mixed $value  config value if $config is not an array
159
     *
160
     * @throws Services_Openstreetmap_Exception If the parameter is unknown
161
     *
162
     * @return Services_Openstreetmap_Config
163
     */
164
    public function setValue($config, $value = null)
165
    {
166 84
        if (is_array($config)) {
167 84
            if (isset($config['adapter'])) {
168 84
                $this->config['adapter'] = $config['adapter'];
169 84
            }
170 84
            foreach ($config as $key=>$value) {
171 84
                if (!array_key_exists($key, $this->config)) {
172 1
                    throw new Services_Openstreetmap_Exception(
173 1
                        "Unknown config parameter '$key'"
174 1
                    );
175 1
                }
176
                switch($key) {
177 84
                case 'server':
178 63
                    $this->setServer($value);
179 60
                    break;
180 84
                case 'passwordfile':
181 9
                    $this->setPasswordfile($value);
182 9
                    break;
183 84
                case 'api_version':
184 1
                    $this->config[$key] = $value;
185 1
                    $api = "Services_Openstreetmap_API_V" . str_replace(
186 1
                        '.',
187 1
                        '',
188
                        $value
189 1
                    );
190 1
                    $this->api = new $api;
191 1
                    break;
192 84
                default:
193 84
                    $this->config[$key] = $value;
194 84
                }
195 84
            }
196 80
        } else {
197 9
            if (!array_key_exists($config, $this->config)) {
198 2
                throw new Services_Openstreetmap_Exception(
199 2
                    "Unknown config parameter '$config'"
200 2
                );
201 2
            }
202 7
            $this->config[$config] = $value;
203 7
            if ($config == 'server') {
204 1
                $this->setServer($this->server);
205 6
            } elseif ($config == 'passwordfile') {
206 5
                $this->setPasswordfile($value);
207 4
            }
208
        }
209 80
        return $this;
210
    }
211
212
    /**
213
     * Connect to specified server.
214
     *
215
     * @param string $server base server details, e.g. http://api.openstreetmap.org
216
     *
217
     * @return Services_Openstreetmap
218
     */
219
    public function setServer($server)
220
    {
221
        try {
222 65
            $c = $this->getTransport()->getResponse($server . '/api/capabilities');
223 65
        } catch (Exception $ex) {
224 1
            throw new Services_Openstreetmap_Exception(
225 1
                'Could not get a valid response from server',
226 1
                $ex->getCode(),
227
                $ex
228 1
            );
229
        }
230 64
        $this->server = $server;
231 64
        $capabilities = $c->getBody();
232 64
        if (!$this->_checkCapabilities($capabilities)) {
233 1
            throw new Services_Openstreetmap_Exception(
234
                'Problem checking server capabilities'
235 1
            );
236 1
        }
237 61
        $this->config['server'] = $server;
238
239 61
        return $this;
240
    }
241
242
    /**
243
     * Set and parse a password file, setting username and password as specified
244
     * in the file.
245
     *
246
     * A password file is a ASCII text file, with username and passwords pairs
247
     * on each line, seperated [delimited] by a semicolon.
248
     * Lines starting with a hash [#] are comments.
249
     * If only one non-commented line is present in the file, that username and
250
     * password will be used for authentication.
251
     * If more than one set of usernames and passwords are present, the
252
     * username must be specified, and the matching password from the file will
253
     * be used.
254
     *
255
     * <pre>
256
     * # Example password file.
257
     * fredfs@example.com:Wilma4evah
258
     * barney@example.net:B3ttyRawks
259
     * </pre>
260
     *
261
     * @param string $file file containing credentials
262
     *
263
     * @return Services_Openstreetmap
264
     */
265
    public function setPasswordfile($file)
266
    {
267 19
        if (is_null($file)) {
268 1
            return $this;
269 1
        }
270 18
        $lines = @file($file);
271 18
        if ($lines === false) {
272 2
            throw new Services_Openstreetmap_Exception(
273
                'Could not read password file'
274 2
            );
275 2
        }
276 16
        $this->config['passwordfile'] =  $file;
277 16
        array_walk($lines, create_function('&$val', '$val = trim($val);'));
278 16
        if (sizeof($lines) == 1) {
279 4
            if (strpos($lines[0], '#') !== 0) {
280 4
                list($this->config['user'], $this->config['password'])
281 4
                    = explode(':', $lines[0]);
282 4
            }
283 16
        } elseif (sizeof($lines) == 2) {
284 8
            if (strpos($lines[0], '#') === 0) {
285 8
                if (strpos($lines[1], '#') !== 0) {
286 8
                    list($this->config['user'], $this->config['password'])
287 8
                        = explode(':', $lines[1]);
288 8
                }
289 8
            }
290 8
        } else {
291 4
            foreach ($lines as $line) {
292 2
                if (strpos($line, '#') === 0) {
293 2
                    continue;
294 2
                }
295 2
                list($user, $pwd) = explode(':', $line);
296 2
                if ($user == $this->config['user']) {
297 2
                    $this->config['password'] = $pwd;
298 2
                }
299 4
            }
300
        }
301 16
        return $this;
302
    }
303
304
    /**
305
     * Set the Transport instance.
306
     *
307
     * @param Services_Openstreetmap_Transport $transport Transport instance.
308
     *
309
     * @return Services_Openstreetmap_Config
310
     */
311
    public function setTransport(Services_Openstreetmap_Transport $transport)
312
    {
313 84
        $this->transport = $transport;
314 84
        return $this;
315
    }
316
317
    /**
318
     * Retrieve the current Transport instance.
319
     *
320
     * @return Services_Openstreetmap_Transport.
321
     */
322
    public function getTransport()
323
    {
324 65
        return $this->transport;
325
    }
326
327
    /**
328
     * Return all config settings in an array.
329
     *
330
     * @return array
331
     */
332
    public function asArray()
333
    {
334 30
        return $this->config;
335
    }
336
337
    /**
338
     * Set various properties to describe the capabilities that the connected
339
     * server supports.
340
     *
341
     * @param mixed $capabilities XML describing the capabilities of the server
342
     *
343
     * @see maxVersion
344
     * @see minVersion
345
     * @see timeout
346
     *
347
     * @return boolean
348
     *
349
     * @internal
350
     * @throws   Services_Openstreetmap_Exception If the API Version is not
351
     *                                            supported.
352
     */
353
    private function _checkCapabilities($capabilities)
354
    {
355 64
        $xml = simplexml_load_string($capabilities);
356 64
        if ($xml === false) {
357 1
            return false;
358 1
        }
359
360 63
        $this->minVersion = (float) $this->getXmlValue($xml, 'version', 'minimum');
361 63
        $this->maxVersion = (float) $this->getXmlValue($xml, 'version', 'maximum');
362 63
        if (($this->minVersion > $this->api_version
363 63
            || $this->api_version > $this->maxVersion)
364 63
        ) {
365 2
            throw new Services_Openstreetmap_Exception(
366 2
                'Specified API Version ' . $this->api_version .' not supported.'
367 2
            );
368 2
        }
369 61
        $this->timeout = (int) $this->getXmlValue($xml, 'timeout', 'seconds');
370
        //changesets
371 61
        $this->changesetMaximumElements = (int) $this->getXmlValue(
372 61
            $xml,
373 61
            'changesets',
374
            'maximum_elements'
375 61
        );
376
377
        // Maximum number of nodes per way.
378 61
        $this->waynodesMaximum = (int) $this->getXmlValue(
379 61
            $xml,
380 61
            'waynodes',
381
            'maximum'
382 61
        );
383
384
        // Number of tracepoints per way.
385 61
        $this->tracepointsPerPage = (int) $this->getXmlValue(
386 61
            $xml,
387 61
            'tracepoints',
388
            'per_page'
389 61
        );
390
391
        // Max size of area that can be downloaded in one request.
392 61
        $this->areaMaximum = (float) $this->getXmlValue(
393 61
            $xml,
394 61
            'area',
395
            'maximum'
396 61
        );
397 61
        return true;
398
    }
399
400
    /**
401
     * Max size of area that can be downloaded in one request.
402
     *
403
     * <code>
404
     * $osm = new Services_Openstreetmap();
405
     * $area_allowed = $osm->getMaxArea();
406
     * </code>
407
     *
408
     * @return float
409
     */
410
    public function getMaxArea()
411
    {
412 1
        return $this->areaMaximum;
413
    }
414
415
    /**
416
     * minVersion - min API version supported by connected server.
417
     *
418
     * <code>
419
     * $config = array('user' => 'fred@example.net', 'password' => 'wilma4eva');
420
     * $osm = new Services_Openstreetmap($config);
421
     * $min = $osm->getMinVersion();
422
     * </code>
423
     *
424
     * @return float
425
     */
426
    public function getMinVersion()
427
    {
428 1
        return $this->minVersion;
429
    }
430
431
    /**
432
     * maxVersion - max API version supported by connected server.
433
     *
434
     * <code>
435
     * $config = array('user' => 'fred@example.net', 'password' => 'wilma4eva');
436
     * $osm = new Services_Openstreetmap($config);
437
     * $max = $osm->getMaxVersion();
438
     * </code>
439
     *
440
     * @return float
441
     */
442
    public function getMaxVersion()
443
    {
444 1
        return $this->maxVersion;
445
    }
446
447
    /**
448
     * Return the number of seconds that must elapse before a connection is
449
     * considered to have timed-out.
450
     *
451
     * @return int
452
     */
453
    public function getTimeout()
454
    {
455 1
        return $this->timeout;
456
    }
457
458
    /**
459
     * Maximum number of tracepoints per page.
460
     *
461
     * <code>
462
     * $osm = new Services_Openstreetmap();
463
     * $tracepoints = $osm->getTracepointsPerPage();
464
     * </code>
465
     *
466
     * @return float
467
     */
468
    public function getTracepointsPerPage()
469
    {
470 1
        return $this->tracepointsPerPage;
471
    }
472
473
    /**
474
     * Maximum number of nodes per way.
475
     *
476
     * Anymore than that and the way must be split.
477
     *
478
     * <code>
479
     * $osm = new Services_Openstreetmap();
480
     * $max = $osm->getMaxNodes();
481
     * </code>
482
     *
483
     * @return float
484
     */
485
    public function getMaxNodes()
486
    {
487 1
        return $this->waynodesMaximum;
488
    }
489
490
    /**
491
     * Number of elements allowed per changeset
492
     *
493
     * <code>
494
     * $osm = new Services_Openstreetmap();
495
     * $max = $osm->getMaxElements();
496
     * </code>
497
     *
498
     * @return float
499
     */
500
    public function getMaxElements()
501
    {
502 1
        return $this->changesetMaximumElements;
503
    }
504
505
    /**
506
     * getXmlValue
507
     *
508
     * @param SimpleXMLElement $xml       Object
509
     * @param string           $tag       name of tag
510
     * @param string           $attribute name of attribute
511
     * @param mixed            $default   default value
512
     *
513
     * @return string
514
     */
515
    public function getXmlValue(
516
        SimpleXMLElement $xml,
517
        $tag,
518
        $attribute,
519
        $default = null
520
    ) {
521 63
        $obj = $xml->xpath('//' . $tag);
522 63
        if (empty($obj)) {
523
            return $default;
524
        }
525 63
        return $obj[0]->attributes()->$attribute;
526
    }
527
528
}
529
530
?>


Report generated at 2012-01-31T21:54:36Z