From bed3f2b3c691d3b157f615f1c0b6137f0908a40d Mon Sep 17 00:00:00 2001 From: Garion Herman Date: Wed, 23 Oct 2019 10:37:43 +1300 Subject: [PATCH] NEW Add type declarations to Tip API, add TippableFieldInterface --- src/Forms/TextField.php | 12 ++--- src/Forms/Tip.php | 68 ++++++++++++++++------------ src/Forms/TippableFieldInterface.php | 13 ++++++ tests/php/Forms/TipTest.php | 6 +-- 4 files changed, 60 insertions(+), 39 deletions(-) create mode 100644 src/Forms/TippableFieldInterface.php diff --git a/src/Forms/TextField.php b/src/Forms/TextField.php index 2f87da55f..0e261f7d7 100644 --- a/src/Forms/TextField.php +++ b/src/Forms/TextField.php @@ -7,7 +7,7 @@ use SilverStripe\Dev\Deprecation; /** * Text input field. */ -class TextField extends FormField +class TextField extends FormField implements TippableFieldInterface { /** * @var int @@ -19,7 +19,7 @@ class TextField extends FormField /** * @var Tip|null A tip to render beside the input */ - protected $tip; + private $tip; /** * Returns an input field. @@ -67,7 +67,7 @@ class TextField extends FormField /** * @return Tip|null */ - public function getTip() + public function getTip(): ?Tip { return $this->tip; } @@ -80,7 +80,7 @@ class TextField extends FormField * @param Tip|null $tip The Tip to apply, or null to remove an existing one * @return $this */ - public function setTip(Tip $tip = null) + public function setTip(?Tip $tip = null): self { $this->tip = $tip; @@ -112,8 +112,8 @@ class TextField extends FormField $data = parent::getSchemaDataDefaults(); $data['data']['maxlength'] = $this->getMaxLength(); - if ($this->tip instanceof Tip) { - $data['tip'] = $this->tip->getTipSchema(); + if ($this->getTip() instanceof Tip) { + $data['tip'] = $this->getTip()->getTipSchema(); } return $data; diff --git a/src/Forms/Tip.php b/src/Forms/Tip.php index 9ad86220e..508e3451c 100644 --- a/src/Forms/Tip.php +++ b/src/Forms/Tip.php @@ -1,4 +1,5 @@ 'normal', 'HIGH' => 'high', ]; - const DEFAULT_ICON = 'lamp'; + private const DEFAULT_ICON = 'lamp'; - const DEFAULT_IMPORTANCE_LEVEL = self::IMPORTANCE_LEVELS['NORMAL']; + private const DEFAULT_IMPORTANCE_LEVEL = self::IMPORTANCE_LEVELS['NORMAL']; /** * @var string The icon that should be used on the Tip button @@ -35,25 +34,24 @@ class Tip private $importance_level; /** - * @var string The contents of the Tip UI + * @var string The message to display in the tip */ private $message; + /** + * @param string $message The message to display in the tip + * @param string $importance_level How important the tip is (normal or high). Informs the color and description. + * @param string $icon The icon that should be used on the Tip button + * @throws InvalidArgumentException + */ public function __construct( - $message, - $importance_level = self::DEFAULT_IMPORTANCE_LEVEL, - $icon = self::DEFAULT_ICON - ) - { - if (!in_array($importance_level, self::IMPORTANCE_LEVELS)) { - throw new InvalidArgumentException( - 'Provided $importance_level must be defined in Tip::IMPORTANCE_LEVELS' - ); - } - - $this->message = $message; - $this->icon = $icon; - $this->importance_level = $importance_level; + string $message, + string $importance_level = self::DEFAULT_IMPORTANCE_LEVEL, + string $icon = self::DEFAULT_ICON + ) { + $this->setMessage($message); + $this->setIcon($icon); + $this->setImportanceLevel($importance_level); } /** @@ -61,35 +59,39 @@ class Tip * * @return array */ - public function getTipSchema() + public function getTipSchema(): array { return [ - 'content' => $this->message, - 'icon' => $this->icon, - 'importance' => $this->importance_level, + 'content' => $this->getMessage(), + 'icon' => $this->getIcon(), + 'importance' => $this->getImportanceLevel(), ]; } /** * @return string */ - public function getImportanceLevel() + public function getImportanceLevel(): string { return $this->importance_level; } /** * @param string $importance_level + * @return Tip + * @throws InvalidArgumentException */ - public function setImportanceLevel($importance_level) + public function setImportanceLevel(string $importance_level): self { if (!in_array($importance_level, self::IMPORTANCE_LEVELS)) { throw new InvalidArgumentException( - 'Provided $importance_level must be defined in Tip::IMPORTANCE_LEVELS' + 'Provided importance level must be defined in Tip::IMPORTANCE_LEVELS' ); } $this->importance_level = $importance_level; + + return $this; } /** @@ -102,25 +104,31 @@ class Tip /** * @param string $icon + * @return Tip */ - public function setIcon($icon) + public function setIcon(string $icon): self { $this->icon = $icon; + + return $this; } /** * @return string */ - public function getMessage() + public function getMessage(): string { return $this->message; } /** * @param string $message + * @return Tip */ - public function setMessage($message) + public function setMessage(string $message): self { $this->message = $message; + + return $this; } } diff --git a/src/Forms/TippableFieldInterface.php b/src/Forms/TippableFieldInterface.php new file mode 100644 index 000000000..cc1ecabe8 --- /dev/null +++ b/src/Forms/TippableFieldInterface.php @@ -0,0 +1,13 @@ +getTipSchema(); @@ -54,7 +54,7 @@ class TipTest extends SapphireTest * Ensure passing an invalid importance level to the constructor fails * * @expectedException InvalidArgumentException - * @expectedExceptionMessage Provided $importance_level must be defined in Tip::IMPORTANCE_LEVELS + * @expectedExceptionMessage Provided importance level must be defined in Tip::IMPORTANCE_LEVELS */ public function testInvalidImportanceLevelInConstructorCausesException() { @@ -65,7 +65,7 @@ class TipTest extends SapphireTest * Ensure setting an invalid importance level fails * * @expectedException InvalidArgumentException - * @expectedExceptionMessage Provided $importance_level must be defined in Tip::IMPORTANCE_LEVELS + * @expectedExceptionMessage Provided importance level must be defined in Tip::IMPORTANCE_LEVELS */ public function testInvalidImportanceLevelInSetterCausesException() {