/** * */ function addAPIParameterAccessMethod() { foreach ($this->apiParameters as $apiParameter => $type) { $translatedParam = ucfirst($this->translateParameter($apiParameter)); $methodGenerator = new MethodGenerator('get' . $translatedParam); $body = 'return $this->' . $apiParameter . ';' . PHP_EOL; $methodGenerator->setBody($body); $methodGenerator->setDocBlock("@return {$type}"); $this->classGenerator->addMethodFromGenerator($methodGenerator); $methodGenerator = new MethodGenerator('set' . $translatedParam); $body = '$this->' . $apiParameter . ' = $value;' . PHP_EOL; $parameterParameter = new ParameterGenerator('value'); $methodGenerator->setParameter($parameterParameter); $methodGenerator->setBody($body); $this->classGenerator->addMethodFromGenerator($methodGenerator); } }
/** * Helper method for generating trivial methods. * @param $methodName * @param $body * @param $docBlock * @param $parameterInfoArray * @return MethodGenerator */ private function createMethodGenerator($methodName, $body, DocBlockGenerator $docBlock, $parameterInfoArray, $returnType = null) { $parameters = []; foreach ($parameterInfoArray as $parameterInfo) { $parameters[] = new ParameterGenerator($parameterInfo[0], $parameterInfo[1]); } $methodGenerator = new MethodGenerator($methodName); $methodGenerator->setParameters($parameters); if ($returnType != null) { if (is_array($returnType)) { $returnType = implode('|', $returnType); } $tags[] = new GenericTag('return', $returnType); $docBlock->setTags($tags); } $methodGenerator->setDocBlock($docBlock); $methodGenerator->setBody($body); return $methodGenerator; }
/** * @param AnalyzedClass $class */ function saveClass(AnalyzedClass $class, $namespace, $outputDirectory) { static $count = 0; $count++; $classGen = new ClassGenerator($class->className); $classGen->setExtendedClass('GithubService\\Model\\DataMapper'); $dataMapEntries = []; foreach ($class->properties as $property) { /** @var $property \Property */ $propertyGen = new PropertyGenerator(); $propertyGen->setName(lcfirst($property->name)); if ($property->multiple == true) { $propertyGen->setDefaultValue([]); } if ($property instanceof AnalyzedClass) { $propertyGen->setStandardDocBlock($namespace . '\\' . $property->realClassName); } $classGen->addPropertyFromGenerator($propertyGen); $dataMapEntries[] = $property->getDataMapEntry(); } $methodGenerator = new MethodGenerator(); $methodGenerator->setVisibility(MethodGenerator::VISIBILITY_PROTECTED); $methodGenerator->setName('getDataMap'); $bodyTest = "\$dataMap = [\n"; foreach ($dataMapEntries as $dataMapEntry) { $line = " ["; $separator = ''; foreach ($dataMapEntry as $key => $value) { $line .= $separator; if (is_numeric($key) == true) { $line .= var_export($value, true); } else { $line .= var_export($key, true) . " => " . var_export($value, true); } $separator = ', '; } $line .= "],"; $bodyTest .= $line . "\n"; } $bodyTest .= "];\n\n"; $bodyTest .= "return \$dataMap;\n"; $methodGenerator->setBody($bodyTest); $classGen->addMethodFromGenerator($methodGenerator); $classString = $classGen->generate(); $output = "<?php \n\nnamespace {$namespace};\n\n"; $output .= $classString; $filename = sprintf($outputDirectory . "/%s.php", $class->className); file_put_contents($filename, $output); }