FactoryParameter.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. }