1 <?php
2
3 /*
4 * This file is part of the ICanBoogie package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace ICanBoogie\CLDR;
13
14 /**
15 * Retrives sections from the CLDR source.
16 */
17 class Retriever
18 {
19 protected $origin = 'http://www.unicode.org/repos/cldr-aux/json/24/';
20
21 /**
22 * Initializes the {@link $origin} property if the `$origin` param is specified.
23 *
24 * @param string $origin
25 */
26 public function __construct($origin=null)
27 {
28 if ($origin)
29 {
30 $this->origin = $origin;
31 }
32 }
33
34 /**
35 * The section path, following the pattern "<identity>/<section>".
36 *
37 * @param string $path
38 *
39 * @throws ResourceNotFound when the specified path does not exists on the CLDR source.
40 *
41 * @return string
42 */
43 public function __invoke($path)
44 {
45 $ch = curl_init($this->origin . $path . '.json');
46
47 curl_setopt_array
48 (
49 $ch, array
50 (
51 CURLOPT_FAILONERROR => true,
52 CURLOPT_RETURNTRANSFER => 1
53 )
54 );
55
56 $rc = curl_exec($ch);
57
58 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
59
60 curl_close($ch);
61
62 if ($http_code != 200)
63 {
64 throw new ResourceNotFound($path);
65 }
66
67 return $rc;
68 }
69 }
70
71 /**
72 * Exception throw when a path does not exists on the CLDR source.
73 *
74 * @property-read string $path The path.
75 */
76 class ResourceNotFound extends \Exception
77 {
78 private $path;
79
80 public function __construct($path, $code=500, \Exception $previous=null)
81 {
82 $this->path = $path;
83
84 parent::__construct("Path not defined: $path.", $code, $previous);
85 }
86
87 public function __get($property)
88 {
89 if ($property == 'path')
90 {
91 return $this->path;
92 }
93
94 throw new PropertyNotDefined(array($property, $this));
95 }
96 }