Fixup conversione stringe to byte
- riconoscimento stringhe non convertibili
- sollevo eccezione per stringe non convertibili
- test di verifica
| | |
| | | * 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)); |
| | |
| | | $this->assertEquals('0', SetupUpload::str2Bytes('0 mB')); |
| | | $this->assertEquals('0', SetupUpload::str2Bytes('0 GB')); |
| | | $this->assertEquals('0', SetupUpload::str2Bytes('0 TB')); |
| | | $this->assertEquals('0', SetupUpload::str2Bytes('0 XB')); |
| | | } |
| | | |
| | | /** |
| | | * Conversione di una stringa nel corrispondente valore di byte, stringhe |
| | | * non convertibili devono andare in errore |
| | | * @since 0.3.1 |
| | | * @expectedException UploadFile_Exc |
| | | * @dataProvider getBytesStringNotValid |
| | | */ |
| | | public function test_str2BytesNonValido($byteString) { |
| | | SetupUpload::str2Bytes($byteString); |
| | | } |
| | | |
| | | public function getBytesStringNotValid() { |
| | | return array( |
| | | array(''), |
| | | array('b'), |
| | | array('1 k'), |
| | | array('1 a'), |
| | | ); |
| | | } |
| | | |
| | | /** |