/** * fromReflection() - build a Code Generation PHP Object from a Class Reflection * * @param Zend_Reflection_Class $reflectionClass * @return dmZendCodeGeneratorPhpClass */ public static function fromReflection(Zend_Reflection_Class $reflectionClass) { $class = new self(); $class->setSourceContent($class->getSourceContent()); $class->setSourceDirty(false); if ($reflectionClass->getDocComment() != '') { $class->setDocblock(Zend_CodeGenerator_Php_Docblock::fromReflection($reflectionClass->getDocblock())); } $class->setAbstract($reflectionClass->isAbstract()); $class->setName($reflectionClass->getName()); if ($parentClass = $reflectionClass->getParentClass()) { $class->setExtendedClass($parentClass->getName()); $interfaces = array_diff($parentClass->getInterfaces(), $reflectionClass->getInterfaces()); } else { $interfaces = $reflectionClass->getInterfaces(); } $class->setImplementedInterfaces($interfaces); $properties = array(); foreach ($reflectionClass->getProperties() as $reflectionProperty) { if ($reflectionProperty->getDeclaringClass()->getName() == $class->getName()) { $properties[] = Zend_CodeGenerator_Php_Property::fromReflection($reflectionProperty); } } $class->setProperties($properties); $methods = array(); foreach ($reflectionClass->getMethods(-1, 'dmZendReflectionMethod') as $reflectionMethod) { if ($reflectionMethod->getDeclaringClass()->getName() == $class->getName()) { $methods[] = dmZendCodeGeneratorPhpMethod::fromReflection($reflectionMethod); } } $class->setMethods($methods); return $class; }
/** * setProperty() * * @param array|Zend_CodeGenerator_Php_Property $property * @return Zend_CodeGenerator_Php_Class */ public function setProperty($property) { if (is_array($property)) { $property = new Zend_CodeGenerator_Php_Property($property); $propertyName = $property->getName(); } elseif ($property instanceof Zend_CodeGenerator_Php_Property) { $propertyName = $property->getName(); } else { require_once 'Zend/CodeGenerator/Php/Exception.php'; throw new Zend_CodeGenerator_Php_Exception('setProperty() expects either an array of property options or an instance of Zend_CodeGenerator_Php_Property'); } if (isset($this->_properties[$propertyName])) { require_once 'Zend/CodeGenerator/Php/Exception.php'; throw new Zend_CodeGenerator_Php_Exception('A property by name ' . $propertyName . ' already exists in this class.'); } $this->_properties[$propertyName] = $property; return $this; }
/** * setConstant() * * @param array|Zend_CodeGenerator_Php_Property $const * @return Zend_CodeGenerator_Php_Class */ public function setConstant($const) { if (is_array($const)) { $const = new Zend_CodeGenerator_Php_Property($const); $constName = $const->getName(); } elseif ($const instanceof Zend_CodeGenerator_Php_Property) { $constName = $const->getName(); } else { require_once 'Zend/CodeGenerator/Php/Exception.php'; throw new Zend_CodeGenerator_Php_Exception('setConstant() expects either an array of property options or an instance of Zend_CodeGenerator_Php_Property'); } if (!$const->isConst()) { require_once 'Zend/CodeGenerator/Php/Exception.php'; throw new Zend_CodeGenerator_Php_Exception('setProperty() expects argument to define a constant'); } if (isset($this->_constants[$constName])) { require_once 'Zend/CodeGenerator/Php/Exception.php'; throw new Zend_CodeGenerator_Php_Exception('A constant by name ' . $constName . ' already exists in this class.'); } $this->_constants[$constName] = $const; return $this; }
/** * @group ZF-7361 */ public function testHasProperty() { $property = new Zend_CodeGenerator_Php_Property(); $property->setName('propertyOne'); $codeGenClass = new Zend_CodeGenerator_Php_Class(); $codeGenClass->setProperty($property); $this->assertTrue($codeGenClass->hasProperty('propertyOne')); }
/** * @group ZF-8849 */ public function testZF8849() { $property = new Zend_CodeGenerator_Php_Property(array('defaultValue' => array('value' => 1.337, 'type' => 'string'), 'name' => 'ZF8849', 'const' => true)); $this->assertEquals($property->generate(), " const ZF8849 = '1.337';"); }
/** * @return string */ protected function _provideColumnConstant(Zend_CodeGenerator_Php_Property $parAttribute) { $tableName = $this->getPersistenceInformation()->getTableName(); $tableNameUpper = strtoupper($tableName); $columnName = strtoupper($parAttribute->getName()); return 'DB_COL_' . $tableNameUpper . '_' . strtoupper($columnName); }
/** * @group ZF-11513 */ public function testAllowsClassConstantToHaveSameNameAsClassProperty() { $const = new Zend_CodeGenerator_Php_Property(); $const->setName('name')->setDefaultValue('constant')->setConst(true); $property = new Zend_CodeGenerator_Php_Property(); $property->setName('name')->setDefaultValue('property'); $codeGenClass = new Zend_CodeGenerator_Php_Class(); $codeGenClass->setName('My_Class')->setProperties(array($const, $property)); $expected = <<<CODE class My_Class { const name = 'constant'; public \$name = 'property'; } CODE; $this->assertEquals($expected, $codeGenClass->generate()); }
/** * Returns the type from the Attributes php tag type. * * @return string */ public static function getType(Zend_CodeGenerator_Php_Property $parAttribute) { $docblock = $parAttribute->getDocblock(); if ($docblock) { $tags = $docblock->getTags(); foreach ($tags as $tag) { /* @var $tag Zend_CodeGenerator_Php_Docblock_Tag */ if ($tag->getName() === 'var') { $type = $tag->getDescription(); return $type; } } } return false; }
public function testOtherTypesThrowExceptionOnGenerate() { $codeGenProperty = new Zend_CodeGenerator_Php_Property(array('name' => 'someVal', 'defaultValue' => new stdClass())); $this->setExpectedException("Zend_CodeGenerator_Php_Exception"); $codeGenProperty->generate(); }
/** * setProperty() * * @param array|Zend_CodeGenerator_Php_Property $property * @return Zend_CodeGenerator_Php_Class */ public function setProperty($property) { if (is_array($property)) { $property = new Zend_CodeGenerator_Php_Property($property); $propertyName = $property->getName(); } elseif ($property instanceof Zend_CodeGenerator_Php_Property) { $propertyName = $property->getName(); } else { // require_once 'Zend/CodeGenerator/Php/Exception.php'; throw new Zend_CodeGenerator_Php_Exception('setProperty() expects either an array of property options or an instance of Zend_CodeGenerator_Php_Property'); } if (isset($this->_properties[$propertyName])) { // require_once 'Zend/CodeGenerator/Php/Exception.php'; throw new Zend_CodeGenerator_Php_Exception('A property by name ' . $propertyName . ' already exists in this class.'); } $append = true; foreach ($this->_properties as &$p) { if ($p->getName() === $property->getName()) { $p->setDefaultValue($property->getDefaultValue()); $append = false; } } if ($append === true) { $this->_properties[$propertyName] = $property; } return $this; }