Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / form / builder / field / TMaximumFormField.class.php
1 <?php
2
3 namespace wcf\system\form\builder\field;
4
5 /**
6 * Provides default implementations of `IMaximumFormField` methods.
7 *
8 * @author Matthias Schmidt
9 * @copyright 2001-2019 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11 * @package WoltLabSuite\Core\System\Form\Builder\Field
12 * @since 5.2
13 */
14 trait TMaximumFormField
15 {
16 /**
17 * maximum of the field value
18 * @var null|number
19 */
20 protected $maximum;
21
22 /**
23 * Returns the maximum of the values of this field or `null` if no maximum
24 * has been set.
25 *
26 * @return null|number
27 */
28 public function getMaximum()
29 {
30 return $this->maximum;
31 }
32
33 /**
34 * Sets the maximum of the values of this field. If `null` is passed, the
35 * maximum is removed.
36 *
37 * @param null|number $maximum maximum field value
38 * @return static this field
39 *
40 * @throws \InvalidArgumentException if the given maximum is no number or otherwise invalid
41 */
42 public function maximum($maximum = null)
43 {
44 if ($maximum !== null) {
45 if (!\is_numeric($maximum)) {
46 throw new \InvalidArgumentException("Given maximum is no int, '" . \gettype($maximum) . "' given.");
47 }
48
49 if ($this instanceof IMinimumFormField) {
50 $minimum = $this->getMinimum();
51 if ($minimum !== null && $minimum > $maximum) {
52 throw new \InvalidArgumentException(
53 "Minimum ({$minimum}) cannot be greater than maximum ({$maximum})."
54 );
55 }
56 }
57 }
58
59 $this->maximum = $maximum;
60
61 return $this;
62 }
63 }