Skip to content

Commit

Permalink
Clean up ProgressBar::class (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Jan 4, 2022
1 parent 613d69f commit d5255e0
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 84 deletions.
14 changes: 8 additions & 6 deletions docs/progressbar.md
Expand Up @@ -44,14 +44,16 @@ The code above generates the following HTML:
<progress id="w1-progressbar" class="progress is-medium is-info" value="75" max="100">75%</progress>
```

## Reference
## Setters

All setters are immutable and return a new instance of the `Yiisoft\Yii\Bulma\ProgressBar` class with the specified value.

Method | Description | Default
-------|-------------|---------
`id(string $value)` | Widget ID. | `''`
`attributes(array $value)` | The HTML attributes. | `[]`
`autoIdPrefix(string $value)` | Prefix to the automatically generated widget ID. | `w`
`value(float $value)` | The progress value. Set to `0` to display a loading animation. | `0`
`color(string $value)` | Bar color. Options available are: (`ProgressBar::COLOR_PRIMARY`, `ProgressBar::COLOR_LINK`, `ProgressBar::COLOR_INFO`, `ProgressBar::COLOR_SUCCESS`, `ProgressBar::COLOR_WARNING`, `ProgressBar::COLOR_DANGER`, `ProgressBar::COLOR_DARK`). | Default is no color.
`id(string $value)` | Widget ID. | `''`
`maxValue(int $value)` | Maximum progress value. `0` means no maximum. | `100`
`options(array $value)` | HTML attributes for the widget container tag. | `['class' => 'progress']`
`size(string $value)` | Bar size. | `is-small`, `is-medium`, `is-large`
`color(string $value)` | Bar color. | `is-primary`, `is-link`, `is-info`, `is-success`, `is-warning`, `is-danger`
`size(string $value)` | Bar size. Options available are: (`ProgressBar::SIZE_SMALL`, `ProgressBar::SIZE_MEDIUM`, `ProgressBar::SIZE_LARGE`). | Default setting is normal.
`value(float $value)` | The progress value. Set to `0` to display a loading animation. | `0`
131 changes: 90 additions & 41 deletions src/ProgressBar.php
Expand Up @@ -6,6 +6,8 @@

use InvalidArgumentException;
use Yiisoft\Html\Html;
use Yiisoft\Html\Tag\CustomTag;
use Yiisoft\Widget\Widget;

use function implode;
use function in_array;
Expand Down Expand Up @@ -37,77 +39,119 @@ final class ProgressBar extends Widget
public const COLOR_SUCCESS = 'is-success';
public const COLOR_WARNING = 'is-warning';
public const COLOR_DANGER = 'is-danger';
public const COLOR_DARK = 'is-dark';
private const COLOR_ALL = [
self::COLOR_PRIMARY,
self::COLOR_LINK,
self::COLOR_INFO,
self::COLOR_SUCCESS,
self::COLOR_WARNING,
self::COLOR_DANGER,
self::COLOR_DARK,
];

private array $options = [];
private float $value = 0;
private int $maxValue = 100;
private string $size = '';
private array $attributes = [];
private string $autoIdPrefix = 'w';
private string $color = '';
private string $size = '';

/**
* Returns a new instance with the specified HTML attributes for the widget container tag.
* The HTML attributes.
*
* @param array $value The HTML attributes for the widget container tag.
* @param array $values Attribute values indexed by attribute names.
*
* @return self
*
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
public function options(array $value): self
public function attributes(array $values): self
{
$new = clone $this;
$new->options = $value;
$new->attributes = $values;
return $new;
}

/**
* Returns a new instance with the specified prefix to the automatically generated widget IDs.
*
* @param string $value The prefix to the automatically generated widget IDs.
*
* @return self
*/
public function autoIdPrefix(string $value): self
{
$new = clone $this;
$new->autoIdPrefix = $value;
return $new;
}

/**
* Returns a new instance with the specified value of the progress.
* Returns a new instance with the specified progress bar color.
*
* @param float $value The value of the progress. Set `0` to display loading animation.
* @param string $value The progress bar color. By default there is no color.
* Possible values: ProgressBar::COLOR_PRIMARY, ProgressBar::COLOR_LINK, ProgressBar::COLOR_INFO,
* ProgressBar::COLOR_SUCCESS, ProgressBar::COLOR_WARNING, ProgressBar::COLOR_DANGER, ProgressBar::COLOR_DARK.
*
* @return self
*/
public function value(float $value): self
public function color(string $value): self
{
if (!in_array($value, self::COLOR_ALL, true)) {
$values = implode('", "', self::COLOR_ALL);
throw new InvalidArgumentException("Invalid color. Valid values are: \"$values\".");
}

$new = clone $this;
$new->value = $value;
$new->color = $value;

return $new;
}

/**
* Returns a new instance with the specified ID of the widget.
*
* @param string $value The ID of the widget.
*
* @return self
*/
public function id(string $value): self
{
$new = clone $this;
$new->attributes['id'] = $value;
return $new;
}

/**
* Returns a new instance with the specified maximum progress value.
*
* @param int $value Maximum progress value. Set `0` for no maximum.
* @param int|null $value Maximum progress value. Set `0` for no maximum.
*
* @return self
*/
public function maxValue(int $value): self
public function maxValue(?int $value): self
{
if ($value < 0 || $value > 100) {
throw new InvalidArgumentException('Invalid max value. It must be between 0 and 100.');
}

$new = clone $this;
$new->maxValue = $value;
$new->attributes['max'] = $value;

return $new;
}

/**
* Returns a new instance with the specified progress bar size class.
*
* @param string $value The progress bar size class.
* @param string $value The progress bar size class. Default setting is "normal".
* Possible values: ProgressBar::SIZE_SMALL, ProgressBar::SIZE_MEDIUM, Model::SIZE_LARGE.
*
* @return self
*/
public function size(string $value): self
{
if (!in_array($value, self::SIZE_ALL, true)) {
$values = implode('"', self::SIZE_ALL);
$values = implode('", "', self::SIZE_ALL);
throw new InvalidArgumentException("Invalid size. Valid values are: \"$values\".");
}

Expand All @@ -118,56 +162,61 @@ public function size(string $value): self
}

/**
* Returns a new instance with the specified progress bar color.
* Returns a new instance with the specified value of the progress.
*
* @param string $value The progress bar color.
* @param float|null $value The value of the progress. Set `0` to display loading animation.
*
* @return self
*/
public function color(string $value): self
public function value(?float $value): self
{
if (!in_array($value, self::COLOR_ALL, true)) {
$values = implode('"', self::COLOR_ALL);
throw new InvalidArgumentException("Invalid color. Valid values are: \"$values\".");
if ($value < 0 || $value > 100) {
throw new InvalidArgumentException('Invalid value. It must be between 0 and 100.');
}

$new = clone $this;
$new->color = $value;
$new->attributes['value'] = $value;

return $new;
}

protected function run(): string
{
$this->buildOptions();
$attributes = $this->build($this->attributes);
$content = '';

$content = $this->value > 0 ? $this->value . '%' : '';
if (array_key_exists('value', $attributes)) {
/** @var float|null */
$attributes['value'] = $attributes['value'] === 0.0 ? null : $attributes['value'];
$content = $attributes['value'] > 0 ? (string)$attributes['value'] . '%' : '';
}

return Html::tag('progress', $content, $this->options)->render();
return CustomTag::name('progress')->attributes($attributes)->content($content)->render();
}

private function buildOptions(): void
private function build(array $attributes): array
{
if (!isset($this->options['id'])) {
$this->options['id'] = "{$this->getId()}-progressbar";
/** @var string */
$attributes['id'] ??= (Html::generateId($this->autoIdPrefix) . '-progressbar');

if (array_key_exists('max', $attributes)) {
/** @var int|null */
$attributes['max'] = $attributes['max'] === 0 ? null : $attributes['max'];
} else {
/** @var int|null */
$attributes['max'] ??= 100;
}

$this->options = $this->addOptions($this->options, 'progress');

if ($this->maxValue > 0) {
$this->options['max'] = $this->maxValue;
}

if ($this->value > 0) {
$this->options['value'] = $this->value;
}
Html::addCssClass($attributes, 'progress');

if ($this->size !== '') {
Html::addCssClass($this->options, $this->size);
Html::addCssClass($attributes, $this->size);
}

if ($this->color !== '') {
Html::addCssClass($this->options, $this->color);
Html::addCssClass($attributes, $this->color);
}

return $attributes;
}
}

0 comments on commit d5255e0

Please sign in to comment.