[ PHPXref.com ] [ Generated: Sun Jul 20 19:05:09 2008 ] [ onPHP 0.4.6 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/meta/builders/ -> BaseBuilder.class.php (source)

   1  <?php
   2  /***************************************************************************
   3   *   Copyright (C) 2006 by Konstantin V. Arkhipov                          *
   4   *                                                                         *
   5   *   This program is free software; you can redistribute it and/or modify  *
   6   *   it under the terms of the GNU General Public License as published by  *
   7   *   the Free Software Foundation; either version 2 of the License, or     *
   8   *   (at your option) any later version.                                   *
   9   *                                                                         *
  10   ***************************************************************************/
  11  /* $Id: BaseBuilder.class.php 1684 2006-06-10 20:58:39Z voxus $ */
  12  
  13      /**
  14       * @ingroup Builders
  15      **/
  16      abstract class BaseBuilder extends StaticFactory
  17      {
  18          abstract public static function build(MetaClass $class);
  19          
  20  		protected static function buildFillers(MetaClass $class)
  21          {
  22              $out = null;
  23              
  24              $className = $class->getName();
  25              $varName = strtolower($className[0]).substr($className, 1);
  26  
  27              $setters = array();
  28              
  29              $standaloneFillers = array();
  30              $chainFillers = array();
  31              
  32              if ($class->getParent())
  33                  $setterIndent = 4;
  34              else
  35                  $setterIndent = 5;
  36              
  37              foreach ($class->getProperties() as $property) {
  38                  
  39                  $filler = $property->toDaoSetter($className);
  40                  
  41                  if ($filler !== null) {
  42                      
  43                      $setters[] = $property->toDaoField($className, $setterIndent);
  44                      
  45                      if (
  46                          (
  47                              !$property->getType()->isGeneric()
  48                              || $property->getType() instanceof ObjectType
  49                          )
  50                          && !$property->isRequired()
  51                      )
  52                          $standaloneFillers[] =
  53                              implode(
  54                                  "\n",
  55                                  explode("\n", $filler)
  56                              );
  57                      else
  58                          $chainFillers[] =
  59                              implode(
  60                                  "\n",
  61                                  explode("\n", $filler)
  62                              );
  63                  }
  64              }
  65              
  66              $out .= implode("->\n", $setters).";\n";
  67  
  68              $out .= <<<EOT
  69          }
  70  
  71  EOT;
  72  
  73              if (
  74                  $class->getPattern() instanceof StraightMappingPattern
  75                  || $class->getPattern() instanceof DictionaryClassPattern
  76              ) {
  77                  $out .= <<<EOT
  78              
  79  public function makeObject(&\$array, \$prefix = null)
  80  {
  81      return \$this->fillObject(new {$className}(), \$array, \$prefix);
  82  }
  83  
  84  EOT;
  85              } else {
  86                  $out .= <<<EOT
  87                  
  88  // there is no makeObject because of abstract nature of meta-class
  89                  
  90  EOT;
  91              }
  92              
  93              $out .= <<<EOT
  94  
  95  protected function fillObject(/* {$className} */ \${$varName}, &\$array, \$prefix = null)
  96  {
  97  
  98  EOT;
  99              if ($class->getParent()) {
 100                  $out .= <<<EOT
 101  parent::fillObject(\${$varName}, \$array, \$prefix);
 102  
 103  
 104  EOT;
 105              }
 106              
 107              if ($chainFillers) {
 108                  
 109                  $out .= "\${$varName}->\n";
 110                  
 111                  $out .= implode("->\n", $chainFillers).";\n\n";
 112              }
 113              
 114              if ($standaloneFillers) {
 115                  $out .= implode("\n", $standaloneFillers)."\n";
 116              }
 117  
 118              $out .= <<<EOT
 119              return \${$varName};
 120          }
 121      }
 122  
 123  EOT;
 124              return $out;
 125          }
 126          
 127  		protected static function buildPointers(MetaClass $class)
 128          {
 129              return <<<EOT
 130  public function getTable()
 131  {
 132      return '{$class->getDumbName()}';
 133  }
 134  
 135  public function getObjectName()
 136  {
 137      return '{$class->getName()}';
 138  }
 139  
 140  public function getSequence()
 141  {
 142      return '{$class->getDumbName()}_id';
 143  }
 144  EOT;
 145          }
 146          
 147  		protected static function buildMapping(MetaClass $class)
 148          {
 149              $mapping = array();
 150              
 151              foreach ($class->getProperties() as $property) {
 152                  
 153                  $row = null;
 154                  
 155                  if ($property->getType()->isGeneric()) {
 156                      
 157                      $name = $property->getName();
 158                      $dumbName = $property->getDumbName();
 159                      
 160                      if ($property->getType() instanceof RangeType) {
 161                          
 162                          $row =
 163                              array(
 164                                  "'{$name}Min' => '{$dumbName}_min'",
 165                                  "'{$name}Max' => '{$dumbName}_max'"
 166                              );
 167                          
 168                      } else {
 169                          if ($name == $dumbName)
 170                              $map = 'null';
 171                          else
 172                              $map = "'{$dumbName}'";
 173                          
 174                          $row .= "'{$name}' => {$map}";
 175                      }
 176                  } else {
 177                      
 178                      $relation = $property->getRelation();
 179                      
 180                      if (
 181                          $relation->getId() == MetaRelation::ONE_TO_ONE
 182                      ) {
 183                          $remoteClass =
 184                              MetaConfiguration::me()->
 185                              getClassByName(
 186                                  $property->getType()->getClass()
 187                              );
 188                          
 189                          $identifier = $remoteClass->getIdentifier();
 190                          
 191                          $row .=
 192                              "'{$property->getName()}".ucfirst($identifier->getName())
 193                              ."' => '{$property->getDumbName()}_"
 194                              ."{$identifier->getDumbName()}'";
 195                      } else
 196                          $row = null;
 197                  }
 198                  
 199                  if ($row) {
 200                      if (is_array($row))
 201                          $mapping = array_merge($mapping, $row);
 202                      else // string
 203                          $mapping[] = $row;
 204                  }
 205              }
 206              
 207              return $mapping;
 208          }
 209          
 210  		protected static function getHead()
 211          {
 212              $head = self::startCap();
 213              
 214              $head .=
 215                  ' *   This file is autogenerated - do not edit.'
 216                  .'                               *';
 217  
 218              return $head."\n".self::endCap();
 219          }
 220          
 221  		protected static function startCap()
 222          {
 223              $version = ONPHP_VERSION;
 224              $date = date('Y-m-d H:i:s');
 225              
 226              $info = " *   Generated by onPHP-{$version} at {$date}";
 227              $info = str_pad($info, 77, ' ', STR_PAD_RIGHT).'*';
 228              
 229              $cap = <<<EOT
 230  <?php
 231  /*****************************************************************************
 232   *   Copyright (C) 2006, onPHP's MetaConfiguration Builder.                  *
 233  {$info}
 234  
 235  EOT;
 236  
 237              return $cap;
 238          }
 239          
 240          protected static function endCap()
 241          {
 242              $cap = <<<EOT
 243   *****************************************************************************/
 244  /* \$Id\$ */
 245  
 246  
 247  EOT;
 248              return $cap;
 249          }
 250          
 251  		protected static function getHeel()
 252          {
 253              return '?>';
 254          }
 255      }
 256  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]