Пример #1
0
 /**
  * @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);
 }