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. * @contributor Philippe Villiers
  8. * @copyright   2001-2005 CopixTeam, 2005-2011 Laurent Jouanneau
  9. *  This class was get originally from the Copix project (CopixDAODefinitionV1, Copix 2.3dev20050901, http://www.copix.org)
  10. *  Few lines of code are still copyrighted 2001-2005 CopixTeam (LGPL licence).
  11. *  Initial authors of this Copix class are Gerald Croes and Laurent Jouanneau,
  12. *  and this class was adapted/improved for Jelix by Laurent Jouanneau
  13. *
  14. * @link        http://www.jelix.org
  15. * @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  16. */
  17.  
  18. /**
  19.  * Container for properties of a dao property
  20.  * @package  jelix
  21.  * @subpackage dao
  22.  */
  23.  
  24. class jDaoProperty {
  25.     /**
  26.     * the name of the property of the object
  27.     */
  28.     public $name = '';
  29.  
  30.     /**
  31.     * the name of the field in table
  32.     */
  33.     public $fieldName = '';
  34.  
  35.     /**
  36.     * give the regular expression that needs to be matched against.
  37.     * @var string 
  38.     */
  39.     public $regExp = null;
  40.  
  41.     /**
  42.     * says if the field is required when doing a check
  43.     * @var boolean 
  44.     */
  45.     public $required = false;
  46.  
  47.     /**
  48.     * says if the value of the field is required when construct SQL conditions
  49.     * @var boolean 
  50.     */
  51.     public $requiredInConditions = false;
  52.  
  53.     /**
  54.     * Says if it's a primary key.
  55.     * @var boolean 
  56.     */
  57.     public $isPK = false;
  58.  
  59.     /**
  60.     * Says if it's a foreign key
  61.     * @var boolean 
  62.     */
  63.     public $isFK = false;
  64.  
  65.     public $datatype;
  66.  
  67.     public $unifiedType;
  68.  
  69.     public $table=null;
  70.     public $updatePattern='%s';
  71.     public $insertPattern='%s';
  72.     public $selectPattern='%s';
  73.     public $sequenceName='';
  74.  
  75.     /**
  76.     * the maxlength of the key if given
  77.     * @var int 
  78.     */
  79.     public $maxlength = null;
  80.     public $minlength = null;
  81.  
  82.     public $ofPrimaryTable = true;
  83.  
  84.     public $defaultValue = null;
  85.  
  86.     public $autoIncrement = false;
  87.  
  88.     /**
  89.     * comment field / eg : use to form's label
  90.     * @var string 
  91.     */
  92.     public $comment = '';
  93.  
  94.     /**
  95.     * constructor.
  96.     * @param array  $attributes  list of attributes of a simpleXmlElement
  97.     * @param jDaoParser $parser the parser on the dao file
  98.     * @param jDbTools $tools 
  99.     */
  100.     function __construct ($aAttributes, $parser, $tools){
  101.         $needed = array('name', 'fieldname', 'table', 'datatype', 'required',
  102.                         'minlength', 'maxlength', 'regexp', 'sequence', 'default', 'autoincrement');
  103.  
  104.         // Allowed attributes names
  105.         $allowed = array('name', 'fieldname', 'table', 'datatype', 'required',
  106.                         'minlength', 'maxlength', 'regexp', 'sequence', 'default', 'autoincrement',
  107.                         'updatepattern', 'insertpattern', 'selectpattern','comment');
  108.  
  109.         foreach($aAttributes as $attributeName => $attributeValue) {
  110.             if(!in_array($attributeName, $allowed)) {
  111.                 throw new jDaoXmlException ($parser->selector, 'unknown.attr', array($attributeName, 'property'));
  112.             }
  113.         }
  114.  
  115.         $params = $parser->getAttr($aAttributes, $needed);
  116.  
  117.         if ($params['name']===null){
  118.             throw new jDaoXmlException ($parser->selector, 'missing.attr', array('name', 'property'));
  119.         }
  120.         $this->name       = $params['name'];
  121.  
  122.         if(!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $this->name)){
  123.             throw new jDaoXmlException ($parser->selector, 'property.invalid.name', $this->name);
  124.         }
  125.  
  126.         $this->fieldName  = $params['fieldname'] !==null ? $params['fieldname'] : $this->name;
  127.         $this->table      = $params['table'] !==null ? $params['table'] : $parser->getPrimaryTable();
  128.  
  129.         $tables = $parser->getTables();
  130.  
  131.         if(!isset( $tables[$this->table])){
  132.             throw new jDaoXmlException ($parser->selector, 'property.unknown.table', $this->name);
  133.         }
  134.  
  135.         $this->required   = $this->requiredInConditions = $parser->getBool ($params['required']);
  136.         $this->maxlength  = $params['maxlength'] !== null ? intval($params['maxlength']) : null;
  137.         $this->minlength  = $params['minlength'] !== null ? intval($params['minlength']) : null;
  138.         $this->regExp     = $params['regexp'];
  139.         $this->autoIncrement = $parser->getBool ($params['autoincrement']);
  140.  
  141.         if ($params['datatype']===null){
  142.             throw new jDaoXmlException ($parser->selector, 'missing.attr', array('datatype', 'property'));
  143.         }
  144.         $params['datatype'] = trim(strtolower($params['datatype']));
  145.  
  146.         if ($params['datatype'] == '') {
  147.             throw new jDaoXmlException ($parser->selector, 'wrong.attr', array($params['datatype'],
  148.                                                            $this->fieldName,
  149.                                                            'property'));
  150.         }
  151.         $this->datatype = strtolower($params['datatype']);
  152.  
  153.         $ti = $tools->getTypeInfo($this->datatype);
  154.         $this->unifiedType = $ti[1];
  155.         if (!$this->autoIncrement)
  156.             $this->autoIncrement = $ti[6];
  157.  
  158.         if ($this->unifiedType == 'integer' || $this->unifiedType == 'numeric') {
  159.             if ($params['sequence'] !== null) {
  160.                 $this->sequenceName = $params['sequence'];
  161.                 $this->autoIncrement = true;
  162.             }
  163.         }
  164.  
  165.         $this->isPK = in_array($this->fieldName, $tables[$this->table]['pk']);
  166.         if(!$this->isPK && $this->table == $parser->getPrimaryTable()){
  167.             foreach($tables as $table=>$info) {
  168.                 if ($table == $this->table)
  169.                     continue;
  170.                 if (isset($info['fk']) && in_array($this->fieldName, $info['fk'])) {
  171.                     $this->isFK = true;
  172.                     break;
  173.                 }
  174.             }
  175.         }
  176.         else {
  177.             $this->required = true;
  178.             $this->requiredInConditions = true;
  179.         }
  180.  
  181.         if ($this->autoIncrement) {
  182.             $this->required = false;
  183.             $this->requiredInConditions = true;
  184.         }
  185.  
  186.         if ($params['default'] !== null) {
  187.             $this->defaultValue = $tools->stringToPhpValue($this->unifiedType, $params['default']);
  188.         }
  189.  
  190.         // insertpattern is allowed on primary keys noy autoincremented
  191.         if ($this->isPK && !$this->autoIncrement && isset($aAttributes['insertpattern'])) {
  192.             $this->insertPattern=(string)$aAttributes['insertpattern'];
  193.         }
  194.         if ($this->isPK) {
  195.             $this->updatePattern = '';
  196.         }
  197.         // we ignore *pattern attributes on PK and FK fields
  198.         if (!$this->isPK && !$this->isFK) {
  199.             if(isset($aAttributes['updatepattern'])) {
  200.                 $this->updatePattern=(string)$aAttributes['updatepattern'];
  201.             }
  202.  
  203.             if(isset($aAttributes['insertpattern'])) {
  204.                 $this->insertPattern=(string)$aAttributes['insertpattern'];
  205.             }
  206.  
  207.             if(isset($aAttributes['selectpattern'])) {
  208.                 $this->selectPattern=(string)$aAttributes['selectpattern'];
  209.             }
  210.         }
  211.  
  212.  
  213.         // no update and insert patterns for field of external tables
  214.         if ($this->table != $parser->getPrimaryTable()) {
  215.             $this->updatePattern = '';
  216.             $this->insertPattern = '';
  217.             $this->required = false;
  218.             $this->requiredInConditions = false;
  219.             $this->ofPrimaryTable = false;
  220.         }
  221.         else {
  222.             $this->ofPrimaryTable=true;
  223.         }
  224.  
  225.         // field comment
  226.         if(isset($aAttributes['comment'])) {
  227.             $this->comment=(string)$aAttributes['comment'];
  228.         }
  229.  
  230.     }
  231. }

Documentation generated on Wed, 04 Jan 2017 22:53:25 +0100 by phpDocumentor 1.4.3