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

121 lines
3.7 KiB
PHP
Raw Normal View History

2010-01-04 14:26:20 +00:00
<?php
/*
* This file is part of the Symfony package.
2010-01-04 14:26:20 +00:00
*
* (c) Fabien Potencier <fabien@symfony.com>
2010-01-04 14:26:20 +00:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2010-01-04 14:26:20 +00:00
*/
namespace Symfony\Component\DependencyInjection;
/**
* SimpleXMLElement class.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
2010-01-04 14:26:20 +00:00
class SimpleXMLElement extends \SimpleXMLElement
{
2011-02-13 18:06:41 +00:00
/**
* Converts an attribute as a php type.
*
* @param string $name
2011-02-13 18:06:41 +00:00
* @return mixed
*/
public function getAttributeAsPhp($name)
2010-01-04 14:26:20 +00:00
{
return self::phpize($this[$name]);
}
2010-01-04 14:26:20 +00:00
2011-02-13 18:06:41 +00:00
/**
* Returns arguments as valid php types.
*
* @param string $name
2011-02-13 18:06:41 +00:00
* @return mixed
*/
public function getArgumentsAsPhp($name, $lowercase = true)
{
$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);
2010-01-04 14:26:20 +00:00
// parameter keys are case insensitive
2011-03-16 13:45:34 +00:00
if ('parameter' == $name && $lowercase) {
$key = strtolower($key);
}
2010-01-04 14:26:20 +00:00
2011-01-26 23:14:31 +00:00
// 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':
2010-06-27 17:28:29 +01:00
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
if (isset($arg['on-invalid']) && 'ignore' == $arg['on-invalid']) {
2010-06-27 17:28:29 +01:00
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
} elseif (isset($arg['on-invalid']) && 'null' == $arg['on-invalid']) {
2010-06-27 17:28:29 +01:00
$invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
}
2011-01-17 22:28:59 +00:00
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, false);
break;
case 'string':
$arguments[$key] = (string) $arg;
break;
case 'constant':
$arguments[$key] = constant((string) $arg);
break;
default:
$arguments[$key] = self::phpize($arg);
}
}
2010-01-04 14:26:20 +00:00
return $arguments;
}
2010-01-04 14:26:20 +00:00
2011-02-13 18:06:41 +00:00
/**
* Converts an xml value to a php type.
*
* @param mixed $value
2011-02-13 18:06:41 +00:00
* @return mixed
*/
static public function phpize($value)
2010-01-04 14:26:20 +00:00
{
$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;
}
2010-01-04 14:26:20 +00:00
}
}