Example #1
0
    /**
     * Standard constructor
     */
    public function __construct($fullyQualifiedClass, array $registrations = [])
    {
        $this->class = new PhpClass(\Bond\get_unqualified_class($fullyQualifiedClass), \Bond\get_namespace($fullyQualifiedClass), false);
        if (!$registrations) {
            return;
        }
        foreach ($registrations as &$registration) {
            $registration = sprintf("\$em->register( '%s', '%s' );", addslashes($registration[0]), addslashes($registration[1]));
        }
        $registrations = new Format($registrations);
        $this->class->classComponents[] = new FunctionDeclaration('__construct', new Sformatf(<<<'PHP'
                /**
                 * Register a set of entities with a EntityManager
                 * @inheritDoc
                 */
                public function __construct( \Bond\EntityManager $em )
                {
%s
                }
PHP
, $registrations->indent(20)));
    }
Example #2
0
 public function testDuplicateEmptyLinesRemove()
 {
     $format = new Format(self::duplicateLines);
     $format->removeDuplicateEmptyLines();
     $this->assertSame(self::duplicateLinesRemoved, (string) $format);
 }
Example #3
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
));
    }
Example #4
0
 public function __construct()
 {
     $string = call_user_func_array('sprintf', func_get_args());
     parent::__construct($string);
     $this->deindent();
 }
Example #5
0
    /**
     * Build function declaration set()
     * @param array $lines Array of lines comprimising 'case:' (switch stylee) statements that form the bassis for function set(...)
     */
    private function formatSetFunctionDeclaration($lines)
    {
        // do we need a switch statement
        $switch = '';
        if (count($lines)) {
            $lines = new Format($lines);
            $switch = <<<PHP
    switch( \$key ) {

{$lines->indent(8)}
    }

PHP;
        }
        $fn = new Sformatf(<<<'PHP'
/**
 * Set entity property
 * {@inheritDoc}
 */
public function set( $key, $value = null, $inputValidate = null )
{
%s    return parent::set( $key, $value, $inputValidate );
}

PHP
, $switch);
        $this->class->classComponents[] = new FunctionDeclaration("set", $fn);
    }
Example #6
0
    public function render()
    {
        $output = new Format(sprintf(<<<'PHP'
<?php

namespace %s;

%s

%s%s
{%s}
PHP
, $this->namespace, $this->getUsesDeclarations(), $this->classComment, $this->getClassDeclaration(), $this->getClassBody()->indent(4)));
        return $output->removeDuplicateEmptyLines();
    }