File tree Expand file tree Collapse file tree
files/lib/system/form/builder/field Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ { * todo *}
You can’t perform that action at this time.
0 commit comments