| 1 |
lars |
1 |
--TEST--
|
|
|
2 |
XML_Query2XML::registerPrefix()
|
|
|
3 |
--SKIPIF--
|
|
|
4 |
<?php require_once dirname(dirname(__FILE__)) . '/skipif.php'; ?>
|
|
|
5 |
--FILE--
|
|
|
6 |
<?php
|
|
|
7 |
require_once 'XML/Query2XML.php';
|
|
|
8 |
require_once 'XML/Query2XML/Data/Processor.php';
|
|
|
9 |
require_once dirname(dirname(__FILE__)) . '/db_init.php';
|
|
|
10 |
|
|
|
11 |
class Year2UnixTime extends XML_Query2XML_Data_Processor
|
|
|
12 |
{
|
|
|
13 |
/**
|
|
|
14 |
* Create a new instance of this class.
|
|
|
15 |
*
|
|
|
16 |
* @param mixed $preProcessor The pre-processor to be used. An instance of
|
|
|
17 |
* XML_Query2XML_Data or null.
|
|
|
18 |
* @param string $configPath The configuration path within the $options
|
|
|
19 |
* array.
|
|
|
20 |
*
|
|
|
21 |
* @return XML_Query2XML_Data_Processor_Base64
|
|
|
22 |
*/
|
|
|
23 |
public function create($preProcessor, $configPath)
|
|
|
24 |
{
|
|
|
25 |
$processor = new Year2UnixTime($preProcessor);
|
|
|
26 |
$processor->setConfigPath($configPath);
|
|
|
27 |
return $processor;
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Called by XML_Query2XML for every record in the result set.
|
|
|
32 |
*
|
|
|
33 |
* @param array $record An associative array.
|
|
|
34 |
*
|
|
|
35 |
* @return string The base64-encoded version the string returned
|
|
|
36 |
* by the pre-processor.
|
|
|
37 |
* @throws XML_Query2XML_ConfigException If the pre-processor returns
|
|
|
38 |
* something that cannot be converted to a string
|
|
|
39 |
* (i.e. an object or an array).
|
|
|
40 |
*/
|
|
|
41 |
public function execute(array $record)
|
|
|
42 |
{
|
|
|
43 |
$data = $this->runPreProcessor($record);
|
|
|
44 |
if (is_array($data) || is_object($data)) {
|
|
|
45 |
throw new XML_Query2XML_ConfigException(
|
|
|
46 |
$this->getConfigPath()
|
|
|
47 |
. ': XML_Query2XML_Data_Processor_Base64: string '
|
|
|
48 |
. 'expected from pre-processor, but ' . gettype($data) . ' returned.'
|
|
|
49 |
);
|
|
|
50 |
}
|
|
|
51 |
return DateTime::createFromFormat('Y-m-d H:i:s', $data . '-01-01 00:00:00', new DateTimeZone('Etc/GMT+0'))->format('U');
|
|
|
52 |
}
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
$query2xml = XML_Query2XML::factory($db);
|
|
|
56 |
|
|
|
57 |
$query2xml->registerPrefix('-', 'Year2UnixTime');
|
|
|
58 |
|
|
|
59 |
$dom = $query2xml->getXML(
|
|
|
60 |
"SELECT * FROM artist",
|
|
|
61 |
array(
|
|
|
62 |
'rootTag' => 'artists',
|
|
|
63 |
'idColumn' => 'artistid',
|
|
|
64 |
'rowTag' => 'artist',
|
|
|
65 |
'elements' => array(
|
|
|
66 |
'name',
|
|
|
67 |
'birth_year',
|
|
|
68 |
'birth_year_in_unix_time' => '-birth_year'
|
|
|
69 |
)
|
|
|
70 |
)
|
|
|
71 |
);
|
|
|
72 |
|
|
|
73 |
header('Content-Type: application/xml');
|
|
|
74 |
$dom->formatOutput = true;
|
|
|
75 |
print $dom->saveXML();
|
|
|
76 |
?>
|
|
|
77 |
--EXPECT--
|
|
|
78 |
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
79 |
<artists>
|
|
|
80 |
<artist>
|
|
|
81 |
<name>Curtis Mayfield</name>
|
|
|
82 |
<birth_year>1920</birth_year>
|
|
|
83 |
<birth_year_in_unix_time>-1577923200</birth_year_in_unix_time>
|
|
|
84 |
</artist>
|
|
|
85 |
<artist>
|
|
|
86 |
<name>Isaac Hayes</name>
|
|
|
87 |
<birth_year>1942</birth_year>
|
|
|
88 |
<birth_year_in_unix_time>-883612800</birth_year_in_unix_time>
|
|
|
89 |
</artist>
|
|
|
90 |
<artist>
|
|
|
91 |
<name>Ray Charles</name>
|
|
|
92 |
<birth_year>1930</birth_year>
|
|
|
93 |
<birth_year_in_unix_time>-1262304000</birth_year_in_unix_time>
|
|
|
94 |
</artist>
|
|
|
95 |
</artists>
|