Example #1
0
 /**
  * test getConversionList
  * expected array
  */
 public function testBinaryConversionList()
 {
     $value = new Zend_Measure_Binary('-100', Zend_Measure_Binary::STANDARD, 'de');
     $unit = $value->getConversionList();
     $this->assertTrue(is_array($unit), 'Array expected');
 }
 /**
  * The procedural part that gets executes an API call and loops through the
  * results, processing them and saving them to the database table.
  *
  * @return void
  */
 public function processResults($options, $showProgress = true, $showMemory = false)
 {
     $this->_showProgress = $showProgress;
     $this->_showMemory = $showMemory;
     /**
      * Perform the actual API call
      */
     $results = $this->_doApiCall($options);
     //var_dump($results);
     //var_dump($this->_perPage);
     //var_dump($this);
     //var_dump($results->totalPages());
     $this->totalPages = $results->totalPages();
     //var_dump($this->totalPages);
     if ($this->_showMemory) {
         require_once 'Zend/Measure/Binary.php';
         $curMem = memory_get_usage(true);
         $curMem = new Zend_Measure_Binary(memory_get_usage(true), Zend_Measure_Binary::BYTE);
         echo '<h1>Current memory usage after fetching page ' . $options['page'] . ' of ' . $this->totalPages . ': ' . $curMem->convertTo(Zend_Measure_Binary::MEGABYTE) . '</h1>' . PHP_EOL;
     }
     foreach ($results as $result) {
         $this->_setRow($result->id);
         $this->_formatData($result);
         $this->_preSave($result);
         $this->_saveRow($result);
         $this->_postSave($result);
         $this->_rowAction = null;
         $this->_tableRow = null;
     }
     if ($this->_showMemory) {
         $curMem = new Zend_Measure_Binary(memory_get_usage(true), Zend_Measure_Binary::BYTE);
         echo '<h1>Current memory usage after database work of page ' . $options['page'] . ' of ' . $this->totalPages . ': ' . $curMem->convertTo(Zend_Measure_Binary::MEGABYTE) . '</h1>' . PHP_EOL;
     }
     if ($this->_showProgress) {
         echo '<h1>Done with page ' . $options['page'] . '</h1>' . PHP_EOL;
         @ob_end_flush();
         @ob_flush();
         @flush();
     }
 }
Example #3
0
 /**
  * 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());
 }
 /**
  * Show stats
  *
  * @param array $stats
  * @param OutputInterface $output
  * @return void
  */
 protected function _showStats(&$stats, OutputInterface $output)
 {
     $countBefore = $stats['count']['before'];
     $countAfter = $stats['count']['after'];
     $countPercentage = $stats['count']['percent'];
     $sizeBefore = $stats['size']['before'];
     $sizeAfter = $stats['size']['after'];
     $sizePercentage = $stats['size']['percent'];
     if ($countBefore <= $countAfter) {
         $output->writeln('<info>No files to remove</info> <comment>YOUR MEDIA IS OPTIMIZED AS HELL!</comment>');
         return;
     }
     $measureBefore = new \Zend_Measure_Binary($sizeBefore);
     $measureAfter = new \Zend_Measure_Binary($sizeAfter);
     $formattedBefore = $measureBefore->convertTo(\Zend_Measure_Binary::MEGABYTE);
     $formattedAfter = $measureAfter->convertTo(\Zend_Measure_Binary::MEGABYTE);
     $pad1Length = max(strlen($countBefore), strlen($formattedBefore));
     $pad2Length = max(strlen($countAfter), strlen($formattedAfter));
     $output->writeln('<info>Statistics: (before -> after)</info>');
     $output->writeln(' <comment>files:</comment> ' . str_pad($countBefore, $pad1Length, ' ', STR_PAD_LEFT) . ' -> ' . str_pad($countAfter, $pad2Length, ' ', STR_PAD_LEFT) . ' (' . round($countPercentage * 100, 1) . '%)');
     $output->writeln(' <comment>size:</comment>  ' . str_pad($formattedBefore, $pad1Length, ' ', STR_PAD_LEFT) . ' -> ' . str_pad($formattedAfter, $pad2Length, ' ', STR_PAD_LEFT) . ' (' . round($sizePercentage * 100, 1) . '%)');
     $output->writeln("\n");
 }
Example #5
0
 /**
  * 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);
 }
 /**
  * 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);
 }
Example #7
0
 /**
  * Compare if the value and type is equal
  *
  * @param  Zend_Measure_Binary  $object  Binary object to compare
  * @return boolean
  */
 public function equals($object)
 {
     if ($object->toString() == $this->toString()) {
         return true;
     }
     return false;
 }