/**
  * Standard constructor
  */
 public function __construct(Repository $repository, $namespace)
 {
     $this->repository = $repository;
     $this->class = new PhpClass($this->repository->class->class, $namespace, false);
     $this->class->addExtends($this->repository->class->getFullyQualifiedClassname(true));
     $this->class->setClassComment($this->repository->class->classComment);
 }
Beispiel #2
0
    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();
    }
    /**
     * Standard constructor
     */
    public function __construct(Entity $entity, $namespace)
    {
        $this->entity = $entity;
        $this->class = new PhpClass($this->entity->class->class, $namespace, false);
        $this->class->addExtends($this->entity->class->getFullyQualifiedClassname(true));
        $this->class->setClassComment($this->entity->class->classComment);
        $this->class->addUses('Symfony\\Component\\Validator\\Mapping\\ClassMetadata');
        $this->class->classComponents[] = new FunctionDeclaration('loadValidatorMetadata', (new Format(<<<'PHP'
                /**
                 * Symfony Validator Metadata.
                 * WARNING! Workaround. Symfony validator uses its own inheritance mechanism. This unusual setup is designed to short circuit that.
                 *
                 * @param ClassMetadata $metadata
                 * @return void
                 */
                public static function loadValidatorMetadata( ClassMetadata $metadata )
                {
                    parent::_loadValidatorMetaData( $metadata );
                }
PHP
))->deindent());
    }
Beispiel #4
0
    /**
     * 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
));
    }
Beispiel #5
0
    /**
     * 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);
    }