vendor/symfony/src/Symfony/Component/DependencyInjection/SimpleXMLElement.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Component\DependencyInjection;
       
    13 
       
    14 /**
       
    15  * SimpleXMLElement class.
       
    16  *
       
    17  * @author Fabien Potencier <fabien@symfony.com>
       
    18  */
       
    19 class SimpleXMLElement extends \SimpleXMLElement
       
    20 {
       
    21     /**
       
    22      * Converts an attribute as a php type.
       
    23      *
       
    24      * @param string $name
       
    25      * @return mixed
       
    26      */
       
    27     public function getAttributeAsPhp($name)
       
    28     {
       
    29         return self::phpize($this[$name]);
       
    30     }
       
    31 
       
    32     /**
       
    33      * Returns arguments as valid php types.
       
    34      *
       
    35      * @param string  $name
       
    36      * @param Boolean $lowercase
       
    37      *
       
    38      * @return mixed
       
    39      */
       
    40     public function getArgumentsAsPhp($name, $lowercase = true)
       
    41     {
       
    42         $arguments = array();
       
    43         foreach ($this->$name as $arg) {
       
    44             if (isset($arg['name'])) {
       
    45                 $arg['key'] = (string) $arg['name'];
       
    46             }
       
    47             $key = isset($arg['key']) ? (string) $arg['key'] : (!$arguments ? 0 : max(array_keys($arguments)) + 1);
       
    48 
       
    49             // parameter keys are case insensitive
       
    50             if ('parameter' == $name && $lowercase) {
       
    51                 $key = strtolower($key);
       
    52             }
       
    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 
       
    60             switch ($arg['type']) {
       
    61                 case 'service':
       
    62                     $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
       
    63                     if (isset($arg['on-invalid']) && 'ignore' == $arg['on-invalid']) {
       
    64                         $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
       
    65                     } elseif (isset($arg['on-invalid']) && 'null' == $arg['on-invalid']) {
       
    66                         $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
       
    67                     }
       
    68 
       
    69                     if (isset($arg['strict'])) {
       
    70                         $strict = self::phpize($arg['strict']);
       
    71                     } else {
       
    72                         $strict = true;
       
    73                     }
       
    74 
       
    75                     $arguments[$key] = new Reference((string) $arg['id'], $invalidBehavior, $strict);
       
    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 
       
    91         return $arguments;
       
    92     }
       
    93 
       
    94     /**
       
    95      * Converts an xml value to a php type.
       
    96      *
       
    97      * @param mixed $value
       
    98      * @return mixed
       
    99      */
       
   100     static public function phpize($value)
       
   101     {
       
   102         $value = (string) $value;
       
   103         $lowercaseValue = strtolower($value);
       
   104 
       
   105         switch (true) {
       
   106             case 'null' === $lowercaseValue:
       
   107                 return null;
       
   108             case ctype_digit($value):
       
   109                 return '0' == $value[0] ? octdec($value) : intval($value);
       
   110             case 'true' === $lowercaseValue:
       
   111                 return true;
       
   112             case 'false' === $lowercaseValue:
       
   113                 return false;
       
   114             case is_numeric($value):
       
   115                 return '0x' == $value[0].$value[1] ? hexdec($value) : floatval($value);
       
   116             case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $value):
       
   117                 return floatval(str_replace(',', '', $value));
       
   118             default:
       
   119                 return $value;
       
   120         }
       
   121     }
       
   122 }