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)); } }
/** * Factory method to create an appropriate type * * @param FormField $field * @param $submittedData * @return SpecialEmailValidator * @throws FormFieldException */ public static function create(FormField $field, $submittedData) { switch ($field->getType()) { case 'email': $object = new SpecialEmailValidator($field, $submittedData); break; default: throw FormFieldException::noSuchType($field->getType()); break; } return $object; }
/** * Compact files from uploaded files array - usually $_FILES * * @param array $files * @param $uploadDir * @throws FormFieldException * @throws NoFileUploadedException * @throws NoNameParam * @internal param $ [type] $files [description] */ public function compactFiles(array $files, $uploadDir) { if ($this->type !== self::TYPE_FILE) { throw FormFieldException::notAFileUpload($this->tagName); } if (empty($files)) { throw new NoFileUploadedException("Files are missing from payload"); } foreach (UploadedFile::create($this->name, $files, $uploadDir) as $uploadedFile) { $this->files[] = $uploadedFile; } }
/** * 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]; }