Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReflectionObject instead of Object #31

Closed
MlleDelphine opened this issue Sep 26, 2017 · 1 comment
Closed

ReflectionObject instead of Object #31

MlleDelphine opened this issue Sep 26, 2017 · 1 comment

Comments

@MlleDelphine
Copy link

Hi !

I would like to use callback but it seems that there's an issue.
I am using Symfony 3.1.10.

MyEntityObject.php


/**
     * @var string
     *
     * @ORM\Column(name="masterNumber", type="string", length=255, unique=true)
     * @Generate(generator="string", options={"length"=26}, callbacks={"setPrefix"="getPrefix"})
     * @JMSSer\Expose
     * @JMSSer\Groups({"bo_export_masterservicerequest", "crm"})
     */
    private $masterNumber;

  public function getPrefix(){
        $concat = "my logic";
        return $concat;
    }

But, in GeneratorListener.php existence of method getPrefix (in callbacks annotation) is checked on the ReflectionObject of entity and obviously "getPrefix()" method does not exist in ReflectionObject class but methods of ReflectedObject (MyEntityObject) are available thanks to ReflectionObject->getMethods(). and callable thanks to, for each method contained, $method->invoke($myEntityObject, 'argument');

 /**
     * @param GeneratorInterface $generator
     * @param GeneratorAnnotation $annotation
     * @param \ReflectionObject $object
     */
    public function performCallbacks(GeneratorInterface $generator, GeneratorAnnotation $annotation, $object)
    {
        foreach($annotation->callbacks as $callback => $value){

            if($this->isMethod($generator, $callback)){

                if($this->isMethod($object, $value)){
                    $value = $object->$value();
                }
        [...]
            }
        }
    }

    /**
     * @param $class
     * @param $callback
     * @return bool
     */
    private function isMethod($class, $callback)
    {
        return method_exists($class, $callback) && is_callable([$class, $callback]);
    }
@Brunty
Copy link
Contributor

Brunty commented Sep 27, 2017

Hi @MlleDelphine - thanks for filing the issue, we'll get it looked into ASAP :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants