NEW Add type declarations to Tip API, add TippableFieldInterface

This commit is contained in:
Garion Herman 2019-10-23 10:37:43 +13:00
parent 195417b061
commit bed3f2b3c6
4 changed files with 60 additions and 39 deletions

View File

@ -7,7 +7,7 @@ use SilverStripe\Dev\Deprecation;
/** /**
* Text input field. * Text input field.
*/ */
class TextField extends FormField class TextField extends FormField implements TippableFieldInterface
{ {
/** /**
* @var int * @var int
@ -19,7 +19,7 @@ class TextField extends FormField
/** /**
* @var Tip|null A tip to render beside the input * @var Tip|null A tip to render beside the input
*/ */
protected $tip; private $tip;
/** /**
* Returns an input field. * Returns an input field.
@ -67,7 +67,7 @@ class TextField extends FormField
/** /**
* @return Tip|null * @return Tip|null
*/ */
public function getTip() public function getTip(): ?Tip
{ {
return $this->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 * @param Tip|null $tip The Tip to apply, or null to remove an existing one
* @return $this * @return $this
*/ */
public function setTip(Tip $tip = null) public function setTip(?Tip $tip = null): self
{ {
$this->tip = $tip; $this->tip = $tip;
@ -112,8 +112,8 @@ class TextField extends FormField
$data = parent::getSchemaDataDefaults(); $data = parent::getSchemaDataDefaults();
$data['data']['maxlength'] = $this->getMaxLength(); $data['data']['maxlength'] = $this->getMaxLength();
if ($this->tip instanceof Tip) { if ($this->getTip() instanceof Tip) {
$data['tip'] = $this->tip->getTipSchema(); $data['tip'] = $this->getTip()->getTipSchema();
} }
return $data; return $data;

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
namespace SilverStripe\Forms; namespace SilverStripe\Forms;
@ -7,22 +8,20 @@ use InvalidArgumentException;
/** /**
* Represents a Tip which can be rendered alongside a form field in the front-end. * Represents a Tip which can be rendered alongside a form field in the front-end.
* See the Tip component in the silverstripe/admin module. * See the Tip component in the silverstripe/admin module.
*
* @package SilverStripe\Forms
*/ */
class Tip class Tip
{ {
/** /**
* These map to levels in the front-end Tip component * These map to levels in the front-end Tip component
*/ */
const IMPORTANCE_LEVELS = [ public const IMPORTANCE_LEVELS = [
'NORMAL' => 'normal', 'NORMAL' => 'normal',
'HIGH' => 'high', '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 * @var string The icon that should be used on the Tip button
@ -35,25 +34,24 @@ class Tip
private $importance_level; private $importance_level;
/** /**
* @var string The contents of the Tip UI * @var string The message to display in the tip
*/ */
private $message; 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( public function __construct(
$message, string $message,
$importance_level = self::DEFAULT_IMPORTANCE_LEVEL, string $importance_level = self::DEFAULT_IMPORTANCE_LEVEL,
$icon = self::DEFAULT_ICON string $icon = self::DEFAULT_ICON
) ) {
{ $this->setMessage($message);
if (!in_array($importance_level, self::IMPORTANCE_LEVELS)) { $this->setIcon($icon);
throw new InvalidArgumentException( $this->setImportanceLevel($importance_level);
'Provided $importance_level must be defined in Tip::IMPORTANCE_LEVELS'
);
}
$this->message = $message;
$this->icon = $icon;
$this->importance_level = $importance_level;
} }
/** /**
@ -61,35 +59,39 @@ class Tip
* *
* @return array * @return array
*/ */
public function getTipSchema() public function getTipSchema(): array
{ {
return [ return [
'content' => $this->message, 'content' => $this->getMessage(),
'icon' => $this->icon, 'icon' => $this->getIcon(),
'importance' => $this->importance_level, 'importance' => $this->getImportanceLevel(),
]; ];
} }
/** /**
* @return string * @return string
*/ */
public function getImportanceLevel() public function getImportanceLevel(): string
{ {
return $this->importance_level; return $this->importance_level;
} }
/** /**
* @param string $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)) { if (!in_array($importance_level, self::IMPORTANCE_LEVELS)) {
throw new InvalidArgumentException( 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; $this->importance_level = $importance_level;
return $this;
} }
/** /**
@ -102,25 +104,31 @@ class Tip
/** /**
* @param string $icon * @param string $icon
* @return Tip
*/ */
public function setIcon($icon) public function setIcon(string $icon): self
{ {
$this->icon = $icon; $this->icon = $icon;
return $this;
} }
/** /**
* @return string * @return string
*/ */
public function getMessage() public function getMessage(): string
{ {
return $this->message; return $this->message;
} }
/** /**
* @param string $message * @param string $message
* @return Tip
*/ */
public function setMessage($message) public function setMessage(string $message): self
{ {
$this->message = $message; $this->message = $message;
return $this;
} }
} }

View File

@ -0,0 +1,13 @@
<?php
namespace SilverStripe\Forms;
/**
* Declares that a form field has the ability to accept and render Tips.
*/
interface TippableFieldInterface
{
public function getTip(): ?Tip;
public function setTip(Tip $tip);
}

View File

@ -35,7 +35,7 @@ class TipTest extends SapphireTest
$tip = new Tip( $tip = new Tip(
'message', 'message',
Tip::IMPORTANCE_LEVELS['HIGH'], Tip::IMPORTANCE_LEVELS['HIGH'],
'page' 'page'
); );
$schema = $tip->getTipSchema(); $schema = $tip->getTipSchema();
@ -54,7 +54,7 @@ class TipTest extends SapphireTest
* Ensure passing an invalid importance level to the constructor fails * Ensure passing an invalid importance level to the constructor fails
* *
* @expectedException InvalidArgumentException * @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() public function testInvalidImportanceLevelInConstructorCausesException()
{ {
@ -65,7 +65,7 @@ class TipTest extends SapphireTest
* Ensure setting an invalid importance level fails * Ensure setting an invalid importance level fails
* *
* @expectedException InvalidArgumentException * @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() public function testInvalidImportanceLevelInSetterCausesException()
{ {