public function testMethodAccessors() { $classGenerator = new ClassGenerator(); $classGenerator->addMethods(array( 'methodOne', new MethodGenerator('methodTwo') )); $methods = $classGenerator->getMethods(); $this->assertEquals(count($methods), 2); $this->isInstanceOf(current($methods), '\Zend\Code\Generator\PhpMethod'); $method = $classGenerator->getMethod('methodOne'); $this->isInstanceOf($method, '\Zend\Code\Generator\PhpMethod'); $this->assertEquals($method->getName(), 'methodOne'); // add a new property $classGenerator->addMethod('methodThree'); $this->assertEquals(count($classGenerator->getMethods()), 3); }
/** * Generate Api class from metadata * * @param stdClass $metadata* * @return Generator\ClassGenerator|null */ protected function generateApiFromMetadata($metadata) { $console = $this->getServiceLocator()->get('console'); $name = strtolower($metadata->Name); $ucName = ucfirst($name); $objectPrototype = sprintf('Mailjet\\Model\\%s', $ucName); // Create Api Class $class = new Generator\ClassGenerator($ucName, 'Mailjet\\Api'); $class->setExtendedClass('AbstractApi')->adduse('Mailjet')->adduse('Mailjet\\Model')->addUse('Mailjet\\Api\\ResultSet')->setDocBlock(new Generator\DocBlockGenerator($class->getName() . ' Api', $metadata->Description, array(new Generator\DocBlock\Tag(array('name' => 'see', 'description' => sprintf('http://mjdemo.poxx.net/~shubham/%s.html', $name))))))->addMethod('init', array(), Generator\MethodGenerator::FLAG_PUBLIC, sprintf('$this->getResultSetPrototype()->setObjectPrototype(new %s);', $objectPrototype) . PHP_EOL . sprintf('$this->setUrl(\'%s/%s/\');', AbstractApi::API_BASE_URI, $name) . PHP_EOL, new Generator\DocBlockGenerator("Post __construct initializations")); // Property : Name of Api $nameProperty = new Generator\PropertyGenerator('name'); $nameProperty->setFlags(Generator\PropertyGenerator::FLAG_PROTECTED); $nameProperty->setDefaultValue($name); $nameProperty->setDocBlock(new Generator\DocBlockGenerator('Api name')); $class->addPropertyFromGenerator($nameProperty); // Property : Filters $filters = array(); foreach ($metadata->Filters as $filter) { $filters[$filter->Name] = array('name' => $filter->Name, 'required' => $filter->IsRequired); } $filtersProperty = new Generator\PropertyGenerator('filters'); $filtersProperty->setFlags(Generator\PropertyGenerator::FLAG_PROTECTED); $filtersProperty->setDefaultValue($filters); $filtersProperty->setDocBlock(new Generator\DocBlockGenerator('Supported Filters')); $class->addPropertyFromGenerator($filtersProperty); // Property : Properties $properties = array(); $initMethod = $class->getMethod('init'); $initBodyMehtod = $initMethod->getBody(); $hydratorAppended = false; foreach ($metadata->Properties as $property) { $property = $this->getNormalizedFilterOrProperty($property); if (isset($property['hydrator']['strategy'])) { if (!$hydratorAppended) { $initBodyMehtod .= '$hydrator = $this->getResultSetPrototype()->getHydrator();' . PHP_EOL; $hydratorAppended = true; } $class->addUse($property['hydrator']['strategy']); $initBodyMehtod .= $property['hydrator']['init']; } $properties[$property['name']] = $property; while (list($k, $v) = each($properties[$property['name']])) { if (!in_array($k, array('name', 'dataType', 'required'))) { unset($properties[$property['name']][$k]); } } } $initMethod->setBody($initBodyMehtod); $propertiesProperty = new Generator\PropertyGenerator('properties'); $propertiesProperty->setFlags(Generator\PropertyGenerator::FLAG_PROTECTED); $propertiesProperty->setDefaultValue($properties); $propertiesProperty->setDocBlock(new Generator\DocBlockGenerator('Supported properties')); $class->addPropertyFromGenerator($propertiesProperty); $httpMethodes = explode(', ', $metadata->PublicOperations); array_walk($httpMethodes, function (&$v) { $v = strtoupper($v); }); foreach ($httpMethodes as $httpMethod) { $this->generateApiOperationFromMetadata($class, $httpMethod, $metadata); } // Create and Write Api Class File $file = new Generator\FileGenerator(); $file->setClass($class); $file->setDocBlock(new Generator\DocBlockGenerator('MailJet Api', self::LICENSE)); $file->setFilename(dirname(__DIR__) . '/Api/' . $class->getName() . '.php'); if (file_put_contents($file->getFilename(), $file->generate())) { $console->writeLine(sprintf("The Api %s has been created.", $class->getName()), Color::GREEN); return $class; } else { $console->writeLine(sprintf("There was an error during %s Api creation.", $class->getName()), Color::RED); } }