Skip to content

Commit 2fdf090

Browse files
committed
prepare star rating field
1 parent c37822e commit 2fdf090

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace wcf\system\form\builder\field;
4+
5+
use InvalidArgumentException;
6+
7+
/**
8+
* @author Florian Gail
9+
* @copyright Florian Gail; 2018 - 2022; <https://www.mysterycode.de>
10+
*/
11+
class MCStarRatingFormField extends SingleSelectionFormField implements IMinimumFormField
12+
{
13+
use TMinimumFormField;
14+
15+
protected int $bestRating;
16+
17+
public function __construct()
18+
{
19+
$this->minimum(1);
20+
$this->bestRating(5);
21+
}
22+
23+
protected function generateStars(int $amount): string
24+
{
25+
$result = '';
26+
27+
for ($i = 1; $i <= $amount; $i++) {
28+
$result .= '';
29+
}
30+
31+
return $result;
32+
}
33+
34+
public function populate()
35+
{
36+
$ratings = [];
37+
for ($i = 0; $i <= $this->getBestRating(); $i++) {
38+
$ratings[$i] = $this->generateStars($i);
39+
}
40+
41+
$this->options($ratings);
42+
}
43+
44+
public function bestRating(int $bestRating): self
45+
{
46+
if ($this->getMinimum() !== null && $bestRating < $this->getMinimum()) {
47+
throw new InvalidArgumentException('Best rating cannot be less than minimum rating.');
48+
}
49+
50+
$this->bestRating = $bestRating;
51+
52+
return $this;
53+
}
54+
55+
public function getBestRating(): int
56+
{
57+
return $this->bestRating;
58+
}
59+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{* todo *}

0 commit comments

Comments
 (0)