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

Source for file case08_debug.php

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

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