SimpleXMLElement.php 3.5 KB

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