1<?php
2
3declare(strict_types=1);
4
5namespace Metadata;
6
7/**
8 * Base class for method metadata.
9 *
10 * This class is intended to be extended to add your application specific
11 * properties, and flags.
12 *
13 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
14 */
15class MethodMetadata implements \Serializable
16{
17    /**
18     * @var string
19     */
20    public $class;
21
22    /**
23     * @var string
24     */
25    public $name;
26
27    /**
28     * @var \ReflectionMethod
29     */
30    public $reflection;
31
32    public function __construct(string $class, string $name)
33    {
34        $this->class = $class;
35        $this->name = $name;
36
37        $this->reflection = new \ReflectionMethod($class, $name);
38        $this->reflection->setAccessible(true);
39    }
40
41    /**
42     * @param mixed[] $args
43     *
44     * @return mixed
45     */
46    public function invoke(object $obj, array $args = [])
47    {
48        return $this->reflection->invokeArgs($obj, $args);
49    }
50
51    /**
52     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
53     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
54     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.UselessReturnAnnotation
55     *
56     * @return string
57     */
58    public function serialize()
59    {
60        return serialize([$this->class, $this->name]);
61    }
62
63    /**
64     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
65     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
66     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.UselessReturnAnnotation
67     *
68     * @param string $str
69     * @return void
70     */
71    public function unserialize($str)
72    {
73        list($this->class, $this->name) = unserialize($str);
74
75        $this->reflection = new \ReflectionMethod($this->class, $this->name);
76        $this->reflection->setAccessible(true);
77    }
78}
79