Overview

Namespaces

  • Composer
    • Autoload
  • Guzzle
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
        • Header
      • QueryAggregator
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Mock
    • Stream
  • Mockery
    • Adapter
      • Phpunit
    • CountValidator
    • Exception
    • Generator
      • StringManipulation
        • Pass
    • Loader
    • Matcher
  • None
  • Omnipay
    • Common
      • Exception
      • Message
    • Dummy
      • Message
    • Fatzebra
      • Message
  • PHP
  • Symfony
    • Component
      • EventDispatcher
        • Debug
        • DependencyInjection
        • Tests
          • Debug
          • DependencyInjection
      • HttpFoundation
        • File
          • Exception
          • MimeType
        • Session
          • Attribute
          • Flash
          • Storage
            • Handler
            • Proxy
        • Tests
          • File
            • MimeType
          • Session
            • Attribute
            • Flash
            • Storage
              • Handler
              • Proxy
      • Yaml
        • Exception
        • Tests

Classes

  • Symfony\Component\Yaml\Tests\A
  • Symfony\Component\Yaml\Tests\B
  • Symfony\Component\Yaml\Tests\DumperTest
  • Symfony\Component\Yaml\Tests\InlineTest
  • Symfony\Component\Yaml\Tests\ParseExceptionTest
  • Symfony\Component\Yaml\Tests\ParserTest
  • Symfony\Component\Yaml\Tests\YamlTest
  • Overview
  • Namespace
  • Function
  • Tree
  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\HttpFoundation;
 13: 
 14: use Symfony\Component\HttpFoundation\File\UploadedFile;
 15: 
 16: /**
 17:  * FileBag is a container for uploaded files.
 18:  *
 19:  * @author Fabien Potencier <fabien@symfony.com>
 20:  * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
 21:  *
 22:  * @api
 23:  */
 24: class FileBag extends ParameterBag
 25: {
 26:     private static $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type');
 27: 
 28:     /**
 29:      * Constructor.
 30:      *
 31:      * @param array $parameters An array of HTTP files
 32:      *
 33:      * @api
 34:      */
 35:     public function __construct(array $parameters = array())
 36:     {
 37:         $this->replace($parameters);
 38:     }
 39: 
 40:     /**
 41:      * {@inheritdoc}
 42:      *
 43:      * @api
 44:      */
 45:     public function replace(array $files = array())
 46:     {
 47:         $this->parameters = array();
 48:         $this->add($files);
 49:     }
 50: 
 51:     /**
 52:      * {@inheritdoc}
 53:      *
 54:      * @api
 55:      */
 56:     public function set($key, $value)
 57:     {
 58:         if (!is_array($value) && !$value instanceof UploadedFile) {
 59:             throw new \InvalidArgumentException('An uploaded file must be an array or an instance of UploadedFile.');
 60:         }
 61: 
 62:         parent::set($key, $this->convertFileInformation($value));
 63:     }
 64: 
 65:     /**
 66:      * {@inheritdoc}
 67:      *
 68:      * @api
 69:      */
 70:     public function add(array $files = array())
 71:     {
 72:         foreach ($files as $key => $file) {
 73:             $this->set($key, $file);
 74:         }
 75:     }
 76: 
 77:     /**
 78:      * Converts uploaded files to UploadedFile instances.
 79:      *
 80:      * @param array|UploadedFile $file A (multi-dimensional) array of uploaded file information
 81:      *
 82:      * @return array A (multi-dimensional) array of UploadedFile instances
 83:      */
 84:     protected function convertFileInformation($file)
 85:     {
 86:         if ($file instanceof UploadedFile) {
 87:             return $file;
 88:         }
 89: 
 90:         $file = $this->fixPhpFilesArray($file);
 91:         if (is_array($file)) {
 92:             $keys = array_keys($file);
 93:             sort($keys);
 94: 
 95:             if ($keys == self::$fileKeys) {
 96:                 if (UPLOAD_ERR_NO_FILE == $file['error']) {
 97:                     $file = null;
 98:                 } else {
 99:                     $file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error']);
100:                 }
101:             } else {
102:                 $file = array_map(array($this, 'convertFileInformation'), $file);
103:             }
104:         }
105: 
106:         return $file;
107:     }
108: 
109:     /**
110:      * Fixes a malformed PHP $_FILES array.
111:      *
112:      * PHP has a bug that the format of the $_FILES array differs, depending on
113:      * whether the uploaded file fields had normal field names or array-like
114:      * field names ("normal" vs. "parent[child]").
115:      *
116:      * This method fixes the array to look like the "normal" $_FILES array.
117:      *
118:      * It's safe to pass an already converted array, in which case this method
119:      * just returns the original array unmodified.
120:      *
121:      * @param array $data
122:      *
123:      * @return array
124:      */
125:     protected function fixPhpFilesArray($data)
126:     {
127:         if (!is_array($data)) {
128:             return $data;
129:         }
130: 
131:         $keys = array_keys($data);
132:         sort($keys);
133: 
134:         if (self::$fileKeys != $keys || !isset($data['name']) || !is_array($data['name'])) {
135:             return $data;
136:         }
137: 
138:         $files = $data;
139:         foreach (self::$fileKeys as $k) {
140:             unset($files[$k]);
141:         }
142: 
143:         foreach (array_keys($data['name']) as $key) {
144:             $files[$key] = $this->fixPhpFilesArray(array(
145:                 'error' => $data['error'][$key],
146:                 'name' => $data['name'][$key],
147:                 'type' => $data['type'][$key],
148:                 'tmp_name' => $data['tmp_name'][$key],
149:                 'size' => $data['size'][$key],
150:             ));
151:         }
152: 
153:         return $files;
154:     }
155: }
156: 
Omnipay Fat Zebra / Paystream Gateway Module API Documentation API documentation generated by ApiGen