From 1bbf00e0b86a22eeec58e6fc5961ed0f51f05fe2 Mon Sep 17 00:00:00 2001
From: Cristiano Magro <cristiano.magro@vola.it>
Date: Tue, 16 Jan 2018 15:48:57 +0100
Subject: [PATCH] Refactor inizio a sistemare la libreria in stile PSR-0

---
 src/UploadFile/SetupUpload.class.php |   18 ++++++++++++++++--
 1 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/src/UploadFile/SetupUpload.class.php b/src/UploadFile/SetupUpload.class.php
index 8bd941a..fed2bed 100644
--- a/src/UploadFile/SetupUpload.class.php
+++ b/src/UploadFile/SetupUpload.class.php
@@ -200,15 +200,29 @@
      * 1 Mb = 1024^2
      * 
      * @param string $strSize Stringa da convertire
+     * @throws UploadFile_Excs
      * @return integer
      */
     public static function str2Bytes($strSize) {
+        $Units = array('b' => 0, 'kb' => 1, 'mb' => 2, 'gb' => 3, 'tb' => 4);
+
         $strUnit = strtolower($strSize);
         $strUnit = preg_replace('/[^a-z]/', '', $strUnit);
+        $strValue = preg_replace('/[^0-9]/', '', $strSize);
+        $value = intval($strValue);
 
-        $value = intval(preg_replace('/[^0-9]/', '', $strSize));
+        //validazione
+        if (strlen(trim($strSize)) === 0) {
+            //una stringa vuota non può essere convertita
+            throw new UploadFile_Exc();
+        } elseif (strlen(trim($strValue)) === 0) {
+            //se non trovo la parte del valore della stringa non posso convertire
+            throw new UploadFile_Exc();
+        } elseif (strlen($strUnit) >= 1 && !array_key_exists($strUnit, $Units)) {
+            //l'unita' non e' nota
+            throw new UploadFile_Exc();
+        }
 
-        $Units = array('b' => 0, 'kb' => 1, 'mb' => 2, 'gb' => 3, 'tb' => 4);
         $Exponent = isset($Units[$strUnit]) ? $Units[$strUnit] : 0;
 
         return ($value * pow(1024, $Exponent));

--
Gitblit v1.8.0