MetaClass.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2008 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     class MetaClass
00016     {
00017         private $name       = null;
00018         private $tableName  = null;
00019         private $type       = null;
00020         
00021         private $parent     = null;
00022         
00023         private $properties = array();
00024         private $interfaces = array();
00025         private $references = array();
00026         
00027         private $pattern    = null;
00028         private $identifier = null;
00029         
00030         private $source     = null;
00031         
00032         private $strategy   = null;
00033         
00034         private $build      = true;
00035         
00036         public function __construct($name)
00037         {
00038             $this->name = $name;
00039             
00040             $dumb = strtolower(
00041                 preg_replace(':([A-Z]):', '_\1', $name)
00042             );
00043             
00044             if ($dumb[0] == '_')
00045                 $this->tableName = substr($dumb, 1);
00046             else
00047                 $this->tableName = $dumb;
00048         }
00049         
00050         public function getName()
00051         {
00052             return $this->name;
00053         }
00054         
00055         public function getTableName()
00056         {
00057             return $this->tableName;
00058         }
00059         
00063         public function setTableName($name)
00064         {
00065             $this->tableName = $name;
00066             
00067             return $this;
00068         }
00069         
00073         public function getType()
00074         {
00075             return $this->type;
00076         }
00077         
00078         public function getTypeId()
00079         {
00080             return
00081                 $this->type
00082                     ? $this->type->getId()
00083                     : null;
00084         }
00085         
00089         public function setType(MetaClassType $type)
00090         {
00091             $this->type = $type;
00092             
00093             return $this;
00094         }
00095         
00099         public function getParent()
00100         {
00101             return $this->parent;
00102         }
00103         
00107         public function getFinalParent()
00108         {
00109             if ($this->parent)
00110                 return $this->parent->getFinalParent();
00111             
00112             return $this;
00113         }
00114         
00118         public function setParent(MetaClass $parent)
00119         {
00120             $this->parent = $parent;
00121             
00122             return $this;
00123         }
00124         
00125         public function hasBuildableParent()
00126         {
00127             return (
00128                 $this->parent
00129                 && (
00130                     !$this->getParent()->getPattern()
00131                         instanceof InternalClassPattern
00132                 )
00133             );
00134         }
00135         
00136         public function getProperties()
00137         {
00138             return $this->properties;
00139         }
00140         
00142         public function getAllProperties()
00143         {
00144             if ($this->parent)
00145                 return array_merge(
00146                     $this->parent->getAllProperties(),
00147                     $this->properties
00148                 );
00149             
00150             return $this->getProperties();
00151         }
00152         
00154         public function getWithInternalProperties()
00155         {
00156             if ($this->parent) {
00157                 $out = $this->properties;
00158                 
00159                 $class = $this;
00160                 
00161                 while ($parent = $class->getParent()) {
00162                     if ($parent->getPattern() instanceof InternalClassPattern) {
00163                         $out = array_merge($parent->getProperties(), $out);
00164                     }
00165                     
00166                     $class = $parent;
00167                 }
00168                 
00169                 return $out;
00170             }
00171             
00172             return $this->getProperties();
00173         }
00174         
00176         public function getAllParentsProperties()
00177         {
00178             $out = array();
00179             
00180             $class = $this;
00181             
00182             while ($parent = $class->getParent()) {
00183                 $out = array_merge($out, $parent->getProperties());
00184                 $class = $parent;
00185             }
00186             
00187             return $out;
00188         }
00189         
00193         public function addProperty(MetaClassProperty $property)
00194         {
00195             $name = $property->getName();
00196             
00197             if (!isset($this->properties[$name]))
00198                 $this->properties[$name] = $property;
00199             else
00200                 throw new WrongArgumentException(
00201                     "property '{$name}' already exist"
00202                 );
00203             
00204             if ($property->isIdentifier())
00205                 $this->identifier = $property;
00206             
00207             return $this;
00208         }
00209         
00214         public function getPropertyByName($name)
00215         {
00216             if (isset($this->properties[$name]))
00217                 return $this->properties[$name];
00218             
00219             throw new MissingElementException("unknown property '{$name}'");
00220         }
00221         
00222         public function hasProperty($name)
00223         {
00224             return isset($this->properties[$name]);
00225         }
00226         
00230         public function dropProperty($name)
00231         {
00232             if (isset($this->properties[$name])) {
00233                 
00234                 if ($this->properties[$name]->isIdentifier())
00235                     unset($this->identifier);
00236                 
00237                 unset($this->properties[$name]);
00238             
00239             } else
00240                 throw new MissingElementException(
00241                     "property '{$name}' does not exist"
00242                 );
00243             
00244             return $this;
00245         }
00246         
00247         public function getInterfaces()
00248         {
00249             return $this->interfaces;
00250         }
00251         
00255         public function addInterface($name)
00256         {
00257             $this->interfaces[] = $name;
00258             
00259             return $this;
00260         }
00261         
00265         public function getPattern()
00266         {
00267             return $this->pattern;
00268         }
00269         
00273         public function setPattern(GenerationPattern $pattern)
00274         {
00275             $this->pattern = $pattern;
00276             
00277             return $this;
00278         }
00279         
00283         public function getIdentifier()
00284         {
00285             // return parent's identifier, if we're child
00286             if (!$this->identifier && $this->parent)
00287                 return $this->parent->getIdentifier();
00288             
00289             return $this->identifier;
00290         }
00291         
00295         public function setSourceLink($link)
00296         {
00297             $this->source = $link;
00298             
00299             return $this;
00300         }
00301         
00302         public function getSourceLink()
00303         {
00304             return $this->source;
00305         }
00306         
00310         public function setReferencingClass($className)
00311         {
00312             $this->references[$className] = true;
00313             
00314             return $this;
00315         }
00316         
00317         public function getReferencingClasses()
00318         {
00319             return array_keys($this->references);
00320         }
00321         
00325         public function setFetchStrategy(FetchStrategy $strategy)
00326         {
00327             $this->strategy = $strategy;
00328             
00329             return $this;
00330         }
00331         
00335         public function getFetchStrategy()
00336         {
00337             return $this->strategy;
00338         }
00339         
00340         public function getFetchStrategyId()
00341         {
00342             if ($this->strategy)
00343                 return $this->strategy->getId();
00344             
00345             return null;
00346         }
00347         
00348         public function hasChilds()
00349         {
00350             foreach (MetaConfiguration::me()->getClassList() as $class) {
00351                 if (
00352                     $class->getParent()
00353                     && $class->getParent()->getName() == $this->getName()
00354                 )
00355                     return true;
00356             }
00357             
00358             return false;
00359         }
00360         
00361         public function dump()
00362         {
00363             if ($this->doBuild())
00364                 return $this->pattern->build($this);
00365             
00366             return $this->pattern;
00367         }
00368         
00369         public function doBuild()
00370         {
00371             return $this->build;
00372         }
00373         
00377         public function setBuild($do)
00378         {
00379             $this->build = $do;
00380             
00381             return $this;
00382         }
00383         
00387         public function isRedefinedProperty($name)
00388         {
00389             $parent = $this;
00390             
00391             while ($parent = $parent->getParent()) {
00392                 if ($parent->hasProperty($name))
00393                     return $parent->getPropertyByName($name);
00394             }
00395             
00396             return false;
00397         }
00398     }
00399 ?>