2019-10-22 17:04:58 +13:00
|
|
|
<?php
|
2019-10-23 10:37:43 +13:00
|
|
|
declare(strict_types=1);
|
2019-10-22 17:04:58 +13:00
|
|
|
|
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a Tip which can be rendered alongside a form field in the front-end.
|
|
|
|
* See the Tip component in the silverstripe/admin module.
|
|
|
|
*/
|
|
|
|
class Tip
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* These map to levels in the front-end Tip component
|
|
|
|
*/
|
2019-10-23 10:37:43 +13:00
|
|
|
public const IMPORTANCE_LEVELS = [
|
2019-10-22 17:04:58 +13:00
|
|
|
'NORMAL' => 'normal',
|
|
|
|
'HIGH' => 'high',
|
|
|
|
];
|
|
|
|
|
2019-10-23 10:37:43 +13:00
|
|
|
private const DEFAULT_ICON = 'lamp';
|
2019-10-22 17:04:58 +13:00
|
|
|
|
2019-10-23 10:37:43 +13:00
|
|
|
private const DEFAULT_IMPORTANCE_LEVEL = self::IMPORTANCE_LEVELS['NORMAL'];
|
2019-10-22 17:04:58 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string The icon that should be used on the Tip button
|
|
|
|
*/
|
|
|
|
private $icon;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string How important the tip is (normal or high). Informs the color and description.
|
|
|
|
*/
|
|
|
|
private $importance_level;
|
|
|
|
|
|
|
|
/**
|
2019-10-23 10:37:43 +13:00
|
|
|
* @var string The message to display in the tip
|
2019-10-22 17:04:58 +13:00
|
|
|
*/
|
|
|
|
private $message;
|
|
|
|
|
2019-10-23 10:37:43 +13:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2019-10-22 17:04:58 +13:00
|
|
|
public function __construct(
|
2019-10-23 10:37:43 +13:00
|
|
|
string $message,
|
|
|
|
string $importance_level = self::DEFAULT_IMPORTANCE_LEVEL,
|
|
|
|
string $icon = self::DEFAULT_ICON
|
|
|
|
) {
|
|
|
|
$this->setMessage($message);
|
|
|
|
$this->setIcon($icon);
|
|
|
|
$this->setImportanceLevel($importance_level);
|
2019-10-22 17:04:58 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputs props to be passed to the front-end Tip component.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-10-23 10:37:43 +13:00
|
|
|
public function getTipSchema(): array
|
2019-10-22 17:04:58 +13:00
|
|
|
{
|
|
|
|
return [
|
2019-10-23 10:37:43 +13:00
|
|
|
'content' => $this->getMessage(),
|
|
|
|
'icon' => $this->getIcon(),
|
|
|
|
'importance' => $this->getImportanceLevel(),
|
2019-10-22 17:04:58 +13:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-10-23 10:37:43 +13:00
|
|
|
public function getImportanceLevel(): string
|
2019-10-22 17:04:58 +13:00
|
|
|
{
|
|
|
|
return $this->importance_level;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $importance_level
|
2019-10-23 10:37:43 +13:00
|
|
|
* @return Tip
|
|
|
|
* @throws InvalidArgumentException
|
2019-10-22 17:04:58 +13:00
|
|
|
*/
|
2019-10-23 10:37:43 +13:00
|
|
|
public function setImportanceLevel(string $importance_level): self
|
2019-10-22 17:04:58 +13:00
|
|
|
{
|
|
|
|
if (!in_array($importance_level, self::IMPORTANCE_LEVELS)) {
|
|
|
|
throw new InvalidArgumentException(
|
2019-10-23 10:37:43 +13:00
|
|
|
'Provided importance level must be defined in Tip::IMPORTANCE_LEVELS'
|
2019-10-22 17:04:58 +13:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->importance_level = $importance_level;
|
2019-10-23 10:37:43 +13:00
|
|
|
|
|
|
|
return $this;
|
2019-10-22 17:04:58 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getIcon(): string
|
|
|
|
{
|
|
|
|
return $this->icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $icon
|
2019-10-23 10:37:43 +13:00
|
|
|
* @return Tip
|
2019-10-22 17:04:58 +13:00
|
|
|
*/
|
2019-10-23 10:37:43 +13:00
|
|
|
public function setIcon(string $icon): self
|
2019-10-22 17:04:58 +13:00
|
|
|
{
|
|
|
|
$this->icon = $icon;
|
2019-10-23 10:37:43 +13:00
|
|
|
|
|
|
|
return $this;
|
2019-10-22 17:04:58 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-10-23 10:37:43 +13:00
|
|
|
public function getMessage(): string
|
2019-10-22 17:04:58 +13:00
|
|
|
{
|
|
|
|
return $this->message;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $message
|
2019-10-23 10:37:43 +13:00
|
|
|
* @return Tip
|
2019-10-22 17:04:58 +13:00
|
|
|
*/
|
2019-10-23 10:37:43 +13:00
|
|
|
public function setMessage(string $message): self
|
2019-10-22 17:04:58 +13:00
|
|
|
{
|
|
|
|
$this->message = $message;
|
2019-10-23 10:37:43 +13:00
|
|
|
|
|
|
|
return $this;
|
2019-10-22 17:04:58 +13:00
|
|
|
}
|
|
|
|
}
|