| 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 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\PhpGenerator\Traits; |
| 9 |
|
| 10 |
use Packetery\Nette; |
| 11 |
use Packetery\Nette\PhpGenerator\Dumper; |
| 12 |
use Packetery\Nette\PhpGenerator\Parameter; |
| 13 |
/** |
| 14 |
* @internal |
| 15 |
*/ |
| 16 |
trait FunctionLike |
| 17 |
{ |
| 18 |
/** @var string */ |
| 19 |
private $body = ''; |
| 20 |
/** @var Parameter[] */ |
| 21 |
private $parameters = []; |
| 22 |
/** @var bool */ |
| 23 |
private $variadic = \false; |
| 24 |
/** @var string|null */ |
| 25 |
private $returnType; |
| 26 |
/** @var bool */ |
| 27 |
private $returnReference = \false; |
| 28 |
/** @var bool */ |
| 29 |
private $returnNullable = \false; |
| 30 |
/** @return static */ |
| 31 |
public function setBody(string $code, array $args = null) : self |
| 32 |
{ |
| 33 |
$this->body = $args === null ? $code : (new Dumper())->format($code, ...$args); |
| 34 |
return $this; |
| 35 |
} |
| 36 |
public function getBody() : string |
| 37 |
{ |
| 38 |
return $this->body; |
| 39 |
} |
| 40 |
/** @return static */ |
| 41 |
public function addBody(string $code, array $args = null) : self |
| 42 |
{ |
| 43 |
$this->body .= ($args === null ? $code : (new Dumper())->format($code, ...$args)) . "\n"; |
| 44 |
return $this; |
| 45 |
} |
| 46 |
/** |
| 47 |
* @param Parameter[] $val |
| 48 |
* @return static |
| 49 |
*/ |
| 50 |
public function setParameters(array $val) : self |
| 51 |
{ |
| 52 |
$this->parameters = []; |
| 53 |
foreach ($val as $v) { |
| 54 |
if (!$v instanceof Parameter) { |
| 55 |
throw new \Packetery\Nette\InvalidArgumentException('Argument must be \\Packetery\\Nette\\PhpGenerator\\Parameter[].'); |
| 56 |
} |
| 57 |
$this->parameters[$v->getName()] = $v; |
| 58 |
} |
| 59 |
return $this; |
| 60 |
} |
| 61 |
/** @return Parameter[] */ |
| 62 |
public function getParameters() : array |
| 63 |
{ |
| 64 |
return $this->parameters; |
| 65 |
} |
| 66 |
/** |
| 67 |
* @param string $name without $ |
| 68 |
*/ |
| 69 |
public function addParameter(string $name, $defaultValue = null) : Parameter |
| 70 |
{ |
| 71 |
$param = new Parameter($name); |
| 72 |
if (\func_num_args() > 1) { |
| 73 |
$param->setDefaultValue($defaultValue); |
| 74 |
} |
| 75 |
return $this->parameters[$name] = $param; |
| 76 |
} |
| 77 |
/** |
| 78 |
* @param string $name without $ |
| 79 |
* @return static |
| 80 |
*/ |
| 81 |
public function removeParameter(string $name) : self |
| 82 |
{ |
| 83 |
unset($this->parameters[$name]); |
| 84 |
return $this; |
| 85 |
} |
| 86 |
/** @return static */ |
| 87 |
public function setVariadic(bool $state = \true) : self |
| 88 |
{ |
| 89 |
$this->variadic = $state; |
| 90 |
return $this; |
| 91 |
} |
| 92 |
public function isVariadic() : bool |
| 93 |
{ |
| 94 |
return $this->variadic; |
| 95 |
} |
| 96 |
/** @return static */ |
| 97 |
public function setReturnType(?string $type) : self |
| 98 |
{ |
| 99 |
if ($type && $type[0] === '?') { |
| 100 |
$type = \substr($type, 1); |
| 101 |
$this->returnNullable = \true; |
| 102 |
} |
| 103 |
$this->returnType = $type; |
| 104 |
return $this; |
| 105 |
} |
| 106 |
public function getReturnType() : ?string |
| 107 |
{ |
| 108 |
return $this->returnType; |
| 109 |
} |
| 110 |
/** @return static */ |
| 111 |
public function setReturnReference(bool $state = \true) : self |
| 112 |
{ |
| 113 |
$this->returnReference = $state; |
| 114 |
return $this; |
| 115 |
} |
| 116 |
public function getReturnReference() : bool |
| 117 |
{ |
| 118 |
return $this->returnReference; |
| 119 |
} |
| 120 |
/** @return static */ |
| 121 |
public function setReturnNullable(bool $state = \true) : self |
| 122 |
{ |
| 123 |
$this->returnNullable = $state; |
| 124 |
return $this; |
| 125 |
} |
| 126 |
public function isReturnNullable() : bool |
| 127 |
{ |
| 128 |
return $this->returnNullable; |
| 129 |
} |
| 130 |
/** @deprecated use isReturnNullable() */ |
| 131 |
public function getReturnNullable() : bool |
| 132 |
{ |
| 133 |
return $this->returnNullable; |
| 134 |
} |
| 135 |
/** @deprecated */ |
| 136 |
public function setNamespace(\Packetery\Nette\PhpGenerator\PhpNamespace $val = null) : self |
| 137 |
{ |
| 138 |
\trigger_error(__METHOD__ . '() is deprecated', \E_USER_DEPRECATED); |
| 139 |
return $this; |
| 140 |
} |
| 141 |
} |
| 142 |
|