Nette\PhpGenerator\ClassType::__toString PHP Method

__toString() public method

public __toString ( ) : string
return string PHP code
    public function __toString()
    {
        $consts = [];
        foreach ($this->consts as $name => $value) {
            $consts[] = "const {$name} = " . Helpers::dump($value) . ";\n";
        }
        $properties = [];
        foreach ($this->properties as $property) {
            $doc = str_replace("\n", "\n * ", $property->getComment());
            $properties[] = ($doc ? strpos($doc, "\n") === FALSE ? "/** {$doc} */\n" : "/**\n * {$doc}\n */\n" : '') . $property->getVisibility() . ($property->isStatic() ? ' static' : '') . ' $' . $property->getName() . ($property->value === NULL ? '' : ' = ' . Helpers::dump($property->value)) . ";\n";
        }
        $mapper = function (array $arr) {
            return $this->namespace ? array_map([$this->namespace, 'unresolveName'], $arr) : $arr;
        };
        return Strings::normalize(($this->comment ? str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n" : '') . ($this->abstract ? 'abstract ' : '') . ($this->final ? 'final ' : '') . $this->type . ' ' . $this->name . ' ' . ($this->extends ? 'extends ' . implode(', ', $mapper((array) $this->extends)) . ' ' : '') . ($this->implements ? 'implements ' . implode(', ', $mapper($this->implements)) . ' ' : '') . "\n{\n" . Strings::indent(($this->traits ? 'use ' . implode(";\nuse ", $mapper($this->traits)) . ";\n\n" : '') . ($this->consts ? implode('', $consts) . "\n" : '') . ($this->properties ? implode("\n", $properties) . "\n" : '') . ($this->methods ? "\n" . implode("\n\n\n", $this->methods) . "\n\n" : ''), 1) . '}') . "\n";
    }

Usage Example

 /**
  * @param ClassType $classType
  * @param string $dir
  */
 private function createClass(ClassType $classType, $dir = "")
 {
     if ($dir) {
         $dir = "/{$dir}";
     }
     $filePath = $this->target . "{$dir}/" . $classType->getName() . ".php";
     if (!file_exists($filePath)) {
         file_put_contents($filePath, "<?php" . PHP_EOL . PHP_EOL . $classType->__toString());
     }
 }