Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Code / Generator / MethodGenerator.php
blobcb246f33907d69f46330b459c0cbe1d761318846
1 <?php
2 /**
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
8 */
10 namespace Zend\Code\Generator;
12 use Zend\Code\Reflection\MethodReflection;
14 class MethodGenerator extends AbstractMemberGenerator
16 /**
17 * @var DocBlockGenerator
19 protected $docBlock = null;
21 /**
22 * @var ParameterGenerator[]
24 protected $parameters = array();
26 /**
27 * @var string
29 protected $body = null;
31 /**
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);
52 } else {
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());
66 return $method;
69 /**
70 * Generate from array
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
83 * @param array $array
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) {
96 // normalize key
97 switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
98 case 'docblock':
99 $docBlock = ($value instanceof DocBlockGenerator) ? $value : DocBlockGenerator::fromArray($value);
100 $method->setDocBlock($docBlock);
101 break;
102 case 'flags':
103 $method->setFlags($value);
104 break;
105 case 'parameters':
106 $method->setParameters($value);
107 break;
108 case 'body':
109 $method->setBody($value);
110 break;
111 case 'abstract':
112 $method->setAbstract($value);
113 break;
114 case 'final':
115 $method->setFinal($value);
116 break;
117 case 'static':
118 $method->setStatic($value);
119 break;
120 case 'visibility':
121 $method->setVisibility($value);
122 break;
126 return $method;
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,
137 $docBlock = null)
139 if ($name) {
140 $this->setName($name);
142 if ($parameters) {
143 $this->setParameters($parameters);
145 if ($flags !== self::FLAG_PUBLIC) {
146 $this->setFlags($flags);
148 if ($body) {
149 $this->setBody($body);
151 if ($docBlock) {
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);
166 return $this;
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',
181 __METHOD__,
182 __NAMESPACE__
186 $parameterName = $parameter->getName();
188 $this->parameters[$parameterName] = $parameter;
190 return $this;
194 * @return ParameterGenerator[]
196 public function getParameters()
198 return $this->parameters;
202 * @param string $body
203 * @return MethodGenerator
205 public function setBody($body)
207 $this->body = $body;
208 return $this;
212 * @return string
214 public function getBody()
216 return $this->body;
220 * @return string
222 public function generate()
224 $output = '';
226 $indent = $this->getIndentation();
228 if (($docBlock = $this->getDocBlock()) !== null) {
229 $docBlock->setIndentation($indent);
230 $output .= $docBlock->generate();
233 $output .= $indent;
235 if ($this->isAbstract()) {
236 $output .= 'abstract ';
237 } else {
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);
254 $output .= ')';
256 if ($this->isAbstract()) {
257 return $output . ';';
260 $output .= self::LINE_FEED . $indent . '{' . self::LINE_FEED;
262 if ($this->body) {
263 $output .= preg_replace('#^(.+?)$#m', $indent . $indent . '$1', trim($this->body))
264 . self::LINE_FEED;
267 $output .= $indent . '}' . self::LINE_FEED;
269 return $output;
272 public function __toString()
274 return $this->generate();