public function testSomething() { $class = new PhpClass('Spanner', 'Fish', false); $class->addUses('Goat', ['Monkey', 'Bananna']); $class->addExtends('Wibble'); $class->addImplements('FunTime'); $class->classComponents[] = new FunctionDeclaration('hello', 'public function hello(){ return "hello"; }'); $class->classComponents[] = new FunctionDeclaration('goodbye', 'public function goodbye(){ return "goodbye"; }'); $class->classComponents[] = new FunctionDeclaration('__construct', 'public function __construct(){}'); $class->classComponents[] = new FunctionDeclaration('multiLineDeclaration', <<<'PHP' /** * Some multiline comment */ public function multiLineDeclaration () { return array( 'monkey' => 'goat', ); } PHP ); $class->classComponents[] = new VariableDeclaration('size', 'public $size = "big";'); $class->classComponents[] = new VariableDeclaration('age', 'public $age;'); $class->classComponents[] = new ClassConstant('t-rex', 'const T_REX = 100;'); $php = $class->render(); $php = new Php($class->render()); $isValid = $php->isValid($error); // echo $class->render()->addLineNumbers(); }
public function __construct(\SplFileInfo $file) { if (!$file->isFile()) { throw new \LogicException("Please pass a file not a directory."); } $this->originalPhp = file_get_contents($this->file->getPathname()); $php = new Php($this->originalPhp); if (!$php->isValid($errors, true)) { throw new BadPhpException($this->originalPhp, $errors); } $this->file = $file; $this->analyse(); }
/** * Generate a get Options Resolver component for the enity */ private function getOptionsResolverFunctionDeclaration() { // get required properties $required = []; $optional = []; $allowedValues = []; // optional foreach ($this->entity->dataTypes as $name => $dataType) { // is required if (!$dataType->getDefault() and !$dataType->isNullable()) { $required[] = $name; } else { $optional[] = $name; } // set allowed values (currently only works for enums) if ($dataType->isEnum($enumName)) { // At the moment this is bugged in Symfony.Don't think it is of vital importance. Ignoring for now. # $allowedValues[$name] = $enumName; } } // allowed values declaration $allowedValuesPhp = ''; if ($allowedValues) { $allowedValuesPhp = "\n\$resolver->setAllowedValues(\n array(\n"; // at the moment this enum supported only foreach ($allowedValues as $column => $enumName) { $allowedValuesPhp .= sprintf(' %s => Enum::getValues(%s),%s', Php::varExport($column), Php::varExport($enumName), "\n"); } $allowedValuesPhp .= " )\n);"; $allowedValuesPhp = new Format($allowedValuesPhp); } // require block $requiredPhp = new Format(); if ($required) { $requiredPhp = "\n\$resolver->setRequired(\n" . (new Format(Php::varExport($required)))->indent(4) . "\n);"; $requiredPhp = new Format($requiredPhp); $requiredPhp->indent(24); } // optional $optionalPhp = new Format(); if ($optional) { $optionalPhp = "\n\$resolver->setOptional(\n" . (new Format(Php::varExport($optional)))->indent(4) . "\n);"; $optionalPhp = new Format($optionalPhp); $optionalPhp->indent(24); } $this->class->addUses('Symfony\\Component\\OptionsResolver\\OptionsResolver'); $this->class->classComponents[] = new FunctionDeclaration('resolverGet', new Sformatf(<<<'PHP' /** * Resolver getter. Singleton * @return Symfony\Component\Options\Resolver */ public function resolverGet() { // do we already have a resolver instance if( !$this->resolver ) { $resolver = new OptionsResolver();%s%s%s $this->resolver = $resolver; } return $this->resolver; } PHP , $requiredPhp, $optionalPhp, $allowedValuesPhp)); $this->class->classComponents[] = new VariableDeclaration('resolver', new SFormatf(<<<'PHP' /** * Options Resolver * @var array */ private $resolver; PHP )); }
/** * Symfony Simple named constraint with 1 single / no arguments, i.e. * NotBlank, Blank, Null, NotNull, MinLength, MaxLength, etc. * @param mixed $column * @param mixed $contraint * @return mixed */ private function getSymfonyConstraint($column, $contraint, $args = null, $addUses = true) { if ($addUses) { $this->class->addUses(sprintf('Symfony\\Component\\Validator\\Constraints\\%s', $contraint)); } if (is_null($args)) { $output = <<<PHP \$metadata->addPropertyConstraint( '%s', new %s() ); PHP; } else { $output = <<<PHP \$metadata->addPropertyConstraint( '%s', new %s( %s ) ); PHP; } return (new Sformatf($output, addslashes($column), $contraint, Php::varExport($args, 8, true)))->indent(4); }