From dec3cbe541c5c91d848aa48e05e68804d804967e Mon Sep 17 00:00:00 2001 From: Dean Blackborough Date: Wed, 11 Oct 2017 12:56:36 +0100 Subject: [PATCH 1/2] Base class * Added a back class for Bootstrap 4 view helpers. --- src/Bootstrap4Helper.php | 105 ++++++++++++++++++++++ src/Bootstrap4ProgressBar.php | 152 ++++++++++++++------------------ tests/Bootstrap4ProgressBar.php | 140 +++++++++++++++++++++++++++++ 3 files changed, 313 insertions(+), 84 deletions(-) create mode 100644 src/Bootstrap4Helper.php diff --git a/src/Bootstrap4Helper.php b/src/Bootstrap4Helper.php new file mode 100644 index 0000000..6e209c4 --- /dev/null +++ b/src/Bootstrap4Helper.php @@ -0,0 +1,105 @@ + + * @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(); + } +} diff --git a/src/Bootstrap4ProgressBar.php b/src/Bootstrap4ProgressBar.php index 171e246..9e595e1 100644 --- a/src/Bootstrap4ProgressBar.php +++ b/src/Bootstrap4ProgressBar.php @@ -3,8 +3,6 @@ namespace DBlackborough\Zf3ViewHelpers; -use Zend\View\Helper\AbstractHelper; - /** * Generate a Bootstrap 4 progress bar * @@ -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 */ @@ -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 * @@ -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 '
' . + (($this->label !== null) ? $this->label : null) . '
'; } /** @@ -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) { @@ -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 '
' . - (($this->label !== null) ? $this->label : null) . '
'; + $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; } } diff --git a/tests/Bootstrap4ProgressBar.php b/tests/Bootstrap4ProgressBar.php index 72f0f09..c635932 100644 --- a/tests/Bootstrap4ProgressBar.php +++ b/tests/Bootstrap4ProgressBar.php @@ -213,6 +213,20 @@ public function testBgStyleWhite() ); } + /** + * Set background style, invalid + */ + public function testBgStyleInvalid() + { + $view_helper = new Bootstrap4ProgressBar(); + $view_helper->__invoke(25) + ->setBgStyle('invalid'); + $this->assertEquals( + '
', + $view_helper->__toString() + ); + } + /** * Set the striped option */ @@ -241,4 +255,130 @@ public function testStripedAndAnimated() $view_helper->__toString() ); } + + /** + * Set text style, primary + */ + public function testTextStylePrimary() + { + $view_helper = new Bootstrap4ProgressBar(); + $view_helper->__invoke(25) + ->setBgStyle('primary'); + $this->assertEquals( + '
', + $view_helper->__toString() + ); + } + + /** + * Set text style, secondary + */ + public function testTextStyleSecondary() + { + $view_helper = new Bootstrap4ProgressBar(); + $view_helper->__invoke(25) + ->setBgStyle('secondary'); + $this->assertEquals( + '
', + $view_helper->__toString() + ); + } + + /** + * Set text style, success + */ + public function testTextStyleSuccess() + { + $view_helper = new Bootstrap4ProgressBar(); + $view_helper->__invoke(25) + ->setBgStyle('success'); + $this->assertEquals( + '
', + $view_helper->__toString() + ); + } + + /** + * Set text style, danger + */ + public function testTextStyleDanger() + { + $view_helper = new Bootstrap4ProgressBar(); + $view_helper->__invoke(25) + ->setBgStyle('danger'); + $this->assertEquals( + '
', + $view_helper->__toString() + ); + } + + /** + * Set text style, warning + */ + public function testTextStyleWarning() + { + $view_helper = new Bootstrap4ProgressBar(); + $view_helper->__invoke(25) + ->setBgStyle('warning'); + $this->assertEquals( + '
', + $view_helper->__toString() + ); + } + + /** + * Set text style, info + */ + public function testTextStyleInfo() + { + $view_helper = new Bootstrap4ProgressBar(); + $view_helper->__invoke(25) + ->setBgStyle('info'); + $this->assertEquals( + '
', + $view_helper->__toString() + ); + } + + /** + * Set text style, light + */ + public function testTextStyleLight() + { + $view_helper = new Bootstrap4ProgressBar(); + $view_helper->__invoke(25) + ->setBgStyle('light'); + $this->assertEquals( + '
', + $view_helper->__toString() + ); + } + + /** + * Set text style, dark + */ + public function testTextStyleDark() + { + $view_helper = new Bootstrap4ProgressBar(); + $view_helper->__invoke(25) + ->setBgStyle('dark'); + $this->assertEquals( + '
', + $view_helper->__toString() + ); + } + + /** + * Set text style, invalid + */ + public function testTextStyleInvalid() + { + $view_helper = new Bootstrap4ProgressBar(); + $view_helper->__invoke(25) + ->setBgStyle('invalid'); + $this->assertEquals( + '
', + $view_helper->__toString() + ); + } } From 910ebe3c488303a02fee89e008b0d08b7efab533 Mon Sep 17 00:00:00 2001 From: Dean Blackborough Date: Wed, 11 Oct 2017 13:03:41 +0100 Subject: [PATCH 2/2] Release * Updated changelog and readme for release. * Minor changes to methods, set* to set a value --- CHANGELOG.md | 6 ++++++ README.md | 11 ++++++----- src/Bootstrap4ProgressBar.php | 4 ++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfc610a..0db216c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ Full changelog for Zend Framework 3 view helpers library. +## v0.65.0 - Base class - 2017-10-11 + +* Additional tests for Bootstrap 4 Progress bar view helper. +* Added a base class for Bootstrap 4 view helpers, Progress bar now extends it for base functionality. +* Minor changes to method names. + ## v0.64.0 - Tests and Jumbotron - 2017-10-11 * Added tests for Bootstrap 4 Jumbotron view helper. diff --git a/README.md b/README.md index bf2dec7..33394da 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ A collection of Zend Framework 3 view helpers, primarily focused on Bootstrap 3 * Bootstrap 4 Card component * Bootstrap 4 Jumbotron component - [26 tests] * Bootstrap 4 Navbar component (lite) -* Bootstrap 4 Progress bar component - [17 tests] +* Bootstrap 4 Progress bar component - [27 tests] * Bootstrap 4 Multiple progress bar component ### Bootstrap 3 @@ -151,7 +151,7 @@ echo $this->bootstrap4NavbarLite()-> ### Bootstrap 4 progress bar -Create a progress bar +Create a progress bar component. ##### Public methods @@ -177,9 +177,10 @@ Create a progress bar with multiple bars ##### Public methods * animate() - Animate the striped background style -* colors() - Set the background colors for the progress bar -* height() - Set the height of the progress bar -* label() - Set the label for the progress bar +* setBgStyle() - Set the background colour utility class +* setHeight() - Set the height of the progress bar +* setLabel() - Set the label to display in the progress bar +* setTextStyle() - Set the text colour utility class * striped() - Enable the striped style for the progress bar background #### Example diff --git a/src/Bootstrap4ProgressBar.php b/src/Bootstrap4ProgressBar.php index 9e595e1..7ad1153 100644 --- a/src/Bootstrap4ProgressBar.php +++ b/src/Bootstrap4ProgressBar.php @@ -73,7 +73,7 @@ public function animate() : Bootstrap4ProgressBar * * @return Bootstrap4ProgressBar */ - public function height(int $height) : Bootstrap4ProgressBar + public function setHeight(int $height) : Bootstrap4ProgressBar { $this->height = $height; @@ -87,7 +87,7 @@ public function height(int $height) : Bootstrap4ProgressBar * * @return Bootstrap4ProgressBar */ - public function label(string $label) : Bootstrap4ProgressBar + public function setLabel(string $label) : Bootstrap4ProgressBar { $this->label = $label;