PHP Classes

File: src/ProgressBar.php

Recommend this page to a friend!
  Classes of Joseluis Laso   PHP Preemptive Cache   src/ProgressBar.php   Download  
File: src/ProgressBar.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Preemptive Cache
Cache a limited amount of data records in memory
Author: By
Last change:
Date: 8 years ago
Size: 1,560 bytes
 

Contents

Class file image Download
<?php

namespace JLaso\ToolsLib;

class
ProgressBar
{
   
/** @var int */
   
protected $value;
   
/** @var int */
   
protected $oldValue;
   
/** @var int */
   
protected $maxValue;
   
/** @var int */
   
protected $size;
   
/** @var int */
   
protected $percEmptySize;
   
/** @var int */
   
protected $slice;

    const
MASC_PERCENT = "%3d%% ";

   
/**
     * ProgressBar constructor.
     */
   
public function __construct($maxValue, $size = 50)
    {
       
$this->maxValue = $maxValue;
       
$this->value = 0;
       
$this->oldValue = 0;
       
$this->size = $size;
       
$percEmpty = sprintf(self::MASC_PERCENT, 0);
       
$this->percEmptySize = strlen($percEmpty);
       
$this->slice = intval($maxValue/$size);

        print (
$percEmpty . str_repeat('ยท', $this->size) . str_repeat(chr(8), $this->percEmptySize + $this->size));
    }

   
/**
     * @param $value
     */
   
public function updateValue($value)
    {
       
$this->value = intval(ceil(100 * $value / $this->maxValue));
        if (
$this->value != $this->oldValue) {
           
$this->draw();
           
$this->oldValue = $this->value;
        }
    }

    protected function
draw()
    {
       
$mask = array('*','o','-',"\\",'|','/');
       
$this->slice;
       
$pperc = intval($this->value * $this->size / 100);
        print (
           
sprintf(self::MASC_PERCENT, $this->value) .
           
str_repeat("*", $pperc) . $mask[($this->value % $this->slice) % 6] .
           
str_repeat(chr(8), $this->percEmptySize + $pperc + 1)
        );
    }

}