/** * test setting unknown type * expected new type */ public function testBinarySetTypeFailed() { try { $value = new Zend_Measure_Binary('-100', Zend_Measure_Binary::STANDARD, 'de'); $value->setType('Binary::UNKNOWN'); $this->fail('Exception expected because of unknown type'); } catch (Zend_Measure_Exception $e) { // success } }
/** * Formats filesize with specified precision * * @param integer $fileSize Filesize in bytes * @param integer $precision Precision * @param string $norm Which norm use - 'traditional' (1 KB = 2^10 B), 'si' (1 KB = 10^3 B), 'iec' (1 KiB = 2^10 B) * @param string $type Defined export type */ public function fileSize($fileSize, $precision = 0, $norm = 'traditional', $type = null) { $m = new Zend_Measure_Binary($fileSize); $m->setType('BYTE'); if (null === $norm) { $norm = 'traditional'; } if (isset($type)) { $m->setType($type); } elseif ($norm === 'traditional') { if ($fileSize >= $this->_getUnitSize('TERABYTE')) { $m->setType(Zend_Measure_Binary::TERABYTE); } else { if ($fileSize >= $this->_getUnitSize('GIGABYTE')) { $m->setType(Zend_Measure_Binary::GIGABYTE); } else { if ($fileSize >= $this->_getUnitSize('MEGABYTE')) { $m->setType(Zend_Measure_Binary::MEGABYTE); } else { if ($fileSize >= $this->_getUnitSize('KILOBYTE')) { $m->setType(Zend_Measure_Binary::KILOBYTE); } } } } } elseif ($norm === 'si') { if ($fileSize >= $this->_getUnitSize('TERABYTE_SI')) { $m->setType(Zend_Measure_Binary::TERABYTE_SI); } else { if ($fileSize >= $this->_getUnitSize('GIGABYTE_SI')) { $m->setType(Zend_Measure_Binary::GIGABYTE_SI); } else { if ($fileSize >= $this->_getUnitSize('MEGABYTE_SI')) { $m->setType(Zend_Measure_Binary::MEGABYTE_SI); } else { if ($fileSize >= $this->_getUnitSize('KILOBYTE_SI')) { $m->setType(Zend_Measure_Binary::KILOBYTE_SI); } } } } } elseif ($norm === 'iec') { if ($fileSize >= $this->_getUnitSize('TEBIBYTE')) { $m->setType(Zend_Measure_Binary::TEBIBYTE); } else { if ($fileSize >= $this->_getUnitSize('GIBIBYTE')) { $m->setType(Zend_Measure_Binary::GIBIBYTE); } else { if ($fileSize >= $this->_getUnitSize('MEBIBYTE')) { $m->setType(Zend_Measure_Binary::MEBIBYTE); } else { if ($fileSize >= $this->_getUnitSize('KIBIBYTE')) { $m->setType(Zend_Measure_Binary::KIBIBYTE); } } } } } $value = $m->getValue($precision); $value = $this->_round($value, $precision); return $value . ' ' . $this->_getUnitAbr($m->getType()); }
/** * Formats filesize with specified precision * * @param integer $fileSize Filesize in bytes * @param integer $precision Precision * @param string $norm Which norm use - 'traditional' (1 KB = 2^10 B), 'si' (1 KB = 10^3 B), 'iec' (1 KiB = 2^10 B) * @param string $type Defined export type */ public function fileSize($fileSize, $precision = 0, $norm = 'traditional', $type = null) { try { $locale = Zend_Registry::get('Zend_Locale'); if (!$locale instanceof Zend_Locale) { throw new Zend_Exception('Locale is not set correctly.'); } $isLocaleSet = true; } catch (Zend_Exception $e) { $isLocaleSet = false; $locale = null; } $fileSize = floatval($fileSize); $m = new Zend_Measure_Binary($fileSize, null, $locale); //$m->setType('BYTE'); if (null === $norm) { $norm = 'traditional'; } if (isset($type)) { $m->setType($type); } elseif ($norm === 'traditional') { if ($fileSize >= $this->_getUnitSize('TERABYTE')) { $m->setType(Zend_Measure_Binary::TERABYTE); } else { if ($fileSize >= $this->_getUnitSize('GIGABYTE')) { $m->setType(Zend_Measure_Binary::GIGABYTE); } else { if ($fileSize >= $this->_getUnitSize('MEGABYTE')) { $m->setType(Zend_Measure_Binary::MEGABYTE); } else { if ($fileSize >= $this->_getUnitSize('KILOBYTE')) { $m->setType(Zend_Measure_Binary::KILOBYTE); } } } } } elseif ($norm === 'si') { if ($fileSize >= $this->_getUnitSize('TERABYTE_SI')) { $m->setType(Zend_Measure_Binary::TERABYTE_SI); } else { if ($fileSize >= $this->_getUnitSize('GIGABYTE_SI')) { $m->setType(Zend_Measure_Binary::GIGABYTE_SI); } else { if ($fileSize >= $this->_getUnitSize('MEGABYTE_SI')) { $m->setType(Zend_Measure_Binary::MEGABYTE_SI); } else { if ($fileSize >= $this->_getUnitSize('KILOBYTE_SI')) { $m->setType(Zend_Measure_Binary::KILOBYTE_SI); } } } } } elseif ($norm === 'iec') { if ($fileSize >= $this->_getUnitSize('TEBIBYTE')) { $m->setType(Zend_Measure_Binary::TEBIBYTE); } else { if ($fileSize >= $this->_getUnitSize('GIBIBYTE')) { $m->setType(Zend_Measure_Binary::GIBIBYTE); } else { if ($fileSize >= $this->_getUnitSize('MEBIBYTE')) { $m->setType(Zend_Measure_Binary::MEBIBYTE); } else { if ($fileSize >= $this->_getUnitSize('KIBIBYTE')) { $m->setType(Zend_Measure_Binary::KIBIBYTE); } } } } } return $m->toString($precision); }
/** * Formats filesize with specified precision * * @param integer $fileSize Filesize in bytes * @param integer $precision Precision * @param string $norm Which norm use - 'traditional' (1 KB = 2^10 B), 'si' (1 KB = 10^3 B), 'iec' (1 KiB = 2^10 B) * @param string $type Defined export type */ public function fileSize($fileSize, $precision = 0, $norm = 'traditional', $type = null) { try { require_once 'Zend/Registry.php'; $locale = Zend_Registry::get('Zend_Locale'); require_once 'Zend/Locale.php'; if (!$locale instanceof Zend_Locale) { require_once 'Zend/Exception.php'; throw new Zend_Exception('Locale is not set correctly.'); } $isLocaleSet = true; } catch (Zend_Exception $e) { $isLocaleSet = false; $locale = null; } if ($isLocaleSet) { /** * @see Zend_Locale_Math */ require_once 'Zend/Locale/Format.php'; //get localised input value $fileSize = Zend_Locale_Format::getFloat($fileSize, array('locale' => $locale)); } else { $fileSize = floatval($fileSize); } $m = new Zend_Measure_Binary($fileSize, null, $locale); $m->setType('BYTE'); if (null === $norm) { $norm = 'traditional'; } if (isset($type)) { $m->setType($type); } elseif ($norm === 'traditional') { if ($fileSize >= $this->_getUnitSize('TERABYTE')) { $m->setType(Zend_Measure_Binary::TERABYTE); } else { if ($fileSize >= $this->_getUnitSize('GIGABYTE')) { $m->setType(Zend_Measure_Binary::GIGABYTE); } else { if ($fileSize >= $this->_getUnitSize('MEGABYTE')) { $m->setType(Zend_Measure_Binary::MEGABYTE); } else { if ($fileSize >= $this->_getUnitSize('KILOBYTE')) { $m->setType(Zend_Measure_Binary::KILOBYTE); } } } } } elseif ($norm === 'si') { if ($fileSize >= $this->_getUnitSize('TERABYTE_SI')) { $m->setType(Zend_Measure_Binary::TERABYTE_SI); } else { if ($fileSize >= $this->_getUnitSize('GIGABYTE_SI')) { $m->setType(Zend_Measure_Binary::GIGABYTE_SI); } else { if ($fileSize >= $this->_getUnitSize('MEGABYTE_SI')) { $m->setType(Zend_Measure_Binary::MEGABYTE_SI); } else { if ($fileSize >= $this->_getUnitSize('KILOBYTE_SI')) { $m->setType(Zend_Measure_Binary::KILOBYTE_SI); } } } } } elseif ($norm === 'iec') { if ($fileSize >= $this->_getUnitSize('TEBIBYTE')) { $m->setType(Zend_Measure_Binary::TEBIBYTE); } else { if ($fileSize >= $this->_getUnitSize('GIBIBYTE')) { $m->setType(Zend_Measure_Binary::GIBIBYTE); } else { if ($fileSize >= $this->_getUnitSize('MEBIBYTE')) { $m->setType(Zend_Measure_Binary::MEBIBYTE); } else { if ($fileSize >= $this->_getUnitSize('KIBIBYTE')) { $m->setType(Zend_Measure_Binary::KIBIBYTE); } } } } } return $m->toString($precision); }
/** * test setting unknown type * expected new type */ public function testBinarySetTypeFailed() { try { $value = new Zend_Measure_Binary('-100', Zend_Measure_Binary::STANDARD, 'de'); $value->setType('Binary::UNKNOWN'); $this->assertTrue(false, 'Exception expected because of unknown type'); } catch (Exception $e) { return true; // OK } }