Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
class Mappers
3
{
4
    public static function departmentMapper($str)
5
    {
6
        //maps 'one_two_three' to 'oneTwoThree'
7
        return preg_replace("/(_)([a-z])/e", "strtoupper('\\2')", $str);
8
    }
9
 
10
    public static function employeeMapper($str)
11
    {
12
        //maps 'one_two_three' to 'OneTwoThree'
13
        return ucfirst(preg_replace("/(_)([a-z])/e", "strtoupper('\\2')", $str));
14
    }
15
 
16
    public function saleMapper($str)
17
    {
18
        //maps 'one_two_three' to 'ONETWOTHREE'
19
        return strtoupper(str_replace('_', '', $str));
20
    }
21
}
22
 
23
function mapArtist($str)
24
{
25
    //maps 'one_two_three' to 'onetwothree'
26
    return strtolower(str_replace('_', '', $str));
27
}
28
 
29
$myMappers = new Mappers();
30
 
31
require_once 'XML/Query2XML.php';
32
require_once 'XML/Query2XML/ISO9075Mapper.php';
33
require_once 'MDB2.php';
34
$query2xml = XML_Query2XML::factory(MDB2::factory('mysql://root@localhost/Query2XML_Tests'));
35
$dom = $query2xml->getXML(
36
    "SELECT
37
         s.*,
38
         manager.employeeid AS manager_employeeid,
39
         manager.employeename AS manager_employeename,
40
         d.*,
41
         department_head.employeeid AS department_head_employeeid,
42
         department_head.employeename AS department_head_employeename,
43
         e.*,
44
         sa.*,
45
         c.*,
46
         al.*,
47
         ar.*,
48
         (SELECT COUNT(*) FROM sale WHERE sale.store_id = s.storeid) AS store_sales,
49
         (SELECT
50
            COUNT(*)
51
          FROM
52
            sale, employee, employee_department
53
          WHERE
54
            sale.employee_id = employee.employeeid
55
            AND
56
            employee_department.employee_id = employee.employeeid
57
            AND
58
            employee_department.department_id = d.departmentid
59
         ) AS department_sales,
60
         (SELECT
61
            COUNT(*)
62
          FROM
63
            employee, employee_department, department
64
          WHERE
65
            employee_department.employee_id = employee.employeeid
66
            AND
67
            employee_department.department_id = department.departmentid
68
            AND
69
            department.store_id = s.storeid
70
         ) AS store_employees,
71
         (SELECT
72
            COUNT(*)
73
          FROM
74
            employee, employee_department
75
          WHERE
76
            employee_department.employee_id = employee.employeeid
77
            AND
78
            employee_department.department_id = d.departmentid
79
         ) AS department_employees
80
     FROM
81
         store s
82
          LEFT JOIN employee manager ON s.manager = manager.employeeid
83
         LEFT JOIN department d ON d.store_id = s.storeid
84
          LEFT JOIN employee department_head ON department_head.employeeid = d.department_head
85
          LEFT JOIN employee_department ed ON ed.department_id = d.departmentid
86
           LEFT JOIN employee e ON e.employeeid = ed.employee_id
87
            LEFT JOIN sale sa ON sa.employee_id = e.employeeid
88
             LEFT JOIN customer c ON c.customerid = sa.customer_id
89
             LEFT JOIN album al ON al.albumid = sa.album_id
90
              LEFT JOIN artist ar ON ar.artistid = al.artist_id
91
     ORDER BY
92
        s.storeid,
93
        manager.employeeid,
94
        d.departmentid,
95
        department_head.employeeid,
96
        ed.employee_id,
97
        ed.department_id,
98
        e.employeeid,
99
        sa.saleid,
100
        c.customerid,
101
        al.albumid,
102
        ar.artistid",
103
    array(
104
        'rootTag' => 'music_company',
105
        'rowTag' => 'store',
106
        'idColumn' => 'storeid',
107
        'mapper' => 'strtoupper',
108
        'attributes' => array(
109
            'storeid'
110
        ),
111
        'elements' => array(
112
            'store_sales',
113
            'store_employees',
114
            'manager' => array(
115
                'idColumn' => 'manager_employeeid',
116
                'attributes' => array(
117
                    'manager_employeeid'
118
                ),
119
                'elements' => array(
120
                    'manager_employeename'
121
                )
122
            ),
123
            'address' => array(
124
                'elements' => array(
125
                    'country',
126
                    'state' => '#Helper::getStatePostalCode()',
127
                    'city',
128
                    'street',
129
                    'phone'
130
                )
131
            ),
132
            'department' => array(
133
                'idColumn' => 'departmentid',
134
                'mapper' => 'Mappers::departmentMapper',
135
                'attributes' => array(
136
                    'departmentid'
137
                ),
138
                'elements' => array(
139
                    'department_sales',
140
                    'department_employees',
141
                    'departmentname',
142
                    'department_head' => array(
143
                        'idColumn' => 'department_head_employeeid',
144
                        'attributes' => array(
145
                            'department_head_employeeid'
146
                        ),
147
                        'elements' => array(
148
                            'department_head_employeename'
149
                        )
150
                    ),
151
                    'employees' => array(
152
                        'rootTag' => 'employees',
153
                        'rowTag' => 'employee',
154
                        'idColumn' => 'employeeid',
155
                        'mapper' => array('Mappers', 'employeeMapper'),
156
                        'attributes' => array(
157
                            'employeeid'
158
                        ),
159
                        'elements' => array(
160
                            'employeename',
161
                            'sales' => array(
162
                                'rootTag' => 'sales',
163
                                'rowTag' => 'sale',
164
                                'idColumn' => 'saleid',
165
                                'mapper' => array($myMappers, 'saleMapper'),
166
                                'attributes' => array(
167
                                    'saleid'
168
                                ),
169
                                'elements' => array(
170
                                    'timestamp',
171
                                    'customer' => array(
172
                                        'idColumn' => 'customerid',
173
                                        'mapper' => false,
174
                                        'attributes' => array(
175
                                            'customerid'
176
                                        ),
177
                                        'elements' => array(
178
                                            'first_name',
179
                                            'last_name',
180
                                            'email'
181
                                        )
182
                                    ),
183
                                    'album' => array(
184
                                        'idColumn' => 'albumid',
185
                                        'mapper' => 'XML_Query2XML_ISO9075Mapper::map',
186
                                        'attributes' => array(
187
                                            'albumid'
188
                                        ),
189
                                        'elements' => array(
190
                                            'title',
191
                                            'published_year',
192
                                            'comment' => '?#Helper::summarizeComment(12)',
193
                                            'artist' => array(
194
                                                'idColumn' => 'artistid',
195
                                                'mapper' => 'mapArtist',
196
                                                'attributes' => array(
197
                                                    'artistid'
198
                                                ),
199
                                                'elements' => array(
200
                                                    'name',
201
                                                    'birth_year',
202
                                                    'birth_place',
203
                                                    'genre'
204
                                                )
205
                                            )
206
                                        ) // album elements
207
                                    ) //album array
208
                                ) //sales elements
209
                            ) //sales array
210
                        ) //employees elements
211
                    ) //employees array
212
                ) //department elements
213
            ) // department array
214
        ) //root elements
215
    ) //root
216
); //getXML method call
217
 
218
$root = $dom->firstChild;
219
$root->setAttribute('date_generated', '2005-08-23T14:52:50');
220
 
221
header('Content-Type: application/xml');
222
 
223
$dom->formatOutput = true;
224
print $dom->saveXML();
225
 
226
 
227
 
228
/**Static class that provides validation and parsing methods for
229
* generating XML.
230
*
231
* It is static so that we can easyly call its methods from inside
232
* Query2XML using eval'd code.
233
*/
234
class Helper
235
{
236
    /**Associative array of US postal state codes*/
237
    public static $statePostalCodes = array(
238
        'ALABAMA' => 'AL', 'ALASKA' => 'AK', 'AMERICAN SAMOA' => 'AS', 'ARIZONA' => 'AZ', 'ARKANSAS' => 'AR', 'CALIFORNIA' => 'CA',
239
        'COLORADO' => 'CO', 'CONNECTICUT' => 'CT', 'DELAWARE' => 'DE', 'DISTRICT OF COLUMBIA' => 'DC', 'FEDERATED STATES OF MICRONESIA' => 'FM',
240
        'FLORIDA' => 'FL', 'GEORGIA' => 'GA', 'GUAM' => 'GU', 'HAWAII' => 'HI', 'IDAHO' => 'ID', 'ILLINOIS' => 'IL', 'INDIANA' => 'IN',
241
        'IOWA' => 'IA', 'KANSAS' => 'KS', 'KENTUCKY' => 'KY', 'LOUISIANA' => 'LA', 'MAINE' => 'ME', 'MARSHALL ISLANDS' => 'MH', 'MARYLAND' => 'MD',
242
        'MASSACHUSETTS' => 'MA', 'MICHIGAN' => 'MI', 'MINNESOTA' => 'MN', 'MISSISSIPPI' => 'MS', 'MISSOURI' => 'MO', 'MONTANA' => 'MT',
243
        'NEBRASKA' => 'NE', 'NEVADA' => 'NV', 'NEW HAMPSHIRE' => 'NH', 'NEW JERSEY' => 'NJ', 'NEW JESEY' => 'NJ', 'NEW MEXICO' => 'NM', 'NEW YORK' => 'NY',
244
        'NORTH CAROLINA' => 'NC', 'NORTH DAKOTA' => 'ND', 'NORTHERN MARIANA ISLANDS' => 'MP', 'OHIO' => 'OH', 'OKLAHOMA' => 'OK', 'OREGON' => 'OR',
245
        'PALAU' => 'PW', 'PENNSYLVANIA' => 'PA', 'PUERTO RICO' => 'PR', 'RHODE ISLAND' => 'RI', 'SOUTH CAROLINA' => 'SC', 'SOUTH DAKOTA' => 'SD',
246
        'TENNESSEE' => 'TN', 'TEXAS' => 'TX', 'UTAH' => 'UT', 'VERMONT' => 'VT', 'VIRGIN ISLANDS' => 'VI', 'VIRGINIA' => 'VA', 'WASHINGTON' => 'WA',
247
        'WEST VIRGINIA' => 'WV', 'WISCONSIN' => 'WI', 'WYOMING' => 'WY'
248
    );
249
 
250
    /**Translates a US state name into its two-letter postal code.
251
    * If the translation fails, $state is returned unchanged
252
    * @param $record The record
253
    */
254
    public static function getStatePostalCode($record)
255
    {
256
        $state = $record["state"];
257
        $s = str_replace("  ", " ", trim(strtoupper($state)));
258
        if (isset(self::$statePostalCodes[$s])) {
259
            return self::$statePostalCodes[$s];
260
        } else {
261
            return $state;
262
        }
263
    }
264
 
265
    function summarize($str, $limit=50, $appendString=' ...')
266
    {
267
        if (strlen($str) > $limit) {
268
            $str = substr($str, 0, $limit - strlen($appendString)) . $appendString;
269
        }
270
        return $str;
271
    }
272
 
273
 
274
    function summarizeComment($record, $limit)
275
    {
276
        return self::summarize($record["comment"], $limit);
277
    }
278
}
279
?>