/src/Symfony/Component/DependencyInjection/SimpleXMLElement.php

https://github.com/Exercise/symfony · PHP · 127 lines · 76 code · 13 blank · 38 comment · 17 complexity · 1ed3e3a218fbc907f498889178fa03df MD5 · raw file

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