SimpleXMLElement.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection;
  11. /**
  12. * SimpleXMLElement class.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class SimpleXMLElement extends \SimpleXMLElement
  17. {
  18. /**
  19. * Converts an attribute as a php type.
  20. *
  21. * @param string $name
  22. * @return mixed
  23. */
  24. public function getAttributeAsPhp($name)
  25. {
  26. return self::phpize($this[$name]);
  27. }
  28. /**
  29. * Returns arguments as valid php types.
  30. *
  31. * @param string $name
  32. * @return mixed
  33. */
  34. public function getArgumentsAsPhp($name, $lowercase = true)
  35. {
  36. $arguments = array();
  37. foreach ($this->$name as $arg) {
  38. if (isset($arg['name'])) {
  39. $arg['key'] = (string) $arg['name'];
  40. }
  41. $key = isset($arg['key']) ? (string) $arg['key'] : (!$arguments ? 0 : max(array_keys($arguments)) + 1);
  42. // parameter keys are case insensitive
  43. if ('parameter' == $name && $lowercase) {
  44. $key = strtolower($key);
  45. }
  46. // this is used by DefinitionDecorator to overwrite a specific
  47. // argument of the parent definition
  48. if (isset($arg['index'])) {
  49. $key = 'index_'.$arg['index'];
  50. }
  51. switch ($arg['type']) {
  52. case 'service':
  53. $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
  54. if (isset($arg['on-invalid']) && 'ignore' == $arg['on-invalid']) {
  55. $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
  56. } elseif (isset($arg['on-invalid']) && 'null' == $arg['on-invalid']) {
  57. $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
  58. }
  59. if (isset($arg['strict'])) {
  60. $strict = self::phpize($arg['strict']);
  61. } else {
  62. $strict = true;
  63. }
  64. $arguments[$key] = new Reference((string) $arg['id'], $invalidBehavior, $strict);
  65. break;
  66. case 'collection':
  67. $arguments[$key] = $arg->getArgumentsAsPhp($name, false);
  68. break;
  69. case 'string':
  70. $arguments[$key] = (string) $arg;
  71. break;
  72. case 'constant':
  73. $arguments[$key] = constant((string) $arg);
  74. break;
  75. default:
  76. $arguments[$key] = self::phpize($arg);
  77. }
  78. }
  79. return $arguments;
  80. }
  81. /**
  82. * Converts an xml value to a php type.
  83. *
  84. * @param mixed $value
  85. * @return mixed
  86. */
  87. static public function phpize($value)
  88. {
  89. $value = (string) $value;
  90. $lowercaseValue = strtolower($value);
  91. switch (true) {
  92. case 'null' === $lowercaseValue:
  93. return null;
  94. case ctype_digit($value):
  95. return '0' == $value[0] ? octdec($value) : intval($value);
  96. case 'true' === $lowercaseValue:
  97. return true;
  98. case 'false' === $lowercaseValue:
  99. return false;
  100. case is_numeric($value):
  101. return '0x' == $value[0].$value[1] ? hexdec($value) : floatval($value);
  102. case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $value):
  103. return floatval(str_replace(',', '', $value));
  104. default:
  105. return $value;
  106. }
  107. }
  108. }