Skip to content

Commit

Permalink
Base class
Browse files Browse the repository at this point in the history
* Added a back class for Bootstrap 4 view helpers.
  • Loading branch information
deanblackborough committed Oct 11, 2017
1 parent bab8d24 commit dec3cbe
Show file tree
Hide file tree
Showing 3 changed files with 313 additions and 84 deletions.
105 changes: 105 additions & 0 deletions src/Bootstrap4Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

namespace DBlackborough\Zf3ViewHelpers;

use Zend\View\Helper\AbstractHelper;

/**
* Generate a Bootstrap 4 progress bar
*
* @package DBlackborough\Zf3ViewHelpers
* @author Dean Blackborough <dean@g3d-development.com>
* @copyright Dean Blackborough
* @license https://github.com/deanblackborough/zf3-view-helpers/blob/master/LICENSE
*/
abstract class Bootstrap4Helper extends AbstractHelper
{
/**
* @var array Bootstrap styles
*/
protected $supported_bg_styles = [
'primary',
'secondary',
'success',
'danger',
'warning',
'info',
'light',
'dark',
'white'
];

/**
* @var array Bootstrap styles
*/
protected $supported_text_styles = [
'primary',
'secondary',
'success',
'danger',
'warning',
'info',
'light',
'dark'
];

/**
* @var string Assign an alternate background colour for the component
*/
protected $bg_color;

/**
* @var string Assign an alternate text colour for the component
*/
protected $text_color;

/**
* Override the __toString() method to allow echo/print of the view helper directly,
* saves a call to render()
*
* @return string
*/
public function __toString() : string
{
return $this->render();
}

/**
* Validate and assign the background colour, needs to be one of the following, primary, secondary, success,
* danger, warning, info, light, dark or white, if an incorrect style is passed in we don't apply the class.
*
* @param string $color
*/
protected function assignBgStyle(string $color)
{
if (in_array($color, $this->supported_bg_styles) === true) {
$this->bg_color = $color;
}
}

/**
* Validate and assign the text colour, need to be one of the following, primary, secondary, success, danger,
* warning, info, light or dark, if an incorrect style is passed in we don't apply the class.
*
* @param string $color
*/
protected function assignTextStyle(string $color)
{
if (in_array($color, $this->supported_text_styles) === true) {
$this->text_color = $color;
}
}

/**
* Worker method for the view helper, generates the HTML, the method is protected, called by __toString when
* echo or print are called on the view helper
*
* @return string
*/
protected function render() : string
{
return $this->__toString();
}
}
152 changes: 68 additions & 84 deletions src/Bootstrap4ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

namespace DBlackborough\Zf3ViewHelpers;

use Zend\View\Helper\AbstractHelper;

/**
* Generate a Bootstrap 4 progress bar
*
Expand All @@ -13,18 +11,13 @@
* @copyright Dean Blackborough
* @license https://github.com/deanblackborough/zf3-view-helpers/blob/master/LICENSE
*/
class Bootstrap4ProgressBar extends AbstractHelper
class Bootstrap4ProgressBar extends Bootstrap4Helper
{
/**
* @var integer Current progress bar value
*/
private $value;

/**
* @var string Assigned an alternate background colour
*/
private $color;

/**
* @var boolean Enabled the striped style
*/
Expand All @@ -45,21 +38,6 @@ class Bootstrap4ProgressBar extends AbstractHelper
*/
private $height;

/**
* @var array Bootstrap styles
*/
private $supported_styles = [
'primary',
'secondary',
'success',
'danger',
'warning',
'info',
'light',
'dark',
'white'
];

/**
* Entry point for the view helper
*
Expand All @@ -77,107 +55,104 @@ public function __invoke(int $value): Bootstrap4ProgressBar
}

/**
* Set the background color for the progress bar, one of the following, primary, secondary, success, danger,
* warning, info, light, dark or white, if an incorrect style is passed in we don't apply the class.
*
* @param string $color
* Animate the striped background style
*
* @return Bootstrap4ProgressBar
*/
public function setBgStyle(string $color): Bootstrap4ProgressBar
public function animate() : Bootstrap4ProgressBar
{
if (in_array($color, $this->supported_styles) === true) {
$this->color = $color;
}
$this->animate = true;

return $this;
}

/**
* Enable the striped style for the progress bar background
* Set the height of the progress bar
*
* @param integer $height
*
* @return Bootstrap4ProgressBar
*/
public function striped() : Bootstrap4ProgressBar
public function height(int $height) : Bootstrap4ProgressBar
{
$this->striped = true;
$this->height = $height;

return $this;
}

/**
* Animate the striped background style
* Set the label for the progress bar
*
* @param string $label
*
* @return Bootstrap4ProgressBar
*/
public function animate() : Bootstrap4ProgressBar
public function label(string $label) : Bootstrap4ProgressBar
{
$this->animate = true;
$this->label = $label;

return $this;
}

/**
* Set the label for the progress bar
* Set the background colour for the component, needs to be one of the following, primary, secondary, success,
* danger, warning, info, light, dark or white, if an incorrect style is passed in we don't apply the class.
*
* @param string $label
* @param string $color
*
* @return Bootstrap4ProgressBar
*/
public function label(string $label) : Bootstrap4ProgressBar
public function setBgStyle(string $color) : Bootstrap4ProgressBar
{
$this->label = $label;
$this->assignBgStyle($color);

return $this;
}

/**
* Set the height of the progress bar
* Set the text color for the component, need to be one of the following, primary, secondary, success, danger,
* warning, info, light or dark, if an incorrect style is passed in we don't apply the class.
*
* @param integer $height
* @param string $color
*
* @return Bootstrap4ProgressBar
*/
public function height(int $height) : Bootstrap4ProgressBar
public function setTextStyle(string $color) : Bootstrap4ProgressBar
{
$this->height = $height;
$this->assignTextStyle($color);

return $this;
}

/**
* Reset all properties in case the view helper is called multiple times within a script
* Enable the striped style for the progress bar background
*
* @return void
* @return Bootstrap4ProgressBar
*/
private function reset(): void
public function striped() : Bootstrap4ProgressBar
{
$this->value = 0;
$this->color = null;
$this->striped = false;
$this->animate = false;
$this->label = null;
$this->height = null;
$this->striped = true;

return $this;
}

/**
* Generate the style attributes for the progress bar
* Worker method for the view helper, generates the HTML, the method is private so that we
* can echo/print the view helper directly
*
* @return string
*/
private function styles() : string
protected function render() : string
{
$styles = '';

if ($this->value > 0) {
$styles .= ' width: ' . $this->value . '%;';
}

if ($this->height !== null && $this->height > 0) {
$styles .= ' height: ' . $this->height . 'px;';
$styles = $this->styles();
if (strlen($styles) > 0) {
$styles = 'style="' . trim($styles) . '"';
}

return $styles;
return '<div class="progress"><div class="progress-bar' . $this->classes() .
'" role="progressbar" ' . $styles . ' aria-valuenow="' . $this->value .
'" aria-valuemin="0" aria-valuemax="100">' .
(($this->label !== null) ? $this->label : null) . '</div></div>';
}

/**
Expand All @@ -189,8 +164,12 @@ private function classes() : string
{
$classes = '';

if ($this->color !== null) {
$classes .= ' bg-' . $this->color;
if ($this->bg_color !== null) {
$classes .= ' bg-' . $this->bg_color;
}

if ($this->text_color !== null) {
$classes .= ' text-' . $this->text_color;
}

if ($this->striped === true) {
Expand All @@ -205,32 +184,37 @@ private function classes() : string
}

/**
* Worker method for the view helper, generates the HTML, the method is private so that we
* can echo/print the view helper directly
* Reset all properties in case the view helper is called multiple times within a script
*
* @return string
* @return void
*/
private function render() : string
private function reset(): void
{
$styles = $this->styles();
if (strlen($styles) > 0) {
$styles = 'style="' . trim($styles) . '"';
}

return '<div class="progress"><div class="progress-bar' . $this->classes() .
'" role="progressbar" ' . $styles . ' aria-valuenow="' . $this->value .
'" aria-valuemin="0" aria-valuemax="100">' .
(($this->label !== null) ? $this->label : null) . '</div></div>';
$this->value = 0;
$this->bg_color = null;
$this->striped = false;
$this->animate = false;
$this->label = null;
$this->height = null;
}

/**
* Override the __toString() method to allow echo/print of the view helper directly,
* saves a call to render()
* Generate the style attributes for the progress bar
*
* @return string
*/
public function __toString() : string
private function styles() : string
{
return $this->render();
$styles = '';

if ($this->value > 0) {
$styles .= ' width: ' . $this->value . '%;';
}

if ($this->height !== null && $this->height > 0) {
$styles .= ' height: ' . $this->height . 'px;';
}

return $styles;
}
}
Loading

0 comments on commit dec3cbe

Please sign in to comment.