public static function create($fieldName, $_files, $uploadDir) { if (count($_files) < 1) { throw new NoFileUploadedException('Emtpy file'); } // if the file field has a name with [] the file(s) will be // collected to an array, eg. ['name' => [0 => 'IMG_29042016_134422.png'] } // but if not, the keys value is a flat array, so it needs to be normalized if (strpos($fieldName, '[]') != strlen($fieldName) - 2) { $_files = array_map(function ($file) { return array_map(function ($value) { return [$value]; }, $file); }, $_files); } // $fieldName includes a '[]' when the upload is multiple $fieldName = str_replace(['[', ']'], '', $fieldName); if (!array_key_exists($fieldName, $_files)) { throw FormFieldException::noSuchFieldName($fieldName); } $requestedFileField = $_files[$fieldName]; // Count the uploaded files via one of it's child if (!array_key_exists('name', $requestedFileField)) { throw new NoNameParam('No name param found in file details'); } $uploadedCount = count($requestedFileField['name']); for ($index = 0; $index <= $uploadedCount - 1; $index++) { (yield new self($fieldName, $index, $_files, $uploadDir)); } }
/** * Get a field * * @param $key * @throws FormFieldException * @return mixed */ public function getField($key) { if (!in_array($key, array_keys($this->formFields))) { throw FormFieldException::noSuchFieldName($key); } return $this->formFields[$key]; }