FactoryParameter.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /*
  3. Copyright (c) 2009 hamcrest.org
  4. */
  5. class FactoryParameter
  6. {
  7. /**
  8. * @var FactoryMethod
  9. */
  10. private $method;
  11. /**
  12. * @var ReflectionParameter
  13. */
  14. private $reflector;
  15. public function __construct(FactoryMethod $method, ReflectionParameter $reflector)
  16. {
  17. $this->method = $method;
  18. $this->reflector = $reflector;
  19. }
  20. public function getDeclaration()
  21. {
  22. if ($this->reflector->isArray()) {
  23. $code = 'array ';
  24. } else {
  25. $class = $this->reflector->getClass();
  26. if ($class !== null) {
  27. $code = '\\' . $class->name . ' ';
  28. } else {
  29. $code = '';
  30. }
  31. }
  32. $code .= '$' . $this->reflector->name;
  33. if ($this->reflector->isOptional()) {
  34. $default = $this->reflector->getDefaultValue();
  35. if (is_null($default)) {
  36. $default = 'null';
  37. } elseif (is_bool($default)) {
  38. $default = $default ? 'true' : 'false';
  39. } elseif (is_string($default)) {
  40. $default = "'" . $default . "'";
  41. } elseif (is_numeric($default)) {
  42. $default = strval($default);
  43. } elseif (is_array($default)) {
  44. $default = 'array()';
  45. } else {
  46. echo 'Warning: unknown default type for ' . $this->getMethod()->getFullName() . PHP_EOL;
  47. var_dump($default);
  48. $default = 'null';
  49. }
  50. $code .= ' = ' . $default;
  51. }
  52. return $code;
  53. }
  54. public function getInvocation()
  55. {
  56. return '$' . $this->reflector->name;
  57. }
  58. public function getMethod()
  59. {
  60. return $this->method;
  61. }
  62. }