XML_Query2XML
[ class tree: XML_Query2XML ] [ index: XML_Query2XML ] [ all elements ]

Source for file case06.php

Documentation is available at case06.php

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

Documentation generated on Mon, 11 Mar 2019 15:43:31 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.