1 : <?php
2 : /**
3 : * Criterion.php
4 : * 25-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 Criterion.php
14 : */
15 :
16 : /**
17 : * Services_Openstreetmap_Criterion
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 Criterion.php
24 : */
25 : class Services_Openstreetmap_Criterion
26 : {
27 : /**
28 : * Criterion type.
29 : *
30 : * @var mixed
31 : */
32 : protected $type = null;
33 : /**
34 : * Depending on type, value is null, a specified or generated value.
35 : *
36 : * @var mixed
37 : */
38 : protected $value = null;
39 :
40 : /**
41 : * A Criterion is used to specify a condition on how to search through
42 : * changesets.
43 : *
44 : * Search changesets by
45 : * A user id:
46 : * Services_Openstreetmap_Criterion('user', 12345)
47 : *
48 : * A display/user name:
49 : * Services_Openstreetmap_Criterion('display_name', 'fredflintstone')
50 : *
51 : * A bounding box:
52 : * Services_Openstreetmap_Criterion('bbox', -8.0590275, 52.9347449, -7.9966939, 52.9611999)
53 : *
54 : * For open changesets only:
55 : * Services_Openstreetmap_Criterion('open')
56 : *
57 : * For closed changesets only:
58 : * Services_Openstreetmap_Criterion('closed')
59 : *
60 : * For changesets created after a specific time:
61 : * Services_Openstreetmap_Criterion('time', '17/11/2011')
62 : *
63 : * For changesets created during a specific timespan:
64 : * Services_Openstreetmap_Criterion('time', '17/11/2011', '29/11/2011')
65 : *
66 : * @return Services_Openstreetmap_Criterion
67 : * @throws Services_Openstreetmap_InvalidArgumentException
68 : */
69 : public function __construct()
70 : {
71 7 : $args = func_get_args();
72 7 : $type = $args[0];
73 7 : $this->type = $type;
74 : switch($type) {
75 7 : case 'user':
76 3 : if (is_numeric($args[1])) {
77 3 : $this->value = $args[1];
78 3 : } else {
79 0 : throw new Services_Openstreetmap_InvalidArgumentException(
80 : 'User UID must be numeric'
81 0 : );
82 : }
83 3 : break;
84 6 : case 'bbox':
85 2 : $minLon = $args[1];
86 2 : $minLat = $args[2];
87 2 : $maxLon = $args[3];
88 2 : $maxLat = $args[4];
89 2 : $node = new Services_Openstreetmap_Node();
90 : try {
91 2 : $node->setLon($minLon);
92 2 : $node->setLat($minLat);
93 2 : $node->setLon($maxLon);
94 2 : $node->setLat($maxLat);
95 2 : } catch(Services_Openstreetmap_InvalidArgumentException $ex) {
96 0 : throw new Services_Openstreetmap_InvalidArgumentException(
97 0 : $ex->getMessage()
98 0 : );
99 : }
100 2 : $this->value = "{$minLon},{$minLat},{$maxLon},{$maxLat}";
101 2 : break;
102 5 : case 'display_name':
103 3 : $this->value = $args[1];
104 3 : break;
105 3 : case 'closed':
106 3 : case 'open':
107 1 : break;
108 2 : case 'time':
109 1 : $before = null;
110 1 : $after = null;
111 1 : if (isset($args[1])) {
112 1 : $after = $args[1];
113 1 : $time = strtotime($after);
114 1 : if ($time == -1 or $time === false) {
115 0 : throw new Services_Openstreetmap_InvalidArgumentException(
116 : 'Invalid time value'
117 0 : );
118 : }
119 1 : $after = gmstrftime('%Y-%m-%dT%H:%M:%SZ', $time);
120 1 : }
121 1 : if (isset($args[2])) {
122 1 : $before = $args[2];
123 1 : $time = strtotime($before);
124 1 : if ($time == -1 or $time === false) {
125 0 : throw new Services_Openstreetmap_InvalidArgumentException(
126 : 'Invalid time value'
127 0 : );
128 : }
129 1 : $before = gmstrftime('%Y-%m-%dT%H:%M:%SZ', $time);
130 1 : }
131 1 : if (!is_null($before)) {
132 1 : $this->value = "{$after},{$before}";
133 1 : } else {
134 0 : $this->value = $after;
135 : }
136 1 : break;
137 1 : default:
138 1 : $this->type = null;
139 1 : throw new Services_Openstreetmap_InvalidArgumentException(
140 : 'Unknown constraint type'
141 1 : );
142 1 : }
143 6 : }
144 :
145 : /**
146 : * Create the required query string portion
147 : *
148 : * @return string
149 : */
150 : public function query()
151 : {
152 5 : switch($this->type) {
153 5 : case 'bbox':
154 2 : return "bbox={$this->value}";
155 4 : case 'closed':
156 1 : return 'closed';
157 4 : case 'open':
158 0 : return 'open';
159 4 : case 'display_name':
160 4 : case 'time':
161 4 : case 'user':
162 4 : return http_build_query(array($this->type => $this->value));
163 0 : }
164 0 : }
165 :
166 : /**
167 : * Return the criterion type (closed, open, bbox, display_name, or user)
168 : *
169 : * @return string
170 : */
171 : public function type()
172 : {
173 6 : return $this->type;
174 : }
175 : }
176 :
177 : // vim:set et ts=4 sw=4:
178 : ?>
|