give
/
vendor
/
vendor-prefixed
/
stellarwp
/
admin-notices
/
src
/
DataTransferObjects
/
NoticeElementProperties.php
give
/
vendor
/
vendor-prefixed
/
stellarwp
/
admin-notices
/
src
/
DataTransferObjects
Last commit date
NoticeElementProperties.php
1 year ago
NoticeElementProperties.php
92 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | |
| 6 | namespace Give\Vendors\StellarWP\AdminNotices\DataTransferObjects; |
| 7 | |
| 8 | use InvalidArgumentException; |
| 9 | use Give\Vendors\StellarWP\AdminNotices\AdminNotice; |
| 10 | |
| 11 | class NoticeElementProperties |
| 12 | { |
| 13 | private const CLOSE_BEHAVIOR_HIDE = 'hide'; |
| 14 | private const CLOSE_BEHAVIOR_MARK_DISMISSED = 'mark-dismissed'; |
| 15 | |
| 16 | /** |
| 17 | * @var string custom namespace for the library instance |
| 18 | */ |
| 19 | private $namespace; |
| 20 | |
| 21 | /** |
| 22 | * @var string id attribute that uniquely identifies the notice |
| 23 | */ |
| 24 | public $idAttribute; |
| 25 | |
| 26 | /** |
| 27 | * @var string attribute to put on an element which closes the notice |
| 28 | */ |
| 29 | public $customCloserAttribute; |
| 30 | |
| 31 | /** |
| 32 | * @var string attribute for custom notices which specifies the location |
| 33 | */ |
| 34 | public $customLocationAttribute; |
| 35 | |
| 36 | /** |
| 37 | * @var string attributes to be applied to custom notices |
| 38 | */ |
| 39 | public $customWrapperAttributes; |
| 40 | |
| 41 | /** |
| 42 | * @since 2.0.0 |
| 43 | */ |
| 44 | public function __construct(AdminNotice $notice, string $namespace) |
| 45 | { |
| 46 | $this->namespace = $namespace; |
| 47 | |
| 48 | $this->idAttribute = "data-stellarwp-$namespace-notice-id='{$notice->getId()}'"; |
| 49 | |
| 50 | $this->customLocationAttribute = "data-stellarwp-$namespace-location='{$notice->getLocation()}'"; |
| 51 | $this->customWrapperAttributes = "$this->idAttribute $this->customLocationAttribute"; |
| 52 | |
| 53 | $this->customCloserAttribute = "data-stellarwp-$namespace-close-notice='{$notice->getId()}'"; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @since 2.0.0 |
| 58 | * |
| 59 | * @param 'hide'|'mark-dismissed' $behavior |
| 60 | */ |
| 61 | public function customCloseBehaviorAttribute(string $behavior = self::CLOSE_BEHAVIOR_HIDE): string |
| 62 | { |
| 63 | if (!$this->behaviorIsValid($behavior)) { |
| 64 | throw new InvalidArgumentException('Invalid behavior for custom closer attribute.'); |
| 65 | } |
| 66 | |
| 67 | return "data-stellarwp-{$this->namespace}-close-notice-behavior='$behavior'"; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @since 2.0.0 |
| 72 | * |
| 73 | * @param 'hide'|'mark-dismissed' $behavior |
| 74 | */ |
| 75 | public function closeAttributes(string $behavior = self::CLOSE_BEHAVIOR_HIDE): string |
| 76 | { |
| 77 | if (!$this->behaviorIsValid($behavior)) { |
| 78 | throw new InvalidArgumentException('Invalid behavior for custom closer attribute.'); |
| 79 | } |
| 80 | |
| 81 | return "$this->customCloserAttribute {$this->customCloseBehaviorAttribute($behavior)}"; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @since 2.0.0 |
| 86 | */ |
| 87 | private function behaviorIsValid(string $behavior): bool |
| 88 | { |
| 89 | return in_array($behavior, [self::CLOSE_BEHAVIOR_HIDE, self::CLOSE_BEHAVIOR_MARK_DISMISSED], true); |
| 90 | } |
| 91 | } |
| 92 |