-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add initial factory classes
- Loading branch information
Showing
5 changed files
with
292 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/** | ||
* Notifications API:Messages factory class | ||
* | ||
* @package wordpress/wp-feature-notifications | ||
*/ | ||
|
||
namespace WP\Notifications\Factory; | ||
|
||
use DateTime; | ||
use WP\Notifications\Framework; | ||
use WP\Notifications\Model; | ||
|
||
/** | ||
* Class representing a messages factory. | ||
* | ||
* @implements Framework\Factory<Messages> | ||
*/ | ||
class Messages extends Framework\Factory { | ||
|
||
/** | ||
* Instantiates a Message object. | ||
* | ||
* @param array|string $args { | ||
* Array or string of arguments for creating a message. Supported arguments | ||
* are described below. | ||
* | ||
* @type string $message Text content of the message. | ||
* @type ?string $accept_label Optional label of the accept action. | ||
* @type ?string $accept_link Optional url of the accept action. | ||
* @type ?string $accept_message Optional label of the accept action. | ||
* @type ?string $channel_title Optional human-readable title of the channel | ||
* the message was emitted from. | ||
* @type ?DateTime $created_at Optional datetime at which a message was created. | ||
* Default `'null'` | ||
* @type ?string $dismiss_label Optional label of the dismiss action. | ||
* @type ?DateTime $expires_at Optional datetime at which a message expires. | ||
* Default `'null'` | ||
* @type ?string $icon Optional icon of the message. Default `null` | ||
* @type ?int $id Optional database ID of the message. Default `null` | ||
* @type bool $is_dismissible Optional boolean of whether the notice can | ||
* be dismissed. Default `true` | ||
* @type ?string $severity Optional severity of the message. Default `null` | ||
* @type string $title Optional human-readable message label. Default `''` | ||
* } | ||
* | ||
* @return Model\Message|false A newly created instance of Message or false. | ||
*/ | ||
public function make( $args ) { | ||
$parsed = wp_parse_args( $args ); | ||
|
||
// Required properties | ||
|
||
$message = $parsed['message']; | ||
|
||
// Optional properties | ||
|
||
$accept_label = array_key_exists( 'accept_label', $parsed ) ? $parsed['accept_label'] : null; | ||
$accept_link = array_key_exists( 'accept_link', $parsed ) ? $parsed['accept_link'] : null; | ||
$channel_title = array_key_exists( 'channel_title', $parsed ) ? $parsed['channel_title'] : null; | ||
$created_at = array_key_exists( 'created_at', $parsed ) ? $parsed['created_at'] : null; | ||
$dismiss_label = array_key_exists( 'dismiss_label', $parsed ) ? $parsed['dismiss_label'] : null; | ||
$expires_at = array_key_exists( 'expires_at', $parsed ) ? $parsed['expires_at'] : null; | ||
$icon = array_key_exists( 'icon', $parsed ) ? $parsed['icon'] : null; | ||
$id = array_key_exists( 'id', $parsed ) ? $parsed['id'] : null; | ||
$is_dismissible = array_key_exists( 'is_dismissible', $parsed ) ? $parsed['is_dismissible'] : true; | ||
$severity = array_key_exists( 'severity', $parsed ) ? $parsed['severity'] : null; | ||
$title = array_key_exists( 'title', $parsed ) ? $parsed['title'] : ''; | ||
|
||
return new Model\Message( | ||
$message, | ||
$accept_label, | ||
$accept_link, | ||
$channel_title, | ||
$created_at, | ||
$dismiss_label, | ||
$expires_at, | ||
$icon, | ||
$id, | ||
$is_dismissible, | ||
$severity, | ||
$title, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
/** | ||
* Notifications API:Notifications factory class | ||
* | ||
* @package wordpress/wp-feature-notifications | ||
*/ | ||
|
||
namespace WP\Notifications\Factory; | ||
|
||
use DateTime; | ||
use WP\Notifications\Framework; | ||
use WP\Notifications\Helper; | ||
use WP\Notifications\Model; | ||
|
||
/** | ||
* Class representing a notifications factory. | ||
* | ||
* @implements Framework\Factory<Notifications> | ||
*/ | ||
class Notifications extends Framework\Factory { | ||
|
||
/** | ||
* Instantiates a Notification object. | ||
* @param array|string $args { | ||
* Array or string of arguments for creating a notification. Supported arguments are described below. | ||
* | ||
* @type string $channel_name Channel name, including namespace, | ||
* the notification was emitted from. | ||
* @type int $message_id ID of the message related to the | ||
* notification. | ||
* @type int $user_id ID of the user the notification | ||
* belongs to. | ||
* @type ?string $context Optional display context of the | ||
* notification. Default `'adminbar'` | ||
* @type string|DateTime|null $created_at Optional datetime at which the | ||
* notification was created. Default `null` | ||
* @type string|DateTime|null $dismissed_at Optional datetime t which the | ||
* notification was dismissed. Default `null` | ||
* @type string|DateTime|null $displayed_at Optional datetime at which the | ||
* notification was first displayed. | ||
* Default `null` | ||
* @type string|DateTime|null $expires_at Optional datetime at which the | ||
* notification expires. Default `null` | ||
* } | ||
* @param bool $validate Optionally validate the arguments. | ||
* | ||
* @return Model\Notification|false A newly created instance of Channel or false. | ||
*/ | ||
public function make( $args ) { | ||
$parsed = wp_parse_args( $args ); | ||
|
||
// Required properties | ||
|
||
$channel_name = $parsed['channel_name']; | ||
$message_id = $parsed['message_id']; | ||
$user_id = $parsed['user_id']; | ||
|
||
// Optional properties | ||
|
||
$context = array_key_exists( 'context', $parsed ) ? $parsed['context'] : 'adminbar'; | ||
$created_at = array_key_exists( 'created_at', $parsed ) ? $parsed['created_at'] : null; | ||
$dismissed_at = array_key_exists( 'dismissed_at', $parsed ) ? $parsed['dismissed_at'] : null; | ||
$displayed_at = array_key_exists( 'displayed_at', $parsed ) ? $parsed['displayed_at'] : null; | ||
$expires_at = array_key_exists( 'expires_at', $parsed ) ? $parsed['expires_at'] : null; | ||
|
||
// Deserialize MySQL datetime strings. | ||
|
||
$created_at = Helper\Serde::maybe_deserialize_mysql_date( $created_at ); | ||
$dismissed_at = Helper\Serde::maybe_deserialize_mysql_date( $dismissed_at ); | ||
$displayed_at = Helper\Serde::maybe_deserialize_mysql_date( $displayed_at ); | ||
$expires_at = Helper\Serde::maybe_deserialize_mysql_date( $expires_at ); | ||
|
||
$notification = new Model\Notification( | ||
$channel_name, | ||
$message_id, | ||
$user_id, | ||
$context, | ||
$created_at, | ||
$dismissed_at, | ||
$displayed_at, | ||
$expires_at | ||
); | ||
|
||
return $notification; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
/** | ||
* Notifications API:Subscriptions factory class | ||
* | ||
* @package wordpress/wp-feature-notifications | ||
*/ | ||
|
||
namespace WP\Notifications; | ||
|
||
use DateTime; | ||
use WP\Notifications\Framework; | ||
use WP\Notifications\Helper; | ||
use WP\Notifications\Model; | ||
|
||
/** | ||
* Class representing a subscriptions factory | ||
* | ||
* @implements Framework\Factory<Subscriptions>. | ||
*/ | ||
class Subscriptions extends Framework\Factory { | ||
|
||
/** | ||
* Instantiates a Subscription object. | ||
* | ||
* @param array|string $args { | ||
* Array or string of arguments for creating a subscription. Supported | ||
* arguments are described below. | ||
* | ||
* @type string $channel_name Namespaced channel name of the | ||
* subscription. | ||
* @type int $user_id ID of the user the subscription | ||
* belongs to. | ||
* @type string|DateTime|null $created_at Optional datetime at which the | ||
* subscription was created. | ||
* @type string|DateTime|null $snoozed_until Optional snoozed until datetime | ||
* of the subscription. | ||
* } | ||
* | ||
* @return Model\Subscription|false A newly created instance of Subscription or false. | ||
*/ | ||
public function make( $args ) { | ||
$parsed = wp_parse_args( $args ); | ||
|
||
// Required properties | ||
|
||
$channel_name = $parsed['channel_name']; | ||
$user_id = $parsed['user_id']; | ||
|
||
// Optional properties | ||
|
||
$created_at = array_key_exists( 'created_at', $parsed ) ? $parsed['created_at'] : null; | ||
$snoozed_until = array_key_exists( 'snoozed_until', $parsed ) ? $parsed['snoozed_until'] : null; | ||
|
||
// Deserialize MySQL datetime strings. | ||
|
||
$created_at = Helper\Serde::maybe_deserialize_mysql_date( $created_at ); | ||
$snoozed_until = Helper\Serde::maybe_deserialize_mysql_date( $snoozed_until ); | ||
|
||
$subscription = new Model\Subscription( | ||
$channel_name, | ||
$user_id, | ||
$created_at, | ||
$snoozed_until, | ||
); | ||
|
||
return $subscription; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Notifications API:Factory abstract class | ||
* | ||
* @package wordpress/wp-feature-notifications | ||
*/ | ||
|
||
namespace WP\Notifications\Framework; | ||
|
||
/** | ||
* Abstract class representing a factory. | ||
* | ||
* @template Model | ||
*/ | ||
abstract class Factory { | ||
|
||
/** | ||
* Container for the main instance of the class. | ||
* | ||
* @var ?Model | ||
*/ | ||
protected static $instance = null; | ||
|
||
/** | ||
* Instantiates a model object. | ||
* @param array|string $args Array or string of arguments for creating a model. | ||
* | ||
* @return Model|false A newly created instance of model or false. | ||
*/ | ||
abstract public function make( $args ); | ||
|
||
/** | ||
* Utility method to retrieve the main instance of the class. | ||
* | ||
* The instance will be created if it does not exist yet. | ||
* | ||
* @return Model The main instance. | ||
*/ | ||
public static function get_instance() { | ||
if ( null === self::$instance ) { | ||
self::$instance = new self(); | ||
} | ||
|
||
return self::$instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters