示例#1
0
 /**
  * @return PropertyGenerator
  */
 protected function getActionTableProperty(Table $table)
 {
     $property = new PropertyGenerator('action', [], PropertyGenerator::FLAG_PROTECTED);
     $property->setDocBlock(new DocBlockGenerator('Syntax analyse action table'));
     $property->setDefaultValue(new PropertyValueGenerator($table->getTable()));
     return $property;
 }
示例#2
0
 public function generate()
 {
     $classGenerator = $this->getClassGenerator();
     $description = $this->getDescription();
     $classGenerator->setName($description->getName());
     $basePathProperty = new CodeGenerator\PropertyGenerator();
     $basePathProperty->setName('basePath');
     $basePathProperty->setDefaultValue($description->getBasePath());
     $classGenerator->addPropertyFromGenerator($basePathProperty);
     foreach ($description->getApis() as $api) {
         foreach ($api->getOperations() as $operation) {
             $function = $this->generateFunction($api, $operation);
             if ($function instanceof CodeGenerator\MethodGenerator) {
                 $classGenerator->addMethodFromGenerator($function);
             }
         }
     }
 }
示例#3
0
 /**
  * @param ClassGenerator $class
  * @param Tag $tagItem
  */
 protected function addPropertyFlags(ClassGenerator $class, Tag $tagItem)
 {
     $flags = [];
     foreach ($tagItem->getAttributes() as $tagAttribute) {
         if ($tagAttribute->isFlag()) {
             $flags[] = $tagAttribute->getName();
         }
     }
     sort($flags);
     $property = new PropertyGenerator('flags', [], PropertyGenerator::FLAG_PROTECTED);
     $property->setDefaultValue($flags);
     $docBlock = new DocBlockGenerator();
     $docBlock->setTag(new GenericTag('var', 'array'));
     $property->setDocBlock($docBlock);
     $class->addPropertyFromGenerator($property);
 }
 /**
  * @dataProvider dataSetTypeSetValueGenerate
  * @param string $type
  * @param mixed $value
  * @param string $code
  */
 public function testSetDefaultValue($type, $value, $code)
 {
     $property = new PropertyGenerator();
     $property->setDefaultValue($value, $type);
     $this->assertEquals($type, $property->getDefaultValue()->getType());
     $this->assertEquals($value, $property->getDefaultValue()->getValue());
 }
 /**
  * @param $className
  */
 protected function addMessageTemplates($className)
 {
     // generate property
     $property = new PropertyGenerator();
     $property->setName('messageTemplates');
     $property->setDefaultValue(['invalid' . $className => 'Value "%value%" is not valid']);
     $property->setVisibility(PropertyGenerator::FLAG_PROTECTED);
     // check for api docs
     if ($this->config['flagAddDocBlocks']) {
         $property->setDocBlock(new DocBlockGenerator('Validation failure message template definitions', '', [new GenericTag('var', 'array')]));
     }
     // add property
     $this->addPropertyFromGenerator($property);
 }
示例#6
0
 protected function handleProperty(Generator\ClassGenerator $class, PHPProperty $prop)
 {
     $generatedProp = new PropertyGenerator($prop->getName());
     $generatedProp->setVisibility(PropertyGenerator::VISIBILITY_PROTECTED);
     if (!$class->hasProperty($prop->getName())) {
         $class->addPropertyFromGenerator($generatedProp);
     } else {
         $generatedProp = $class->getProperty($prop->getName());
     }
     $docBlock = new DocBlockGenerator();
     $generatedProp->setDocBlock($docBlock);
     if ($prop->getDoc()) {
         $docBlock->setLongDescription($prop->getDoc());
     }
     $tag = new Generator\DocBlock\Tag();
     $tag->setName("@var {$this->getPropertyType($prop)}");
     $docBlock->setTag($tag);
     $type = $prop->getType();
     if ($type->type && $this->isTypeMapped($type->type->getName())) {
         if (!$class->hasProperty('_typeMap')) {
             $generatedProp = new PropertyGenerator('_typeMap');
             $generatedProp->setDefaultValue([]);
             $generatedProp->setVisibility(PropertyGenerator::VISIBILITY_PROTECTED);
             $class->addPropertyFromGenerator($generatedProp);
         }
         $property = $class->getProperty('_typeMap');
         $defaultValue = $property->getDefaultValue()->getValue();
         $defaultValue[$prop->getName()] = $type->type->getName();
         $property->setDefaultValue($defaultValue);
     }
 }
示例#7
0
 /**
  * Append Properties, Getter and Setter to Model class
  *
  * @param Generator\ClassGenerator $class
  * @param array                    $properties
  * @param array                    $hydratorStrategy
  */
 protected function addProperties(Generator\ClassGenerator &$class, array $properties)
 {
     foreach ($properties as $metadata) {
         $propertyInfos = $this->getNormalizedFilterOrProperty($metadata);
         $name = $propertyInfos['name'];
         $required = $propertyInfos['required'];
         $lazyLoading = $propertyInfos['lazyLoading'];
         $dataType = $propertyInfos['dataType'];
         $defaultValue = $propertyInfos['defaultValue'];
         $description = $propertyInfos['description'];
         $flags = Generator\PropertyGenerator::FLAG_PROTECTED;
         foreach ($propertyInfos['uses'] as $use) {
             $class->addUse($use);
         }
         $property = new Generator\PropertyGenerator($name);
         $property->setFlags($flags);
         if (!empty($defaultValue) or 'bool' === $dataType) {
             $property->setDefaultValue($defaultValue, $dataType);
         }
         $property->setDocBlock(new Generator\DocBlockGenerator($description));
         $class->addPropertyFromGenerator($property);
         // Add Setter method
         $setterParam = new Generator\ParameterGenerator($name, true === $lazyLoading ? null : $dataType);
         if (!$required) {
             $setterParam->setDefaultValue(null);
         }
         $setterBody = '';
         if (true === $lazyLoading) {
             if ($dataType == "ResultSet") {
                 $class->addUse('Mailjet\\Api\\ResultSet\\Exception');
                 $setterBody .= sprintf('if (! ($%s instanceof \\Closure || $%s instanceof %s)) {' . PHP_EOL . '   throw new Exception\\InvalidArgumentException("%s must be an instance of \'%s\' or \\Closure");' . PHP_EOL . '}' . PHP_EOL . PHP_EOL, $name, $name, $dataType, $name, $dataType);
             } else {
                 $setterBody .= sprintf('if (! ($%s instanceof \\Closure || $%s instanceof %s)) {' . PHP_EOL . '   throw new Exception\\InvalidArgumentException("$%s must be an instance of \'%s\' or \\Closure");' . PHP_EOL . '}' . PHP_EOL . PHP_EOL, $name, $name, $dataType, $name, $dataType);
             }
         }
         $setterBody .= sprintf('$this->%s = $%s;' . PHP_EOL . 'return $this;' . PHP_EOL, $name, $name);
         $class->addMethod('set' . ucfirst($name), array($setterParam), Generator\MethodGenerator::FLAG_PUBLIC, $setterBody, new Generator\DocBlockGenerator("Sets the " . $property->getDocBlock()->getShortDescription(), '', array(new ParamTag(array('name' => $name, 'datatype' => $dataType)), new ReturnTag(array('datatype' => $class->getName())))));
         // Add Getter method
         $getterBody = '';
         if (true === $lazyLoading) {
             $getterBody .= sprintf('if ($this->%s instanceof \\Closure) {' . PHP_EOL . '   $this->%s = call_user_func($this->%s);' . PHP_EOL . '}' . PHP_EOL . 'if (! $this->%s instanceof %s) {' . PHP_EOL . '    $this->%s = new %s();' . PHP_EOL . '}' . PHP_EOL . PHP_EOL, $name, $name, $name, $name, $dataType, $name, $dataType);
         }
         $getterBody .= sprintf('return $this->%s;', $name);
         $class->addMethod('get' . ucfirst($name), array(), Generator\MethodGenerator::FLAG_PUBLIC, $getterBody, new Generator\DocBlockGenerator('Gets the ' . $property->getDocBlock()->getShortDescription(), '', array(new ReturnTag(array('datatype' => $dataType)))));
         // If DataType Resulset => add/remove methods
         if ('ResultSet' === $dataType) {
             $class->addMethod('add' . ucfirst($name) . 'Item', array(new Generator\ParameterGenerator('item', $propertyInfos['model'])), Generator\MethodGenerator::FLAG_PUBLIC, sprintf('return $this->get%s()->add($item);' . PHP_EOL, ucfirst($name)), new Generator\DocBlockGenerator('Add new item to ' . ucfirst($name), '', array(new ReturnTag(array('datatype' => 'bool')))));
             $class->addMethod('remove' . ucfirst($name) . 'Item', array(new Generator\ParameterGenerator('item', $propertyInfos['model'])), Generator\MethodGenerator::FLAG_PUBLIC, sprintf('return $this->get%s()->remove($item);' . PHP_EOL, ucfirst($name)), new Generator\DocBlockGenerator('Remove $item from ' . ucfirst($name), '', array(new ReturnTag(array('datatype' => 'bool')))));
         }
     }
 }
示例#8
0
 /**
  * {@inheritdoc}
  */
 public function createEnum(Type $type)
 {
     $class = $this->createClassFromType($type);
     $parentType = $type->getParent()->getBase();
     // set the parent class
     if ($parentType->getSchema()->getTargetNamespace() !== SchemaReader::XSD_NS) {
         // this enum extends another, it will indirectly extend \SplEnum
         $class->setExtendedClass($parentType->getName());
     } elseif ($this->config->isSplEnums()) {
         // this enum has no parent, so make it extend \SplEnum
         $class->setExtendedClass('\\SplEnum');
     }
     // create the class constants
     $checks = $type->getRestriction()->getChecks();
     foreach ($checks['enumeration'] as $enum) {
         $property = new PropertyGenerator();
         $property->setName(filter_var($enum['value'], FILTER_CALLBACK, array('options' => array($this, 'sanitizeVariableName'))));
         $property->setName(filter_var($property->getName(), FILTER_CALLBACK, array('options' => array($this, 'sanitizeConstantName'))));
         $property->setConst(true);
         $property->setDefaultValue($enum['value']);
         if ($enum['doc']) {
             $docDescription = new Html2Text($enum['doc']);
             $property->setDocBlock(new DocBlockGenerator($docDescription->getText()));
         }
         $class->addPropertyFromGenerator($property);
     }
     $this->serializeClass($class);
     // register the class in the classmap
     $this->classmap[$class->getName()] = $class->getNamespaceName() . '\\' . $class->getName();
     return $class->getName();
 }
示例#9
0
 private function generateProxiesMapProp()
 {
     $map = new PropertyGenerator();
     $map->setName('proxiesMap');
     $map->setVisibility(PropertyGenerator::VISIBILITY_PROTECTED);
     $map->setDefaultValue($this->buildProxiesMap(), 'array');
     return $map;
 }