[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/laminas/laminas-diactoros/src/functions/ -> normalize_uploaded_files.php (source)

   1  <?php
   2  
   3  /**
   4   * @see       https://github.com/laminas/laminas-diactoros for the canonical source repository
   5   * @copyright https://github.com/laminas/laminas-diactoros/blob/master/COPYRIGHT.md
   6   * @license   https://github.com/laminas/laminas-diactoros/blob/master/LICENSE.md New BSD License
   7   */
   8  
   9  declare(strict_types=1);
  10  
  11  namespace Laminas\Diactoros;
  12  
  13  use Psr\Http\Message\UploadedFileInterface;
  14  
  15  use function is_array;
  16  
  17  /**
  18   * Normalize uploaded files
  19   *
  20   * Transforms each value into an UploadedFile instance, and ensures that nested
  21   * arrays are normalized.
  22   *
  23   * @return UploadedFileInterface[]
  24   * @throws Exception\InvalidArgumentException for unrecognized values
  25   */
  26  function normalizeUploadedFiles(array $files) : array
  27  {
  28      /**
  29       * Traverse a nested tree of uploaded file specifications.
  30       *
  31       * @param string[]|array[] $tmpNameTree
  32       * @param int[]|array[] $sizeTree
  33       * @param int[]|array[] $errorTree
  34       * @param string[]|array[]|null $nameTree
  35       * @param string[]|array[]|null $typeTree
  36       * @return UploadedFile[]|array[]
  37       */
  38      $recursiveNormalize = function (
  39          array $tmpNameTree,
  40          array $sizeTree,
  41          array $errorTree,
  42          array $nameTree = null,
  43          array $typeTree = null
  44      ) use (&$recursiveNormalize) : array {
  45          $normalized = [];
  46          foreach ($tmpNameTree as $key => $value) {
  47              if (is_array($value)) {
  48                  // Traverse
  49                  $normalized[$key] = $recursiveNormalize(
  50                      $tmpNameTree[$key],
  51                      $sizeTree[$key],
  52                      $errorTree[$key],
  53                      $nameTree[$key] ?? null,
  54                      $typeTree[$key] ?? null
  55                  );
  56                  continue;
  57              }
  58              $normalized[$key] = createUploadedFile([
  59                  'tmp_name' => $tmpNameTree[$key],
  60                  'size' => $sizeTree[$key],
  61                  'error' => $errorTree[$key],
  62                  'name' => $nameTree[$key] ?? null,
  63                  'type' => $typeTree[$key] ?? null,
  64              ]);
  65          }
  66          return $normalized;
  67      };
  68  
  69      /**
  70       * Normalize an array of file specifications.
  71       *
  72       * Loops through all nested files (as determined by receiving an array to the
  73       * `tmp_name` key of a `$_FILES` specification) and returns a normalized array
  74       * of UploadedFile instances.
  75       *
  76       * This function normalizes a `$_FILES` array representing a nested set of
  77       * uploaded files as produced by the php-fpm SAPI, CGI SAPI, or mod_php
  78       * SAPI.
  79       *
  80       * @param array $files
  81       * @return UploadedFile[]
  82       */
  83      $normalizeUploadedFileSpecification = function (array $files = []) use (&$recursiveNormalize) : array {
  84          if (! isset($files['tmp_name']) || ! is_array($files['tmp_name'])
  85              || ! isset($files['size']) || ! is_array($files['size'])
  86              || ! isset($files['error']) || ! is_array($files['error'])
  87          ) {
  88              throw new Exception\InvalidArgumentException(sprintf(
  89                  '$files provided to %s MUST contain each of the keys "tmp_name",'
  90                  . ' "size", and "error", with each represented as an array;'
  91                  . ' one or more were missing or non-array values',
  92                  __FUNCTION__
  93              ));
  94          }
  95  
  96          return $recursiveNormalize(
  97              $files['tmp_name'],
  98              $files['size'],
  99              $files['error'],
 100              $files['name'] ?? null,
 101              $files['type'] ?? null
 102          );
 103      };
 104  
 105      $normalized = [];
 106      foreach ($files as $key => $value) {
 107          if ($value instanceof UploadedFileInterface) {
 108              $normalized[$key] = $value;
 109              continue;
 110          }
 111  
 112          if (is_array($value) && isset($value['tmp_name']) && is_array($value['tmp_name'])) {
 113              $normalized[$key] = $normalizeUploadedFileSpecification($value);
 114              continue;
 115          }
 116  
 117          if (is_array($value) && isset($value['tmp_name'])) {
 118              $normalized[$key] = createUploadedFile($value);
 119              continue;
 120          }
 121  
 122          if (is_array($value)) {
 123              $normalized[$key] = normalizeUploadedFiles($value);
 124              continue;
 125          }
 126  
 127          throw new Exception\InvalidArgumentException('Invalid value in files specification');
 128      }
 129      return $normalized;
 130  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer