Source for file jDaoProperty.class.php

Documentation is available at jDaoProperty.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-2011 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.  * Container for properties of a dao property
  19.  * @package  jelix
  20.  * @subpackage dao
  21.  */
  22.  
  23. class jDaoProperty {
  24.     /**
  25.     * the name of the property of the object
  26.     */
  27.     public $name = '';
  28.  
  29.     /**
  30.     * the name of the field in table
  31.     */
  32.     public $fieldName = '';
  33.  
  34.     /**
  35.     * give the regular expression that needs to be matched against.
  36.     * @var string 
  37.     */
  38.     public $regExp = null;
  39.  
  40.     /**
  41.     * says if the field is required when doing a check
  42.     * @var boolean 
  43.     */
  44.     public $required = false;
  45.  
  46.     /**
  47.     * says if the value of the field is required when construct SQL conditions
  48.     * @var boolean 
  49.     */
  50.     public $requiredInConditions = false;
  51.  
  52.     /**
  53.     * Says if it's a primary key.
  54.     * @var boolean 
  55.     */
  56.     public $isPK = false;
  57.  
  58.     /**
  59.     * Says if it's a foreign key
  60.     * @var boolean 
  61.     */
  62.     public $isFK = false;
  63.  
  64.     public $datatype;
  65.  
  66.     public $unifiedType;
  67.  
  68.     public $table=null;
  69.     public $updatePattern='%s';
  70.     public $insertPattern='%s';
  71.     public $selectPattern='%s';
  72.     public $sequenceName='';
  73.  
  74.     /**
  75.     * the maxlength of the key if given
  76.     * @var int 
  77.     */
  78.     public $maxlength = null;
  79.     public $minlength = null;
  80.  
  81.     public $ofPrimaryTable = true;
  82.  
  83.     public $defaultValue = null;
  84.  
  85.     public $autoIncrement = false;
  86.     /**
  87.     * constructor.
  88.     * @param array  $attributes  list of attributes of a simpleXmlElement
  89.     * @param jDaoParser $parser the parser on the dao file
  90.     * @param jDbTools $tools 
  91.     */
  92.     function __construct ($aAttributes, $parser, $tools){
  93.         $needed = array('name', 'fieldname', 'table', 'datatype', 'required',
  94.                         'minlength', 'maxlength', 'regexp', 'sequence', 'default','autoincrement');
  95.  
  96.         $params = $parser->getAttr($aAttributes, $needed);
  97.  
  98.         if ($params['name']===null){
  99.             throw new jDaoXmlException ($parser->selector, 'missing.attr', array('name', 'property'));
  100.         }
  101.         $this->name       = $params['name'];
  102.  
  103.         if(!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $this->name)){
  104.             throw new jDaoXmlException ($parser->selector, 'property.invalid.name', $this->name);
  105.         }
  106.  
  107.  
  108.         $this->fieldName  = $params['fieldname'] !==null ? $params['fieldname'] : $this->name;
  109.         $this->table      = $params['table'] !==null ? $params['table'] : $parser->getPrimaryTable();
  110.  
  111.         $tables = $parser->getTables();
  112.  
  113.         if(!isset( $tables[$this->table])){
  114.             throw new jDaoXmlException ($parser->selector, 'property.unknown.table', $this->name);
  115.         }
  116.  
  117.         $this->required   = $this->requiredInConditions = $parser->getBool ($params['required']);
  118.         $this->maxlength  = $params['maxlength'] !== null ? intval($params['maxlength']) : null;
  119.         $this->minlength  = $params['minlength'] !== null ? intval($params['minlength']) : null;
  120.         $this->regExp     = $params['regexp'];
  121.         $this->autoIncrement = $parser->getBool ($params['autoincrement']);
  122.  
  123.         if ($params['datatype']===null){
  124.             throw new jDaoXmlException ($parser->selector, 'missing.attr', array('datatype', 'property'));
  125.         }
  126.         $params['datatype'] = trim(strtolower($params['datatype']));
  127.  
  128.         if ($params['datatype'] == '') {
  129.             throw new jDaoXmlException ($parser->selector, 'wrong.attr', array($params['datatype'],
  130.                                                            $this->fieldName,
  131.                                                            'property'));
  132.         }
  133.         $this->datatype = strtolower($params['datatype']);
  134.  
  135.         $ti = $tools->getTypeInfo($this->datatype);
  136.         $this->unifiedType = $ti[1];
  137.         if (!$this->autoIncrement)
  138.             $this->autoIncrement = $ti[6];
  139.  
  140.         if ($this->unifiedType == 'integer' || $this->unifiedType == 'numeric') {
  141.             if ($params['sequence'] !== null) {
  142.                 $this->sequenceName = $params['sequence'];
  143.                 $this->autoIncrement = true;
  144.             }
  145.         }
  146.  
  147.         $this->isPK = in_array($this->fieldName, $tables[$this->table]['pk']);
  148.         if(!$this->isPK && $this->table == $parser->getPrimaryTable()){
  149.             foreach($tables as $table=>$info) {
  150.                 if ($table == $this->table)
  151.                     continue;
  152.                 if (isset($info['fk']) && in_array($this->fieldName, $info['fk'])) {
  153.                     $this->isFK = true;
  154.                     break;
  155.                 }
  156.             }
  157.         }
  158.         else {
  159.             $this->required = true;
  160.             $this->requiredInConditions = true;
  161.         }
  162.  
  163.         if ($this->autoIncrement) {
  164.             $this->required = false;
  165.             $this->requiredInConditions = true;
  166.         }
  167.  
  168.         if ($params['default'] !== null) {
  169.             $this->defaultValue = $tools->stringToPhpValue($this->unifiedType, $params['default']);
  170.         }
  171.  
  172.         // insertpattern is allowed on primary keys noy autoincremented
  173.         if ($this->isPK && !$this->autoIncrement && isset($aAttributes['insertpattern'])) {
  174.             $this->insertPattern=(string)$aAttributes['insertpattern'];
  175.         }
  176.         if ($this->isPK) {
  177.             $this->updatePattern = '';
  178.         }
  179.         // we ignore *pattern attributes on PK and FK fields
  180.         if (!$this->isPK && !$this->isFK) {
  181.             if(isset($aAttributes['updatepattern'])) {
  182.                 $this->updatePattern=(string)$aAttributes['updatepattern'];
  183.             }
  184.  
  185.             if(isset($aAttributes['insertpattern'])) {
  186.                 $this->insertPattern=(string)$aAttributes['insertpattern'];
  187.             }
  188.  
  189.             if(isset($aAttributes['selectpattern'])) {
  190.                 $this->selectPattern=(string)$aAttributes['selectpattern'];
  191.             }
  192.         }
  193.  
  194.         // no update and insert patterns for field of external tables
  195.         if ($this->table != $parser->getPrimaryTable()) {
  196.             $this->updatePattern = '';
  197.             $this->insertPattern = '';
  198.             $this->required = false;
  199.             $this->requiredInConditions = false;
  200.             $this->ofPrimaryTable = false;
  201.         }
  202.         else {
  203.             $this->ofPrimaryTable=true;
  204.         }
  205.     }
  206. }

Documentation generated on Thu, 19 Sep 2013 00:03:35 +0200 by phpDocumentor 1.4.3