19-05-2015, 04:51 PM
Si j'ai bien compris ton besoin, c'est genre ça que tu veux :
<?php
class FileAggregator implements ArrayIterator
{
private $files = [];
public function add(File $file, $lineNumber, FileUploader $fileUploader)
{
switch ($file->type) {
case 'pdf':
// Only one pdf allowed
// We erase all other files
if (isset($this->files[$lineNumber]['pdf']) === true) {
$this->files[$lineNumber] = ['pdf' => $this->files[$lineNumber]['pdf']];
$fileUploader->fileRejected($file, 'PDF already exist on this line');
} else {
$this->files[$lineNumber][$file->type] = $file;
}
break;
case 'dxf':
case 'dwg':
if (isset($this->files[$lineNumber][$file->type]) === false) {
$this->files[$lineNumber][$file->type] = $file;
}
break;
}
}
public function listValidFiles(FileUploader $fileUploader)
{
foreach ($this->files as $files) {
if (isset($files['pdf']) === true) {
foreach ($files as $type as $file) {
$fileUploader->upload($file);
}
}
}
}
}
class File
{
public $filename;
public $type;
}
class FileUploader
{
public function thereIsAFileToUpload($lineNumber, File $file, FileAggregator $fileAggregator)
{
$fileAggregator->add($file, $lineNumber, $this);
}
public function fileRejected(File $file, $message)
{
echo 'File rejected ' . $message;
}
public function uploadAllFiles()
{
$fileAggregator->listValidFiles($this);
}
public function upload(File $file)
{
// Upload stuff for $file
}
}