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

Source for file case08.php

Documentation is available at case08.php

  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('DB.php');
  34. $query2xml XML_Query2XML::factory(DB::connect('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.     array(
  92.         'rootTag' => 'music_company',
  93.         'rowTag' => 'store',
  94.         'idColumn' => 'storeid',
  95.         'mapper' => 'strtoupper',
  96.         'attributes' => array(
  97.             'storeid'
  98.         ),
  99.         'elements' => array(
  100.             'store_sales',
  101.             'store_employees',
  102.             'manager' => array(
  103.                 'idColumn' => 'manager_employeeid',
  104.                 'attributes' => array(
  105.                     'manager_employeeid'
  106.                 ),
  107.                 'elements' => array(
  108.                     'manager_employeename'
  109.                 )
  110.             ),
  111.             'address' => array(
  112.                 'elements' => array(
  113.                     'country',
  114.                     'state' => '!return Helper::getStatePostalCode($record["state"]);',
  115.                     'city',
  116.                     'street',
  117.                     'phone'
  118.                 )
  119.             ),
  120.             'department' => array(
  121.                 'idColumn' => 'departmentid',
  122.                 'mapper' => 'Mappers::departmentMapper',
  123.                 'attributes' => array(
  124.                     'departmentid'
  125.                 ),
  126.                 'elements' => array(
  127.                     'department_sales',
  128.                     'department_employees',
  129.                     'departmentname',
  130.                     'department_head' => array(
  131.                         'idColumn' => 'department_head_employeeid',
  132.                         'attributes' => array(
  133.                             'department_head_employeeid'
  134.                         ),
  135.                         'elements' => array(
  136.                             'department_head_employeename'
  137.                         )
  138.                     ),
  139.                     'employees' => array(
  140.                         'rootTag' => 'employees',
  141.                         'rowTag' => 'employee',
  142.                         'idColumn' => 'employeeid',
  143.                         'mapper' => array('Mappers''employeeMapper'),
  144.                         'attributes' => array(
  145.                             'employeeid'
  146.                         ),
  147.                         'elements' => array(
  148.                             'employeename',
  149.                             'sales' => array(
  150.                                 'rootTag' => 'sales',
  151.                                 'rowTag' => 'sale',
  152.                                 'idColumn' => 'saleid',
  153.                                 'mapper' => array($myMappers'saleMapper'),
  154.                                 'attributes' => array(
  155.                                     'saleid'
  156.                                 ),
  157.                                 'elements' => array(
  158.                                     'timestamp',
  159.                                     'customer' => array(
  160.                                         'idColumn' => 'customerid',
  161.                                         'mapper' => false,
  162.                                         'attributes' => array(
  163.                                             'customerid'
  164.                                         ),
  165.                                         'elements' => array(
  166.                                             'first_name',
  167.                                             'last_name',
  168.                                             'email'
  169.                                         )
  170.                                     ),
  171.                                     'album' => array(
  172.                                         'idColumn' => 'albumid',
  173.                                         'mapper' => 'XML_Query2XML_ISO9075Mapper::map',
  174.                                         'attributes' => array(
  175.                                             'albumid'
  176.                                         ),
  177.                                         'elements' => array(
  178.                                             'title',
  179.                                             'published_year',
  180.                                             'comment' => '?!return Helper::summarize($record["comment"], 12);',
  181.                                             'artist' => array(
  182.                                                 'idColumn' => 'artistid',
  183.                                                 'mapper' => 'mapArtist',
  184.                                                 'attributes' => array(
  185.                                                     'artistid'
  186.                                                 ),
  187.                                                 'elements' => array(
  188.                                                     'name',
  189.                                                     'birth_year',
  190.                                                     'birth_place',
  191.                                                     'genre'
  192.                                                 )
  193.                                             )
  194.                                         // album elements
  195.                                     //album array
  196.                                 //sales elements
  197.                             //sales array
  198.                         //employees elements
  199.                     //employees array
  200.                 //department elements
  201.             // department array
  202.         //root elements
  203.     //root
  204. )//getXML method call
  205.  
  206. $root $dom->firstChild;
  207. $root->setAttribute('date_generated'date("Y-m-d\TH:i:s"1124801570));
  208.  
  209. header('Content-Type: application/xml');
  210. echo '<?xml version="1.0" encoding="UTF-8"?>' "\n";
  211.  
  212. require_once('XML/Beautifier.php');
  213. $beautifier = new XML_Beautifier();
  214. print $beautifier->formatString($dom->saveXML());
  215.  
  216.  
  217.  
  218. /**Static class that provides validation and parsing methods for
  219. * generating XML.
  220. *
  221. * It is static so that we can easyly call its methods from inside
  222. * Query2XML using eval'd code.
  223. */
  224. class Helper
  225. {
  226.     /**Associative array of US postal state codes*/
  227.     public static $statePostalCodes = array(
  228.         'ALABAMA' => 'AL''ALASKA' => 'AK''AMERICAN SAMOA' => 'AS''ARIZONA' => 'AZ''ARKANSAS' => 'AR''CALIFORNIA' => 'CA',
  229.         'COLORADO' => 'CO''CONNECTICUT' => 'CT''DELAWARE' => 'DE''DISTRICT OF COLUMBIA' => 'DC''FEDERATED STATES OF MICRONESIA' => 'FM',
  230.         'FLORIDA' => 'FL''GEORGIA' => 'GA''GUAM' => 'GU''HAWAII' => 'HI''IDAHO' => 'ID''ILLINOIS' => 'IL''INDIANA' => 'IN',
  231.         'IOWA' => 'IA''KANSAS' => 'KS''KENTUCKY' => 'KY''LOUISIANA' => 'LA''MAINE' => 'ME''MARSHALL ISLANDS' => 'MH''MARYLAND' => 'MD',
  232.         'MASSACHUSETTS' => 'MA''MICHIGAN' => 'MI''MINNESOTA' => 'MN''MISSISSIPPI' => 'MS''MISSOURI' => 'MO''MONTANA' => 'MT',
  233.         'NEBRASKA' => 'NE''NEVADA' => 'NV''NEW HAMPSHIRE' => 'NH''NEW JERSEY' => 'NJ''NEW JESEY' => 'NJ''NEW MEXICO' => 'NM''NEW YORK' => 'NY',
  234.         'NORTH CAROLINA' => 'NC''NORTH DAKOTA' => 'ND''NORTHERN MARIANA ISLANDS' => 'MP''OHIO' => 'OH''OKLAHOMA' => 'OK''OREGON' => 'OR',
  235.         'PALAU' => 'PW''PENNSYLVANIA' => 'PA''PUERTO RICO' => 'PR''RHODE ISLAND' => 'RI''SOUTH CAROLINA' => 'SC''SOUTH DAKOTA' => 'SD',
  236.         'TENNESSEE' => 'TN''TEXAS' => 'TX''UTAH' => 'UT''VERMONT' => 'VT''VIRGIN ISLANDS' => 'VI''VIRGINIA' => 'VA''WASHINGTON' => 'WA',
  237.         'WEST VIRGINIA' => 'WV''WISCONSIN' => 'WI''WYOMING' => 'WY'
  238.     );
  239.             
  240.     /**Translates a US state name into its two-letter postal code.
  241.     * If the translation fails, $state is returned unchanged
  242.     * @param $state The state's name
  243.     */
  244.     public static function getStatePostalCode($state)
  245.     {
  246.         $s str_replace("  "" "trim(strtoupper($state)));
  247.         if (isset(self::$statePostalCodes[$s])) {
  248.             return self::$statePostalCodes[$s];
  249.         else {
  250.             return $state;
  251.         }
  252.     }
  253.       
  254.     function summarize($str$limit=50$appendString=' ...')
  255.     {
  256.         if (strlen($str$limit{
  257.             $str substr($str0$limit strlen($appendString)) $appendString;
  258.         }
  259.         return $str;
  260.     }
  261. }
  262. ?>

Documentation generated on Mon, 11 Mar 2019 14:39:32 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.