1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6: */
7:
8: namespace Nette\PhpGenerator\Traits;
9:
10: use Nette;
11: use Nette\PhpGenerator\Helpers;
12: use Nette\PhpGenerator\Parameter;
13: use Nette\PhpGenerator\PhpNamespace;
14:
15:
16: /**
17: * @internal
18: */
19: trait FunctionLike
20: {
21: /** @var string */
22: private $body = '';
23:
24: /** @var array of name => Parameter */
25: private $parameters = [];
26:
27: /** @var bool */
28: private $variadic = false;
29:
30: /** @var string|null */
31: private $returnType;
32:
33: /** @var bool */
34: private $returnReference = false;
35:
36: /** @var bool */
37: private $returnNullable = false;
38:
39: /** @var PhpNamespace|null */
40: private $namespace;
41:
42:
43: /**
44: * @param string
45: * @return static
46: */
47: public function setBody($code, array $args = null)
48: {
49: $this->body = $args === null ? $code : Helpers::formatArgs($code, $args);
50: return $this;
51: }
52:
53:
54: /**
55: * @return string
56: */
57: public function getBody()
58: {
59: return $this->body;
60: }
61:
62:
63: /**
64: * @param string
65: * @return static
66: */
67: public function addBody($code, array $args = null)
68: {
69: $this->body .= ($args === null ? $code : Helpers::formatArgs($code, $args)) . "\n";
70: return $this;
71: }
72:
73:
74: /**
75: * @param Parameter[]
76: * @return static
77: */
78: public function setParameters(array $val)
79: {
80: $this->parameters = [];
81: foreach ($val as $v) {
82: if (!$v instanceof Parameter) {
83: throw new Nette\InvalidArgumentException('Argument must be Nette\PhpGenerator\Parameter[].');
84: }
85: $this->parameters[$v->getName()] = $v;
86: }
87: return $this;
88: }
89:
90:
91: /**
92: * @return Parameter[]
93: */
94: public function getParameters()
95: {
96: return $this->parameters;
97: }
98:
99:
100: /**
101: * @param string without $
102: * @return Parameter
103: */
104: public function addParameter($name, $defaultValue = null)
105: {
106: $param = new Parameter($name);
107: if (func_num_args() > 1) {
108: $param->setOptional(true)->setDefaultValue($defaultValue);
109: }
110: return $this->parameters[$name] = $param;
111: }
112:
113:
114: /**
115: * @param bool
116: * @return static
117: */
118: public function setVariadic($state = true)
119: {
120: $this->variadic = (bool) $state;
121: return $this;
122: }
123:
124:
125: /**
126: * @return bool
127: */
128: public function isVariadic()
129: {
130: return $this->variadic;
131: }
132:
133:
134: /**
135: * @param string|null
136: * @return static
137: */
138: public function setReturnType($val)
139: {
140: $this->returnType = $val ? (string) $val : null;
141: return $this;
142: }
143:
144:
145: /**
146: * @return string|null
147: */
148: public function getReturnType()
149: {
150: return $this->returnType;
151: }
152:
153:
154: /**
155: * @param bool
156: * @return static
157: */
158: public function setReturnReference($state = true)
159: {
160: $this->returnReference = (bool) $state;
161: return $this;
162: }
163:
164:
165: /**
166: * @return bool
167: */
168: public function getReturnReference()
169: {
170: return $this->returnReference;
171: }
172:
173:
174: /**
175: * @param bool
176: * @return static
177: */
178: public function setReturnNullable($state = true)
179: {
180: $this->returnNullable = (bool) $state;
181: return $this;
182: }
183:
184:
185: /**
186: * @return bool
187: */
188: public function getReturnNullable()
189: {
190: return $this->returnNullable;
191: }
192:
193:
194: /**
195: * @return static
196: */
197: public function setNamespace(PhpNamespace $val = null)
198: {
199: $this->namespace = $val;
200: return $this;
201: }
202:
203:
204: /**
205: * @return string
206: */
207: protected function parametersToString()
208: {
209: $params = [];
210: foreach ($this->parameters as $param) {
211: $variadic = $this->variadic && $param === end($this->parameters);
212: $hint = $param->getTypeHint();
213: $params[] = ($hint ? ($param->isNullable() ? '?' : '') . ($this->namespace ? $this->namespace->unresolveName($hint) : $hint) . ' ' : '')
214: . ($param->isReference() ? '&' : '')
215: . ($variadic ? '...' : '')
216: . '$' . $param->getName()
217: . ($param->hasDefaultValue() && !$variadic ? ' = ' . Helpers::dump($param->defaultValue) : '');
218: }
219:
220: return strlen($tmp = implode(', ', $params)) > Helpers::WRAP_LENGTH && count($params) > 1
221: ? "(\n\t" . implode(",\n\t", $params) . "\n)"
222: : "($tmp)";
223: }
224:
225:
226: /**
227: * @return string
228: */
229: protected function returnTypeToString()
230: {
231: return $this->returnType
232: ? ': ' . ($this->returnNullable ? '?' : '') . ($this->namespace ? $this->namespace->unresolveName($this->returnType) : $this->returnType)
233: : '';
234: }
235: }
236: