Parameter.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (https://nette.org)
  4. * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  5. */
  6. declare(strict_types=1);
  7. namespace Nette\PhpGenerator;
  8. use Nette;
  9. use Nette\Utils\Type;
  10. /**
  11. * Function/Method parameter description.
  12. *
  13. * @property mixed $defaultValue
  14. */
  15. class Parameter
  16. {
  17. use Nette\SmartObject;
  18. use Traits\NameAware;
  19. use Traits\AttributeAware;
  20. /** @var bool */
  21. private $reference = false;
  22. /** @var string|null */
  23. private $type;
  24. /** @var bool */
  25. private $nullable = false;
  26. /** @var bool */
  27. private $hasDefaultValue = false;
  28. /** @var mixed */
  29. private $defaultValue;
  30. /** @return static */
  31. public function setReference(bool $state = true): self
  32. {
  33. $this->reference = $state;
  34. return $this;
  35. }
  36. public function isReference(): bool
  37. {
  38. return $this->reference;
  39. }
  40. /** @return static */
  41. public function setType(?string $type): self
  42. {
  43. $this->type = Helpers::validateType($type, $this->nullable);
  44. return $this;
  45. }
  46. /**
  47. * @return Type|string|null
  48. */
  49. public function getType(bool $asObject = false)
  50. {
  51. return $asObject && $this->type
  52. ? Type::fromString($this->type)
  53. : $this->type;
  54. }
  55. /** @deprecated use setType() */
  56. public function setTypeHint(?string $type): self
  57. {
  58. return $this->setType($type);
  59. }
  60. /** @deprecated use getType() */
  61. public function getTypeHint(): ?string
  62. {
  63. return $this->getType();
  64. }
  65. /**
  66. * @deprecated just use setDefaultValue()
  67. * @return static
  68. */
  69. public function setOptional(bool $state = true): self
  70. {
  71. trigger_error(__METHOD__ . '() is deprecated, use setDefaultValue()', E_USER_DEPRECATED);
  72. $this->hasDefaultValue = $state;
  73. return $this;
  74. }
  75. /** @return static */
  76. public function setNullable(bool $state = true): self
  77. {
  78. $this->nullable = $state;
  79. return $this;
  80. }
  81. public function isNullable(): bool
  82. {
  83. return $this->nullable;
  84. }
  85. /** @return static */
  86. public function setDefaultValue($val): self
  87. {
  88. $this->defaultValue = $val;
  89. $this->hasDefaultValue = true;
  90. return $this;
  91. }
  92. public function getDefaultValue()
  93. {
  94. return $this->defaultValue;
  95. }
  96. public function hasDefaultValue(): bool
  97. {
  98. return $this->hasDefaultValue;
  99. }
  100. public function validate(): void
  101. {
  102. }
  103. }