3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Code\Generator
;
12 use Zend\Code\Reflection\MethodReflection
;
14 class MethodGenerator
extends AbstractMemberGenerator
17 * @var DocBlockGenerator
19 protected $docBlock = null;
22 * @var ParameterGenerator[]
24 protected $parameters = array();
29 protected $body = null;
32 * @param MethodReflection $reflectionMethod
33 * @return MethodGenerator
35 public static function fromReflection(MethodReflection
$reflectionMethod)
37 $method = new static();
39 $method->setSourceContent($reflectionMethod->getContents(false));
40 $method->setSourceDirty(false);
42 if ($reflectionMethod->getDocComment() != '') {
43 $method->setDocBlock(DocBlockGenerator
::fromReflection($reflectionMethod->getDocBlock()));
46 $method->setFinal($reflectionMethod->isFinal());
48 if ($reflectionMethod->isPrivate()) {
49 $method->setVisibility(self
::VISIBILITY_PRIVATE
);
50 } elseif ($reflectionMethod->isProtected()) {
51 $method->setVisibility(self
::VISIBILITY_PROTECTED
);
53 $method->setVisibility(self
::VISIBILITY_PUBLIC
);
56 $method->setStatic($reflectionMethod->isStatic());
58 $method->setName($reflectionMethod->getName());
60 foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
61 $method->setParameter(ParameterGenerator
::fromReflection($reflectionParameter));
64 $method->setBody($reflectionMethod->getBody());
72 * @configkey name string [required] Class Name
73 * @configkey docblock string The docblock information
74 * @configkey flags int Flags, one of MethodGenerator::FLAG_ABSTRACT MethodGenerator::FLAG_FINAL
75 * @configkey parameters string Class which this class is extending
76 * @configkey body string
77 * @configkey abstract bool
78 * @configkey final bool
79 * @configkey static bool
80 * @configkey visibility string
82 * @throws Exception\InvalidArgumentException
84 * @return MethodGenerator
86 public static function fromArray(array $array)
88 if (!isset($array['name'])) {
89 throw new Exception\
InvalidArgumentException(
90 'Method generator requires that a name is provided for this object'
94 $method = new static($array['name']);
95 foreach ($array as $name => $value) {
97 switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
99 $docBlock = ($value instanceof DocBlockGenerator
) ?
$value : DocBlockGenerator
::fromArray($value);
100 $method->setDocBlock($docBlock);
103 $method->setFlags($value);
106 $method->setParameters($value);
109 $method->setBody($value);
112 $method->setAbstract($value);
115 $method->setFinal($value);
118 $method->setStatic($value);
121 $method->setVisibility($value);
130 * @param string $name
131 * @param array $parameters
132 * @param int|array $flags
133 * @param string $body
134 * @param DocBlockGenerator|string $docBlock
136 public function __construct($name = null, array $parameters = array(), $flags = self
::FLAG_PUBLIC
, $body = null,
140 $this->setName($name);
143 $this->setParameters($parameters);
145 if ($flags !== self
::FLAG_PUBLIC
) {
146 $this->setFlags($flags);
149 $this->setBody($body);
152 $this->setDocBlock($docBlock);
157 * @param array $parameters
158 * @return MethodGenerator
160 public function setParameters(array $parameters)
162 foreach ($parameters as $parameter) {
163 $this->setParameter($parameter);
170 * @param ParameterGenerator|string $parameter
171 * @throws Exception\InvalidArgumentException
172 * @return MethodGenerator
174 public function setParameter($parameter)
176 if (is_string($parameter)) {
177 $parameter = new ParameterGenerator($parameter);
178 } elseif (!$parameter instanceof ParameterGenerator
) {
179 throw new Exception\
InvalidArgumentException(sprintf(
180 '%s is expecting either a string, array or an instance of %s\ParameterGenerator',
186 $parameterName = $parameter->getName();
188 $this->parameters
[$parameterName] = $parameter;
194 * @return ParameterGenerator[]
196 public function getParameters()
198 return $this->parameters
;
202 * @param string $body
203 * @return MethodGenerator
205 public function setBody($body)
214 public function getBody()
222 public function generate()
226 $indent = $this->getIndentation();
228 if (($docBlock = $this->getDocBlock()) !== null) {
229 $docBlock->setIndentation($indent);
230 $output .= $docBlock->generate();
235 if ($this->isAbstract()) {
236 $output .= 'abstract ';
238 $output .= (($this->isFinal()) ?
'final ' : '');
241 $output .= $this->getVisibility()
242 . (($this->isStatic()) ?
' static' : '')
243 . ' function ' . $this->getName() . '(';
245 $parameters = $this->getParameters();
246 if (!empty($parameters)) {
247 foreach ($parameters as $parameter) {
248 $parameterOutput[] = $parameter->generate();
251 $output .= implode(', ', $parameterOutput);
256 if ($this->isAbstract()) {
257 return $output . ';';
260 $output .= self
::LINE_FEED
. $indent . '{' . self
::LINE_FEED
;
263 $output .= preg_replace('#^(.+?)$#m', $indent . $indent . '$1', trim($this->body
))
267 $output .= $indent . '}' . self
::LINE_FEED
;
272 public function __toString()
274 return $this->generate();