本文整理汇总了PHP中ReflectionParameter::isOptional方法的典型用法代码示例。如果您正苦于以下问题:PHP ReflectionParameter::isOptional方法的具体用法?PHP ReflectionParameter::isOptional怎么用?PHP ReflectionParameter::isOptional使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ReflectionParameter
的用法示例。
在下文中一共展示了ReflectionParameter::isOptional方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: from
/**
* @return self
*/
public static function from(\ReflectionParameter $from)
{
$param = new static($from->getName());
$param->reference = $from->isPassedByReference();
if (PHP_VERSION_ID >= 70000) {
$param->typeHint = $from->hasType() ? (string) $from->getType() : NULL;
} elseif ($from->isArray()) {
$param->typeHint = 'array';
} elseif (PHP_VERSION_ID >= 50400 && $from->isCallable()) {
$param->typeHint = 'callable';
} else {
try {
$param->typeHint = $from->getClass() ? $from->getClass()->getName() : NULL;
} catch (\ReflectionException $e) {
if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
$param->typeHint = $m[1];
} else {
throw $e;
}
}
}
$param->optional = PHP_VERSION_ID < 50407 ? $from->isOptional() || $param->typeHint && $from->allowsNull() : $from->isDefaultValueAvailable();
$param->defaultValue = PHP_VERSION_ID === 50316 ? $from->isOptional() : $from->isDefaultValueAvailable() ? $from->getDefaultValue() : NULL;
return $param;
}
示例2: from
/**
* @return self
*/
public static function from(\ReflectionParameter $from)
{
$param = new static();
$param->name = $from->getName();
$param->reference = $from->isPassedByReference();
if ($from->isArray()) {
$param->typeHint = 'array';
} elseif (PHP_VERSION_ID >= 50400 && $from->isCallable()) {
$param->typeHint = 'callable';
} else {
try {
$param->typeHint = $from->getClass() ? '\\' . $from->getClass()->getName() : NULL;
} catch (\ReflectionException $e) {
if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
$param->typeHint = '\\' . $m[1];
} else {
throw $e;
}
}
}
$param->optional = PHP_VERSION_ID < 50407 ? $from->isOptional() || $param->typeHint && $from->allowsNull() : $from->isDefaultValueAvailable();
$param->defaultValue = PHP_VERSION_ID === 50316 ? $from->isOptional() : $from->isDefaultValueAvailable() ? $from->getDefaultValue() : NULL;
$namespace = $from->getDeclaringClass() ? $from->getDeclaringClass()->getNamespaceName() : NULL;
$namespace = $namespace ? "\\{$namespace}\\" : '\\';
if (Nette\Utils\Strings::startsWith($param->typeHint, $namespace)) {
$param->typeHint = substr($param->typeHint, strlen($namespace));
}
return $param;
}
示例3: getDeclaration
public function getDeclaration()
{
if ($this->reflector->isArray()) {
$code = 'array ';
} else {
$class = $this->reflector->getClass();
if ($class !== null) {
$code = $class->name . ' ';
} else {
$code = '';
}
}
$code .= '$' . $this->reflector->name;
if ($this->reflector->isOptional()) {
$default = $this->reflector->getDefaultValue();
if (is_null($default)) {
$default = 'null';
} elseif (is_bool($default)) {
$default = $default ? 'true' : 'false';
} elseif (is_string($default)) {
$default = "'" . $default . "'";
} elseif (is_numeric($default)) {
$default = strval($default);
} elseif (is_array($default)) {
$default = 'array()';
} else {
echo 'Warning: unknown default type for ' . $this->getMethod()->getFullName() . PHP_EOL;
var_dump($default);
$default = 'null';
}
$code .= ' = ' . $default;
}
return $code;
}
示例4: exportCode
public function exportCode()
{
$default_value = null;
if ($this->_parameter->isDefaultValueAvailable()) {
$default_value = $this->_parameter->getDefaultValue();
if (is_scalar($default_value) && !is_numeric($default_value)) {
$default_value = "'{$default_value}'";
}
} elseif ($this->_parameter->isOptional()) {
$default_value = 'NULL';
}
return sprintf('%s%s$%s%s', $this->_parameter->getClass() ? "{$this->_parameter->getClass()->getName()} " : '', $this->_parameter->isPassedByReference() ? '&' : '', $this->_parameter->getName(), $default_value ? " = {$default_value}" : '');
}
示例5: getParam
protected function getParam(\ReflectionParameter $param)
{
$name = $param->getName();
if (!isset($this->args['params'][$name]) && !$param->isOptional()) {
$message = sprintf('You must define param "%s" for "%s"', $name, $this->args['class']);
throw new \OutOfBoundsException($message);
}
if (!isset($this->args['params'][$name]) && $param->isOptional()) {
return $param->getDefaultValue();
}
$value = $this->args['params'][$name];
$param = is_string($value) && defined($value) ? constant($value) : $value;
return $param;
}
示例6: checkArgument
protected function checkArgument(ReflectionParameter $argument, $i)
{
if ($argument->isOptional() == false && !isset(Request::$get[$i])) {
self::jump(400);
}
return $argument;
}
示例7: isOptional
/**
* Returns whether this parameter is an optional parameter
* @return boolean
* @since PHP 5.0.3
*/
public function isOptional()
{
if ($this->parameter != null) {
return $this->parameter->isOptional();
} else {
return parent::isOptional();
}
}
示例8: getParameterDependency
/**
* @param Horde_Injector $injector
* @param ReflectionParameter $method
*
* @return mixed
* @throws Horde_Injector_Exception
*/
public function getParameterDependency(Horde_Injector $injector, ReflectionParameter $parameter)
{
if ($parameter->getClass()) {
return $injector->getInstance($parameter->getClass()->getName());
} elseif ($parameter->isOptional()) {
return $parameter->getDefaultValue();
}
throw new Horde_Injector_Exception("Untyped parameter \$" . $parameter->getName() . "can't be fulfilled");
}
示例9:
function __construct(API_Doc_Method $method, ReflectionParameter $parameter)
{
$this->name = $parameter->getName();
$this->is_passed_by_reference = $parameter->isPassedByReference();
$this->allows_null = $parameter->allowsNull();
$this->is_optional = $parameter->isOptional();
$this->is_default_value_available = $parameter->isDefaultValueAvailable();
$this->position = $parameter->getPosition();
$this->declaring_method = $method;
}
示例10: __construct
protected function __construct(\ReflectionParameter $parameter, $overridenValue)
{
if (!$overridenValue instanceof MissingParameter && !$overridenValue instanceof DefaultParameter) {
$this->setDefaultValue($overridenValue);
} else {
if ($parameter && $parameter->isOptional()) {
$this->setDefaultValue($parameter->getDefaultValue());
}
}
}
示例11: testWrappedMethods
public function testWrappedMethods()
{
$php_parameter = new \ReflectionParameter([$this, 'method'], 'param');
$our_parameter = new ReflectionParameter($php_parameter);
$this->assertSame($php_parameter->getName(), $our_parameter->getName());
$this->assertSame($php_parameter->allowsNull(), $our_parameter->allowsNull());
$this->assertSame($php_parameter->isOptional(), $our_parameter->isOptional());
$this->assertSame($php_parameter->isDefaultValueAvailable(), $our_parameter->isDefaultValueAvailable());
$this->assertSame($php_parameter->isVariadic(), $our_parameter->isVariadic());
$this->assertSame($php_parameter->isPassedByReference(), $our_parameter->isPassedByReference());
$this->assertSame($php_parameter->getDefaultValue(), $our_parameter->getDefaultValue());
}
示例12: getParameterExtraValue
protected function getParameterExtraValue(\ReflectionParameter $parameter)
{
if (empty($this->extraValues)) {
if ($parameter->isOptional()) {
return $parameter->getDefaultValue();
} else {
throw new Exception("");
}
} else {
return array_pop($extras);
}
}
示例13: getArgument
private function getArgument(\ReflectionParameter $argument)
{
if ($argument->isOptional()) {
return $argument->getDefaultValue();
}
if ($argument->allowsNull()) {
return null;
}
if ($argument->getClass()) {
return $this->getMockBuilder($argument->getClass()->getName())->disableOriginalConstructor()->getMock();
}
return null;
}
示例14: addParameter
/**
* @param \ReflectionParameter $parameter
* @param string $description
*/
public function addParameter(\ReflectionParameter $parameter, $description)
{
$name = $parameter->getName();
if (!$parameter->isOptional()) {
$this->addArgument($name, InputArgument::REQUIRED, $description);
return;
}
if ($this->isFlagOption($parameter)) {
$this->addOption($name, null, InputOption::VALUE_NONE, $description);
return;
}
$default = $parameter->getDefaultValue();
$this->addArgument($name, InputArgument::OPTIONAL, $description, $default);
}
示例15: resolveArgument
private function resolveArgument(\ReflectionParameter $parameter)
{
$name = StringUtil::underscore($parameter->name);
if ('container' === $name) {
return $this->container;
}
if (isset($this->container[$name])) {
return $this->container[$name];
}
if ($parameter->isOptional()) {
return $parameter->getDefaultValue();
}
throw new \RuntimeException(sprintf('Unable to resolve parameter "%s" of class "%s" no service/parameter found with name "%s". ' . 'Consider adding a default value.', $name, $parameter->getDeclaringClass()->name, $name));
}