SimpleXMLElement.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. * @param Boolean $lowercase
  33. *
  34. * @return mixed
  35. */
  36. public function getArgumentsAsPhp($name, $lowercase = true)
  37. {
  38. $arguments = array();
  39. foreach ($this->$name as $arg) {
  40. if (isset($arg['name'])) {
  41. $arg['key'] = (string) $arg['name'];
  42. }
  43. $key = isset($arg['key']) ? (string) $arg['key'] : (!$arguments ? 0 : max(array_keys($arguments)) + 1);
  44. // parameter keys are case insensitive
  45. if ('parameter' == $name && $lowercase) {
  46. $key = strtolower($key);
  47. }
  48. // this is used by DefinitionDecorator to overwrite a specific
  49. // argument of the parent definition
  50. if (isset($arg['index'])) {
  51. $key = 'index_'.$arg['index'];
  52. }
  53. switch ($arg['type']) {
  54. case 'service':
  55. $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
  56. if (isset($arg['on-invalid']) && 'ignore' == $arg['on-invalid']) {
  57. $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
  58. } elseif (isset($arg['on-invalid']) && 'null' == $arg['on-invalid']) {
  59. $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
  60. }
  61. if (isset($arg['strict'])) {
  62. $strict = self::phpize($arg['strict']);
  63. } else {
  64. $strict = true;
  65. }
  66. $arguments[$key] = new Reference((string) $arg['id'], $invalidBehavior, $strict);
  67. break;
  68. case 'collection':
  69. $arguments[$key] = $arg->getArgumentsAsPhp($name, false);
  70. break;
  71. case 'string':
  72. $arguments[$key] = (string) $arg;
  73. break;
  74. case 'constant':
  75. $arguments[$key] = constant((string) $arg);
  76. break;
  77. default:
  78. $arguments[$key] = self::phpize($arg);
  79. }
  80. }
  81. return $arguments;
  82. }
  83. /**
  84. * Converts an xml value to a php type.
  85. *
  86. * @param mixed $value
  87. * @return mixed
  88. */
  89. static public function phpize($value)
  90. {
  91. $value = (string) $value;
  92. $lowercaseValue = strtolower($value);
  93. switch (true) {
  94. case 'null' === $lowercaseValue:
  95. return null;
  96. case ctype_digit($value):
  97. return '0' == $value[0] ? octdec($value) : intval($value);
  98. case 'true' === $lowercaseValue:
  99. return true;
  100. case 'false' === $lowercaseValue:
  101. return false;
  102. case is_numeric($value):
  103. return '0x' == $value[0].$value[1] ? hexdec($value) : floatval($value);
  104. case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $value):
  105. return floatval(str_replace(',', '', $value));
  106. default:
  107. return $value;
  108. }
  109. }
  110. }