Source for file jDaoParser.class.php

Documentation is available at jDaoParser.class.php

  1. <?php
  2. /**
  3. * @package     jelix
  4. * @subpackage  dao
  5. * @author      GĂ©rald Croes, Laurent Jouanneau
  6. * @contributor Laurent Jouanneau
  7. * @copyright   2001-2005 CopixTeam, 2005-2006 Laurent Jouanneau
  8. *  This class was get originally from the Copix project (CopixDAODefinitionV1, Copix 2.3dev20050901, http://www.copix.org)
  9. *  Few lines of code are still copyrighted 2001-2005 CopixTeam (LGPL licence).
  10. *  Initial authors of this Copix class are Gerald Croes and Laurent Jouanneau,
  11. *  and this class was adapted/improved for Jelix by Laurent Jouanneau
  12. *
  13. * @link        http://www.jelix.org
  14. * @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  15. */
  16.  
  17. /**
  18.  * extract data from a dao xml content
  19.  * @package  jelix
  20.  * @subpackage dao
  21.  * @see jDaoCompiler
  22.  */
  23. class jDaoParser {
  24.     /**
  25.     * the properties list.
  26.     * keys = field code name
  27.     * values = jDaoProperty
  28.     */
  29.     private $_properties = array ();
  30.  
  31.     /**
  32.     * all tables with their properties, and their own fields
  33.     * keys = table code name
  34.     * values = array()
  35.     *          'name'=> table code name, 'realname'=>'real table name',
  36.     *          'primarykey'=> attribute , 'pk'=> primary keys list
  37.     *          'onforeignkey'=> attribute, 'fk'=> foreign keys list
  38.     *          'fields'=>array(list of field code name)
  39.     */
  40.     private $_tables = array();
  41.  
  42.     /**
  43.     * primary table code name
  44.     */
  45.     private $_primaryTable = '';
  46.  
  47.     /**
  48.     * code name of foreign table with a outer join
  49.     * @var array  of table code name
  50.     */
  51.     private $_ojoins = array ();
  52.  
  53.     /**
  54.     * code name of foreign table with a inner join
  55.     * @var array  of array(table code name, 0)
  56.     */
  57.     private $_ijoins = array ();
  58.  
  59.     /**
  60.      * @var array list of jDaoMethod objects
  61.      */
  62.     private $_methods = array();
  63.  
  64.     /**
  65.      * list of main events to sent
  66.      */
  67.     private $_eventList = array();
  68.  
  69.     public $hasOnlyPrimaryKeys = false;
  70.     
  71.     public $selector;
  72.     /**
  73.     * Constructor
  74.     */
  75.     function __construct($selector) {
  76.         $this->selector = $selector;
  77.     }
  78.  
  79.     /**
  80.     * parse a dao xml content
  81.     * @param SimpleXmlElement $xml 
  82.     * @param jDbTools $tools 
  83.     * @param int $debug  for debug only 0:parse all, 1:parse only datasource+record, 2;parse only datasource
  84.     */
  85.     public function parse( $xml, $tools){
  86.         $this->parseDatasource($xml);
  87.         $this->parseRecord($xml, $tools);
  88.         $this->parseFactory($xml);
  89.     }
  90.     
  91.     protected function parseDatasource($xml) {
  92.         // -- tables
  93.         if(isset ($xml->datasources) && isset ($xml->datasources[0]->primarytable)){
  94.             $t = $this->_parseTable (0, $xml->datasources[0]->primarytable[0]);
  95.             $this->_primaryTable = $t['name'];
  96.             if(isset($xml->datasources[0]->primarytable[1])){
  97.                 throw new jDaoXmlException ($this->selector, 'table.two.many');
  98.             }
  99.             foreach($xml->datasources[0]->foreigntable as $table){
  100.                 $this->_parseTable (1, $table);
  101.             }
  102.             foreach($xml->datasources[0]->optionalforeigntable as $table){
  103.                 $this->_parseTable (2, $table);
  104.             }
  105.         }else{
  106.             throw new jDaoXmlException ($this->selector, 'datasource.missing');
  107.         }
  108.     }
  109.     
  110.     protected function parseRecord($xml, $tools) {
  111.         $countprop = 0;
  112.         //add the record properties
  113.         if(isset($xml->record) && isset($xml->record[0]->property)){
  114.             foreach ($xml->record[0]->property as $prop){
  115.                 $p = new jDaoProperty ($prop->attributes(), $this, $tools);
  116.                 $this->_properties[$p->name] = $p;
  117.                 $this->_tables[$p->table]['fields'][] = $p->name;
  118.                 if($p->ofPrimaryTable && !$p->isPK)
  119.                     $countprop ++;
  120.             }
  121.             $this->hasOnlyPrimaryKeys = ($countprop == 0);
  122.         }else
  123.             throw new jDaoXmlException ($this->selector, 'properties.missing');
  124.     }
  125.     
  126.     protected function parseFactory($xml) {
  127.         // get additionnal methods definition
  128.         if (isset ($xml->factory)) {
  129.             if (isset($xml->factory[0]['events'])) {
  130.                 $events = (string)$xml->factory[0]['events'];
  131.                 $this->_eventList = preg_split("/[\s,]+/", $events);
  132.             }
  133.  
  134.             if (isset($xml->factory[0]->method)){
  135.                 foreach($xml->factory[0]->method as $method){
  136.                     $m = new jDaoMethod ($method, $this);
  137.                     if(isset ($this->_methods[$m->name])){
  138.                         throw new jDaoXmlException ($this->selector, 'method.duplicate',$m->name);
  139.                     }
  140.                     $this->_methods[$m->name] = $m;
  141.                 }
  142.             }
  143.         }
  144.     }
  145.  
  146.     /**
  147.     * parse a join definition
  148.     */
  149.     private function _parseTable ($typetable, $tabletag){
  150.         $infos = $this->getAttr($tabletag, array('name','realname','primarykey','onforeignkey'));
  151.  
  152.         if ($infos['name'] === null )
  153.             throw new jDaoXmlException ($this->selector, 'table.name');
  154.  
  155.         if($infos['realname'] === null)
  156.             $infos['realname'] = $infos['name'];
  157.  
  158.         if($infos['primarykey'] === null)
  159.             throw new jDaoXmlException ($this->selector, 'primarykey.missing');
  160.  
  161.         $infos['pk']= preg_split("/[\s,]+/", $infos['primarykey']);
  162.         unset($infos['primarykey']);
  163.  
  164.         if(count($infos['pk']) == 0 || $infos['pk'][0] == '')
  165.             throw new jDaoXmlException ($this->selector, 'primarykey.missing');
  166.  
  167.         if($typetable){ // for the foreigntable and optionalforeigntable
  168.             if($infos['onforeignkey'] === null)
  169.                 throw new jDaoXmlException ($this->selector, 'foreignkey.missing');
  170.             $infos['fk']=preg_split("/[\s,]+/",$infos['onforeignkey']);
  171.             unset($infos['onforeignkey']);
  172.             if(count($infos['fk']) == 0 || $infos['fk'][0] == '')
  173.                 throw new jDaoXmlException ($this->selector, 'foreignkey.missing');
  174.             if(count($infos['fk']) != count($infos['pk']))
  175.                 throw new jDaoXmlException ($this->selector, 'foreignkey.missing');
  176.             if($typetable == 1){
  177.                 $this->_ijoins[]=$infos['name'];
  178.             }else{
  179.                 $this->_ojoins[]=array($infos['name'],0);
  180.             }
  181.         }else{
  182.             unset($infos['onforeignkey']);
  183.         }
  184.  
  185.         $infos['fields'] = array ();
  186.         $this->_tables[$infos['name']] = $infos;
  187.  
  188.         return $infos;
  189.     }
  190.  
  191.     /**
  192.     * Try to read all given attributes
  193.     * @param SimpleXmlElement $tag 
  194.     * @param array $requiredattr attributes list
  195.     * @return array attributes and their values
  196.     */
  197.     public function getAttr($tag, $requiredattr){
  198.         $res=array();
  199.         foreach($requiredattr as $attr){
  200.             if(isset($tag[$attr]) && trim((string)$tag[$attr]) != '')
  201.                 $res[$attr]=(string)$tag[$attr];
  202.             else
  203.                 $res[$attr]=null;
  204.         }
  205.         return $res;
  206.     }
  207.  
  208.     /**
  209.     * just a quick way to retrieve boolean values from a string.
  210.     *  will accept yes, true, 1 as "true" values
  211.     *  all other values will be considered as false.
  212.     * @return boolean true / false
  213.     */
  214.     public function getBool ($value) {
  215.         return in_array (trim ($value), array ('true', '1', 'yes'));
  216.     }
  217.  
  218.     public function getProperties () { return $this->_properties; }
  219.     public function getTables(){  return $this->_tables;}
  220.     public function getPrimaryTable(){  return $this->_primaryTable;}
  221.     public function getMethods(){  return $this->_methods;}
  222.     public function getOuterJoins(){  return $this->_ojoins;}
  223.     public function getInnerJoins(){  return $this->_ijoins;}
  224.     public function hasEvent($event){ return in_array($event,$this->_eventList);}
  225. }

Documentation generated on Mon, 26 Oct 2015 21:52:51 +0100 by phpDocumentor 1.4.3