private function getProperties() { $classProperties = array(); $classProperties[] = PropertyGenerator::fromArray(array('name' => 'table', 'defaultvalue' => $this->data['_tbname'], 'flags' => PropertyGenerator::FLAG_PROTECTED, 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Name of database table ', 'longDescription' => null, 'tags' => array(new GenericTag('var', 'string' . ' ' . 'Name of DB Table')))))); $classProperties[] = PropertyGenerator::fromArray(array('name' => 'id', 'defaultvalue' => 'array' !== $this->data['_primaryKey']['phptype'] ? $this->data['_primaryKey']['field'] : eval('return ' . $this->data['_primaryKey']['field'] . ';'), 'flags' => PropertyGenerator::FLAG_PROTECTED, 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Primary key name', 'longDescription' => null, 'tags' => array(new GenericTag('var', 'string|array' . ' ' . 'Primary key name')))))); $classProperties[] = PropertyGenerator::fromArray(array('name' => 'sequence', 'defaultvalue' => 'array' !== $this->data['_primaryKey']['phptype'], 'flags' => PropertyGenerator::FLAG_PROTECTED, 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Is primary Key auto increment', 'longDescription' => null, 'tags' => array(new GenericTag('var', 'boolean' . ' ' . 'Is primary Key auto increment')))))); return $classProperties; }
/** * @param \Protobuf\Compiler\Entity $entity * @param \google\protobuf\FieldDescriptorProto $field * * @return string */ protected function generateExtensionField(Entity $entity, FieldDescriptorProto $field) { $name = $field->getName(); $number = $field->getNumber(); $docBlock = $this->getDocBlockType($field); $type = $this->getFieldTypeName($field); $label = $this->getFieldLabelName($field); $property = PropertyGenerator::fromArray(['static' => true, 'name' => $name, 'visibility' => PropertyGenerator::VISIBILITY_PROTECTED, 'docblock' => ['shortDescription' => "Extension field : {$name} {$label} {$type} = {$number}", 'tags' => [['name' => 'var', 'description' => '\\Protobuf\\Extension']]]]); $property->getDocblock()->setWordWrap(false); return $property; }
/** * @param \Protobuf\Compiler\Entity $entity * * @return string[] */ public function generateConstants(Entity $entity) { $constants = []; $descriptor = $entity->getDescriptor(); $values = $descriptor->getValueList() ?: []; foreach ($values as $value) { $name = $value->getName(); $number = $value->getNumber(); $constant = PropertyGenerator::fromArray(['const' => true, 'defaultvalue' => $number, 'name' => $name . '_VALUE', 'docblock' => ['shortDescription' => "{$name} = {$number}"]]); $constants[] = $constant; } return $constants; }
/** * @param ContextInterface|PropertyContext $context * * @throws AssemblerException */ public function assemble(ContextInterface $context) { $class = $context->getClass(); $property = $context->getProperty(); try { // It's not possible to overwrite a property in zend-code yet! if ($class->hasProperty($property->getName())) { return; } $class->addPropertyFromGenerator(PropertyGenerator::fromArray(['name' => $property->getName(), 'visibility' => PropertyGenerator::VISIBILITY_PROTECTED, 'docblock' => DocBlockGenerator::fromArray(['tags' => [['name' => 'var', 'description' => $property->getType()]]])])); } catch (\Exception $e) { throw AssemblerException::fromException($e); } }
private function getProperties() { $classProperties = array(); $classProperties[] = PropertyGenerator::fromArray(array('name' => 'primary_key', 'defaultvalue' => 'array' !== $this->data['_primaryKey']['phptype'] ? $this->data['_primaryKey']['field'] : eval('return ' . $this->data['_primaryKey']['field'] . ';'), 'flags' => PropertyGenerator::FLAG_PROTECTED, 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Primary key name', 'longDescription' => '', 'tags' => array(new GenericTag('var', $this->data['_primaryKey']['phptype'] . ' primary_key')))))); foreach ($this->data['_columns'] as $column) { $comment = !empty($column['comment']) ? $column['comment'] : null; $classProperties[] = PropertyGenerator::fromArray(array('name' => $column['capital'], 'flags' => PropertyGenerator::FLAG_PROTECTED, 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => $column['capital'], 'longDescription' => $comment, 'tags' => array(new GenericTag('var', $column['phptype'] . ' ' . $column['capital'])))))); } foreach ($this->data['foreignKeysInfo'] as $key) { if (!is_array($key['column_name'])) { $name = $this->data['relationNameParent'][$key['key_name']] . $this->_getCapital($key['column_name']); $classProperties[] = PropertyGenerator::fromArray(array('name' => $name, 'flags' => PropertyGenerator::FLAG_PROTECTED, 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Parent relation', 'longDescription' => null, 'tags' => array(new GenericTag('var', $this->data['className'][$key['key_name']]['foreign_tbl_name'] . ' ' . $name)))))); } } foreach ($this->data['dependentTables'] as $key) { $name = $this->data['relationNameDependent'][$key['key_name']]; $longDescr = sprintf('Type: %s relationship', $key['type'] == 'one' ? 'One-to-One' : 'One-to-Many'); $classProperties[] = PropertyGenerator::fromArray(array('name' => $name, 'flags' => PropertyGenerator::FLAG_PROTECTED, 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Dependent relation ', 'longDescription' => $longDescr, 'tags' => array(new GenericTag('var', $this->data['classNameDependent'][$key['key_name']]['foreign_tbl_name'] . ' ' . $name)))))); } return $classProperties; }
public function getClassArrayRepresentation() { $this->data = $this->getData(); return array('name' => 'Manager', 'namespacename' => $this->data['_namespace'] . '\\Table', 'extendedclass' => $this->tableGatewayClass, 'flags' => ClassGenerator::FLAG_ABSTRACT, 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Application Model DbTables', 'longDescription' => null, 'tags' => array(array('name' => 'package', 'description' => $this->data['_namespace']), array('name' => 'author', 'description' => $this->data['_author']), array('name' => 'copyright', 'description' => $this->data['_copyright']), array('name' => 'license', 'description' => $this->data['_license'])))), 'properties' => array(array('entity', null, PropertyGenerator::FLAG_PROTECTED), array('container', null, PropertyGenerator::FLAG_PROTECTED), PropertyGenerator::fromArray(array('name' => 'wasInTransaction', 'defaultvalue' => false, 'flags' => PropertyGenerator::FLAG_PROTECTED, 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'True if we were already in a transaction when try to start a new one', 'longDescription' => '', 'tags' => array(new GenericTag('var', 'bool'))))))), 'methods' => array(array('name' => '__construct', 'parameters' => array(ParameterGenerator::fromArray(array('name' => 'adapter')), ParameterGenerator::fromArray(array('name' => 'entity', 'type' => 'Entity'))), 'flags' => MethodGenerator::FLAG_PUBLIC, 'body' => '$this->adapter = $adapter;' . "\n" . '$this->entity = $entity;' . "\n" . '$this->featureSet = new Feature\\FeatureSet();' . "\n" . '$this->initialize();', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Constructor', 'longDescription' => null, 'tags' => array(new ParamTag('adapter', array('Adapter')), new ParamTag('entity', array('Entity')))))), array('name' => 'setContainer', 'parameters' => array(ParameterGenerator::fromArray(array('name' => 'c', 'type' => 'Container'))), 'flags' => MethodGenerator::FLAG_PUBLIC, 'body' => '$this->container = $c;' . "\n" . 'return $this;', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Inject container', 'longDescription' => null, 'tags' => array(new ParamTag('c', array('Container')), new ReturnTag(array('datatype' => 'self')))))), array('name' => 'getContainer', 'parameters' => array(), 'flags' => MethodGenerator::FLAG_PUBLIC, 'body' => 'return $this->container;', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => '', 'longDescription' => null, 'tags' => array(new ReturnTag(array('datatype' => 'Container')))))), array('name' => 'all', 'parameters' => array(), 'flags' => MethodGenerator::FLAG_PUBLIC, 'body' => '$select = $this->select();' . '$result = array();' . PHP_EOL . 'foreach ($select as $v) {' . PHP_EOL . ' $result[] = $v->getArrayCopy();' . PHP_EOL . '}' . PHP_EOL . 'return $result;', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => '', 'longDescription' => null, 'tags' => array(new ReturnTag(array('datatype' => 'self')))))), array('name' => 'getPrimaryKeyName', 'parameters' => array(), 'flags' => MethodGenerator::FLAG_PUBLIC, 'body' => 'return $this->id;', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => '', 'longDescription' => null, 'tags' => array(new ReturnTag(array('datatype' => 'array|string')))))), array('name' => 'getTableName', 'parameters' => array(), 'flags' => MethodGenerator::FLAG_PUBLIC, 'body' => 'return $this->table;', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => '', 'longDescription' => null, 'tags' => array(new ReturnTag(array('datatype' => 'array|string')))))), array('name' => 'findBy', 'parameters' => array(ParameterGenerator::fromArray(array('name' => 'criteria', 'defaultvalue' => array(), 'type' => 'array')), ParameterGenerator::fromArray(array('name' => 'order', 'defaultvalue' => null)), ParameterGenerator::fromArray(array('name' => 'limit', 'defaultvalue' => null)), ParameterGenerator::fromArray(array('name' => 'offset', 'defaultvalue' => null)), ParameterGenerator::fromArray(array('name' => 'toEntity', 'defaultvalue' => false))), 'flags' => MethodGenerator::FLAG_PUBLIC, 'body' => '$r = $this->sql->select()->where($criteria);' . PHP_EOL . 'if ($order) {' . PHP_EOL . ' $r->order($order);' . PHP_EOL . '}' . PHP_EOL . 'if ($limit) {' . PHP_EOL . ' $r->limit($limit);' . PHP_EOL . '}' . PHP_EOL . 'if ($offset) {' . PHP_EOL . ' $r->offset($offset);' . PHP_EOL . '}' . PHP_EOL . '$result = $this->selectWith($r)->toArray();' . PHP_EOL . 'if ($toEntity) {' . PHP_EOL . ' foreach($result as &$v){' . PHP_EOL . ' $entity = clone $this->entity;' . PHP_EOL . ' $v = $entity->exchangeArray($v);' . PHP_EOL . ' }' . PHP_EOL . '}' . PHP_EOL . 'return $result;' . PHP_EOL, 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Find by criteria', 'longDescription' => null, 'tags' => array(new ParamTag('criteria', array('array'), 'Search criteria'), new ParamTag('order', array('string'), 'sorting option'), new ParamTag('limit', array('int'), 'limit option'), new ParamTag('offset', array('int'), 'offset option'), new ParamTag('toEntity', array('boolean'), 'return entity result'), new ReturnTag(array('array'), ''))))), array('name' => 'countBy', 'parameters' => array(ParameterGenerator::fromArray(array('name' => 'criteria', 'defaultvalue' => array(), 'type' => 'array'))), 'flags' => MethodGenerator::FLAG_PUBLIC, 'body' => '$r = $this->sql->select()->columns(array("count" => new Expression("count(*)")))->where($criteria);' . PHP_EOL . 'return (int)current($this->selectWith($r)->toArray())["count"];' . PHP_EOL, 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Count by criteria', 'longDescription' => null, 'tags' => array(new ParamTag('criteria', array('array'), 'Criteria'), new ReturnTag(array('int'), ''))))), array('name' => 'deleteEntity', 'parameters' => array(ParameterGenerator::fromArray(array('name' => 'entity', 'type' => 'Entity')), 'useTransaction = true'), 'flags' => array(MethodGenerator::FLAG_PUBLIC, MethodGenerator::FLAG_ABSTRACT), 'body' => null, 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Converts database column name to php setter/getter function name', 'longDescription' => null, 'tags' => array(new ParamTag('entity', array('Entity')), new ParamTag('useTransaction', array('boolean')), new ReturnTag(array('datatype' => 'int')))))), array('name' => 'beginTransaction', 'parameters' => array(), 'flags' => MethodGenerator::FLAG_PROTECTED, 'body' => <<<'BODY' if ($this->adapter->getDriver()->getConnection()->inTransaction()) { $this->wasInTransaction = true; return; } $this->adapter->getDriver()->getConnection()->beginTransaction(); BODY , 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Begin a transaction', 'longDescription' => null, 'tags' => array()))), array('name' => 'rollback', 'parameters' => array(), 'flags' => MethodGenerator::FLAG_PROTECTED, 'body' => <<<'BODY' if ($this->wasInTransaction) { throw new \Exception('Inside transaction rollback call'); } $this->adapter->getDriver()->getConnection()->rollback(); BODY , 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Rollback a transaction', 'longDescription' => null, 'tags' => array()))), array('name' => 'commit', 'parameters' => array(), 'flags' => MethodGenerator::FLAG_PROTECTED, 'body' => <<<'BODY' if (!$this->wasInTransaction) { $this->adapter->getDriver()->getConnection()->commit(); } BODY , 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => ' Commit a transaction', 'longDescription' => null, 'tags' => array()))))); }
/** * * * @configkey name string [required] Class Name * @configkey filegenerator FileGenerator File generator that holds this class * @configkey namespacename string The namespace for this class * @configkey docblock string The docblock information * @configkey flags int Flags, one of ClassGenerator::FLAG_ABSTRACT ClassGenerator::FLAG_FINAL * @configkey extendedclass string Class which this class is extending * @configkey implementedinterfaces * @configkey properties * @configkey methods * * * @static * @throws Exception\InvalidArgumentException * @param array $array * @return ClassGenerator */ public static function fromArray(array $array) { if (!isset($array['name'])) { throw new Exception\InvalidArgumentException('Class generator requires that a name is provided for this object'); } $cg = new static($array['name']); foreach ($array as $name => $value) { // normalize key switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) { case 'containingfile': $cg->setContainingFileGenerator($value); break; case 'namespacename': $cg->setNamespaceName($value); break; case 'docblock': $cg->setDocblock(!$value instanceof DocblockGenerator ?: DocblockGenerator::fromArray($value)); break; case 'flags': $cg->setFlags($value); break; case 'extendedclass': $cg->setExtendedClass($value); break; case 'implementedinterfaces': $cg->setImplementedInterfaces($value); break; case 'properties': foreach ($value as $pValue) { $cg->setProperty(!$pValue instanceof PropertyGenerator ?: PropertyGenerator::fromArray($pValue)); } break; case 'methods': foreach ($value as $mValue) { $cg->setMethod(!$mValue instanceof MethodGenerator ?: MethodGenerator::fromArray($mValue)); } break; } } return $cg; }
public function testCreateFromArray() { $propertyGenerator = PropertyGenerator::fromArray(array('name' => 'SampleProperty', 'const' => true, 'defaultvalue' => 'foo', 'docblock' => array('shortdescription' => 'foo'), 'abstract' => true, 'final' => true, 'static' => true, 'visibility' => PropertyGenerator::VISIBILITY_PROTECTED)); $this->assertEquals('SampleProperty', $propertyGenerator->getName()); $this->assertTrue($propertyGenerator->isConst()); $this->assertInstanceOf('Zend\\Code\\Generator\\ValueGenerator', $propertyGenerator->getDefaultValue()); $this->assertInstanceOf('Zend\\Code\\Generator\\DocBlockGenerator', $propertyGenerator->getDocBlock()); $this->assertTrue($propertyGenerator->isAbstract()); $this->assertTrue($propertyGenerator->isFinal()); $this->assertTrue($propertyGenerator->isStatic()); $this->assertEquals(PropertyGenerator::VISIBILITY_PROTECTED, $propertyGenerator->getVisibility()); }