Merge branch 'FilesFilter'
4 files added
2 files modified
| | |
| | | private static $classes = array( |
| | | 'FileUpload' => 'FileUpload.class.php', |
| | | 'SetupUpload' => 'SetupUpload.class.php', |
| | | 'FilesFilter' => 'FilesFilter.class.php', |
| | | 'FilterExt' => 'FilterExt.class.php', |
| | | 'Upload_Exc_Error' => 'Exc/Error.php', |
| | | 'Upload_Exc_Extension' => 'Exc/Extension.php', |
| | | 'Upload_Exc_ExtensionNotValid' => 'Exc/ExtensionNotValid.php', |
| New file |
| | |
| | | <?php |
| | | /** |
| | | * User: Cristiano Magro |
| | | * Date: 08/07/2019 |
| | | */ |
| | | |
| | | class FilesFilter |
| | | { |
| | | |
| | | private $filtro = array(); |
| | | |
| | | public function toJson() |
| | | { |
| | | |
| | | $temp = array(); |
| | | |
| | | /** @var FilterExt $filtro */ |
| | | foreach ($this->filtro as $filtro) { |
| | | $temp[] = $filtro->getFilterObj(); |
| | | } |
| | | |
| | | return json_encode($temp); |
| | | } |
| | | |
| | | public function addExt($extension = '') |
| | | { |
| | | $titolo = $this->decodeTitolo($extension); |
| | | if ($titolo != null) { |
| | | if ($this->filtro[$titolo] instanceof FilterExt) { |
| | | $this->addExtensionToFilter($titolo, $extension); |
| | | } else { |
| | | $this->createFilter($titolo, $extension); |
| | | } |
| | | } |
| | | return $this; |
| | | } |
| | | |
| | | /** |
| | | * Decodifico il titolo in base all'estensione. |
| | | * Il confronto viene eseguito in lowercase |
| | | * @param string $ext |
| | | * @return string |
| | | */ |
| | | public function decodeTitolo($ext) |
| | | { |
| | | switch (strtolower($ext)) { |
| | | case 'gif': |
| | | case 'jpg': |
| | | case 'jpeg': |
| | | case 'png': |
| | | case 'wmf': |
| | | case 'tif': |
| | | case 'tiff': |
| | | $titolo = "Image files"; |
| | | break; |
| | | case 'doc': |
| | | case 'txt': |
| | | case 'docx': |
| | | case 'docm': |
| | | case 'opsx': |
| | | case 'odt': |
| | | case 'rtf': |
| | | case 'pdf': |
| | | $titolo = "Documenti"; |
| | | break; |
| | | case 'mp3': |
| | | case 'wav': |
| | | $titolo = "Audio files"; |
| | | break; |
| | | case 'zip': |
| | | case 'rar': |
| | | case 'gz': |
| | | $titolo = "Zip files"; |
| | | break; |
| | | case 'xls': |
| | | case 'xlsx': |
| | | case 'eml': |
| | | case 'msg': |
| | | case 'xps': |
| | | case 'cvs': |
| | | $titolo = "Excel files"; |
| | | break; |
| | | } |
| | | |
| | | return $titolo; |
| | | } |
| | | |
| | | /** |
| | | * @param $titolo |
| | | * @param $extension |
| | | */ |
| | | private function createFilter($titolo, $extension) |
| | | { |
| | | $filtro = new FilterExt($titolo, $extension); |
| | | $this->filtro[$titolo] = $filtro; |
| | | } |
| | | |
| | | /** |
| | | * @param $titolo |
| | | * @param $extension |
| | | */ |
| | | private function addExtensionToFilter($titolo, $extension) |
| | | { |
| | | $this->filtro[$titolo]->addExt($extension); |
| | | } |
| | | |
| | | } |
| New file |
| | |
| | | <?php |
| | | /** |
| | | * Costruzione di un filtro |
| | | * User: Cristiano Magro |
| | | * Date: 09/07/2019 |
| | | */ |
| | | |
| | | class FilterExt |
| | | { |
| | | private $titolo; |
| | | private $extensions; |
| | | |
| | | public function __construct($titolo = null, $ext = null) |
| | | { |
| | | $this->titolo = $titolo; |
| | | $this->extensions[$ext] = $ext; |
| | | } |
| | | |
| | | /** |
| | | * Costruisco la struttura json del filtro. |
| | | * La lista viene collassata in una stringa |
| | | * @return string struttura json del filtro |
| | | */ |
| | | public function getFilter() |
| | | { |
| | | $new = new stdClass(); |
| | | $new->title = $this->titolo; |
| | | |
| | | //riordino le chiavi |
| | | sort($this->extensions); |
| | | |
| | | $new->extensions = implode(',', $this->extensions); |
| | | |
| | | return json_encode($new); |
| | | } |
| | | |
| | | /** |
| | | * Aggiungo alla lista una estensione. |
| | | * @param string $newExt |
| | | * @return FilterExt |
| | | */ |
| | | public function addExt($newExt) |
| | | { |
| | | $this->extensions[$newExt] = $newExt; |
| | | return $this; |
| | | } |
| | | |
| | | /** |
| | | * @return string Titolo del filtro |
| | | */ |
| | | public function getTitolo() |
| | | { |
| | | return $this->titolo; |
| | | } |
| | | |
| | | public function getFilterObj() |
| | | { |
| | | return json_decode($this->getFilter()); |
| | | } |
| | | } |
| | |
| | | * 1 Mb = 1024^2 |
| | | * |
| | | * @param string $strSize Stringa da convertire |
| | | * @throws UploadFile_Excs |
| | | * @throws UploadFile_Exc_Error |
| | | * @return integer |
| | | */ |
| | | public static function str2Bytes($strSize) |
| New file |
| | |
| | | <?php |
| | | /** |
| | | * User: Cristiano Magro |
| | | * Date: 08/07/2019 |
| | | */ |
| | | |
| | | class FilesFilterTest extends PHPUnit_Framework_TestCase |
| | | { |
| | | /** @var FilesFilter $object */ |
| | | protected $object; |
| | | |
| | | /** |
| | | * Set up fixture, inizializzazione dei test. |
| | | * Eseguito prima di ogni test |
| | | */ |
| | | protected function setUp() |
| | | { |
| | | $this->object = new FilesFilter(); |
| | | } |
| | | |
| | | /** |
| | | * Tears down the fixture, for example, closes a network connection. |
| | | * This method is called after a test is executed. |
| | | */ |
| | | protected function tearDown() |
| | | { |
| | | |
| | | } |
| | | |
| | | /** |
| | | * @dataProvider addExtension |
| | | */ |
| | | public function testAddExtension($lista, $atteso) |
| | | { |
| | | |
| | | foreach ($lista as $item) { |
| | | $this->object->addExt($item); |
| | | } |
| | | |
| | | $esito = $this->object->toJson(); |
| | | |
| | | $this->assertEquals($atteso, $esito); |
| | | } |
| | | |
| | | public function addExtension() |
| | | { |
| | | return [ |
| | | [[], '[]'], |
| | | [['fasullo'], '[]'], |
| | | [['gif'], '[{"title":"Image files","extensions":"gif"}]'], |
| | | [['doc'], '[{"title":"Documenti","extensions":"doc"}]'], |
| | | [['DOC'], '[{"title":"Documenti","extensions":"DOC"}]'], |
| | | [['txt'], '[{"title":"Documenti","extensions":"txt"}]'], |
| | | [['DOc', 'gif'], '[{"title":"Documenti","extensions":"DOc"},{"title":"Image files","extensions":"gif"}]'], |
| | | [['txt', 'doc'], '[{"title":"Documenti","extensions":"doc,txt"}]'], |
| | | [['gif', 'fasullo'], '[{"title":"Image files","extensions":"gif"}]'], |
| | | ]; |
| | | } |
| | | |
| | | |
| | | public function testDecodeTitolo() |
| | | { |
| | | $atteso = "Documenti"; |
| | | |
| | | $this->assertEquals($atteso, $this->object->decodeTitolo('Doc')); |
| | | $this->assertEquals($atteso, $this->object->decodeTitolo('doc')); |
| | | } |
| | | |
| | | } |
| New file |
| | |
| | | <?php |
| | | /** |
| | | * User: Cristiano Magro |
| | | * Date: 09/07/2019 |
| | | */ |
| | | |
| | | class FilterExtTest extends PHPUnit_Framework_TestCase |
| | | { |
| | | /** @var FilterExt $object */ |
| | | protected $object; |
| | | |
| | | /** |
| | | * Set up fixture, inizializzazione dei test. |
| | | * Eseguito prima di ogni test |
| | | */ |
| | | protected function setUp() |
| | | { |
| | | $this->object = new FilterExt(); |
| | | } |
| | | |
| | | /** |
| | | * Tears down the fixture, for example, closes a network connection. |
| | | * This method is called after a test is executed. |
| | | */ |
| | | protected function tearDown() |
| | | { |
| | | |
| | | } |
| | | |
| | | public function testCreateNewFilter() |
| | | { |
| | | $atteso = '{"title":"titolo","extensions":"ext"}'; |
| | | $obj = new FilterExt('titolo', 'ext'); |
| | | |
| | | $this->assertEquals($atteso, $obj->getFilter()); |
| | | } |
| | | |
| | | public function testAddExt() |
| | | { |
| | | $atteso = '{"title":"titolo","extensions":"ciccio,ext"}'; |
| | | |
| | | $obj = new FilterExt('titolo', 'ext'); |
| | | $obj->addExt('ciccio'); |
| | | |
| | | $this->assertEquals($atteso, $obj->getFilter()); |
| | | } |
| | | |
| | | public function testDuplicatoExt() |
| | | { |
| | | $atteso = '{"title":"titolo","extensions":"ext"}'; |
| | | |
| | | $obj = new FilterExt('titolo', 'ext'); |
| | | $obj->addExt('ext'); |
| | | |
| | | $this->assertEquals($atteso, $obj->getFilter()); |
| | | } |
| | | |
| | | /** |
| | | * Le estensioni possono differire tra maiuscolo e minuscolo. |
| | | */ |
| | | public function testDuplicatoUpper() |
| | | { |
| | | $atteso = '{"title":"qualcosa","extensions":"Ext,ext"}'; |
| | | |
| | | $obj = new FilterExt('qualcosa', 'ext'); |
| | | $obj->addExt('Ext'); |
| | | |
| | | $this->assertEquals($atteso, $obj->getFilter()); |
| | | } |
| | | |
| | | } |