SimpleXMLElement.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. use Symfony\Component\Config\Util\XmlUtils;
  12. /**
  13. * SimpleXMLElement class.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class SimpleXMLElement extends \SimpleXMLElement
  18. {
  19. /**
  20. * Converts an attribute as a php type.
  21. *
  22. * @param string $name
  23. *
  24. * @return mixed
  25. */
  26. public function getAttributeAsPhp($name)
  27. {
  28. return self::phpize($this[$name]);
  29. }
  30. /**
  31. * Returns arguments as valid php types.
  32. *
  33. * @param string $name
  34. * @param Boolean $lowercase
  35. *
  36. * @return mixed
  37. */
  38. public function getArgumentsAsPhp($name, $lowercase = true)
  39. {
  40. $arguments = array();
  41. foreach ($this->$name as $arg) {
  42. if (isset($arg['name'])) {
  43. $arg['key'] = (string) $arg['name'];
  44. }
  45. $key = isset($arg['key']) ? (string) $arg['key'] : (!$arguments ? 0 : max(array_keys($arguments)) + 1);
  46. // parameter keys are case insensitive
  47. if ('parameter' == $name && $lowercase) {
  48. $key = strtolower($key);
  49. }
  50. // this is used by DefinitionDecorator to overwrite a specific
  51. // argument of the parent definition
  52. if (isset($arg['index'])) {
  53. $key = 'index_'.$arg['index'];
  54. }
  55. switch ($arg['type']) {
  56. case 'service':
  57. $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
  58. if (isset($arg['on-invalid']) && 'ignore' == $arg['on-invalid']) {
  59. $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
  60. } elseif (isset($arg['on-invalid']) && 'null' == $arg['on-invalid']) {
  61. $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
  62. }
  63. if (isset($arg['strict'])) {
  64. $strict = self::phpize($arg['strict']);
  65. } else {
  66. $strict = true;
  67. }
  68. $arguments[$key] = new Reference((string) $arg['id'], $invalidBehavior, $strict);
  69. break;
  70. case 'collection':
  71. $arguments[$key] = $arg->getArgumentsAsPhp($name, false);
  72. break;
  73. case 'string':
  74. $arguments[$key] = (string) $arg;
  75. break;
  76. case 'constant':
  77. $arguments[$key] = constant((string) $arg);
  78. break;
  79. default:
  80. $arguments[$key] = self::phpize($arg);
  81. }
  82. }
  83. return $arguments;
  84. }
  85. /**
  86. * Converts an xml value to a php type.
  87. *
  88. * @param mixed $value
  89. *
  90. * @return mixed
  91. */
  92. public static function phpize($value)
  93. {
  94. return XmlUtils::phpize($value);
  95. }
  96. }