This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/SimpleXMLElement.php
Fabien Potencier a8d05746ff Merge remote branch 'schmittjoh/propertyAnnotations'
* schmittjoh/propertyAnnotations:
  [DependencyInjection] added some tests
  [DependencyInjection] adds property injection
2011-03-11 13:12:12 +01:00

121 lines
3.7 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection;
/**
* SimpleXMLElement class.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class SimpleXMLElement extends \SimpleXMLElement
{
/**
* Converts an attribute as a php type.
*
* @param string $name
* @return mixed
*/
public function getAttributeAsPhp($name)
{
return self::phpize($this[$name]);
}
/**
* Returns arguments as valid php types.
*
* @param string $name
* @return mixed
*/
public function getArgumentsAsPhp($name)
{
$arguments = array();
foreach ($this->$name as $arg) {
if (isset($arg['name'])) {
$arg['key'] = (string) $arg['name'];
}
$key = isset($arg['key']) ? (string) $arg['key'] : (!$arguments ? 0 : max(array_keys($arguments)) + 1);
// parameter keys are case insensitive
if ('parameter' == $name) {
$key = strtolower($key);
}
// this is used by DefinitionDecorator to overwrite a specific
// argument of the parent definition
if (isset($arg['index'])) {
$key = 'index_'.$arg['index'];
}
switch ($arg['type']) {
case 'service':
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
if (isset($arg['on-invalid']) && 'ignore' == $arg['on-invalid']) {
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
} elseif (isset($arg['on-invalid']) && 'null' == $arg['on-invalid']) {
$invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
}
if (isset($arg['strict'])) {
$strict = self::phpize($arg['strict']);
} else {
$strict = true;
}
$arguments[$key] = new Reference((string) $arg['id'], $invalidBehavior, $strict);
break;
case 'collection':
$arguments[$key] = $arg->getArgumentsAsPhp($name);
break;
case 'string':
$arguments[$key] = (string) $arg;
break;
case 'constant':
$arguments[$key] = constant((string) $arg);
break;
default:
$arguments[$key] = self::phpize($arg);
}
}
return $arguments;
}
/**
* Converts an xml value to a php type.
*
* @param mixed $value
* @return mixed
*/
static public function phpize($value)
{
$value = (string) $value;
$lowercaseValue = strtolower($value);
switch (true) {
case 'null' === $lowercaseValue:
return null;
case ctype_digit($value):
return '0' == $value[0] ? octdec($value) : intval($value);
case 'true' === $lowercaseValue:
return true;
case 'false' === $lowercaseValue:
return false;
case is_numeric($value):
return '0x' == $value[0].$value[1] ? hexdec($value) : floatval($value);
case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $value):
return floatval(str_replace(',', '', $value));
default:
return $value;
}
}
}