-
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.
- Loading branch information
Showing
6 changed files
with
821 additions
and
1 deletion.
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,60 @@ | ||
<?php | ||
/** | ||
* Notifications API:Serde class | ||
* | ||
* @package wordpress/wp-feature-notifications | ||
*/ | ||
|
||
namespace WP\Notifications\Helper; | ||
|
||
use DateTime; | ||
|
||
/** | ||
* Class Serde | ||
* | ||
* SERialization and DEserialization helper static class. | ||
*/ | ||
class Serde { | ||
|
||
/** | ||
* Maybe serialize a DateTime as an ISO 8601 date string. | ||
* | ||
* @param DateTime|null $date The possible DateTime object to serialize. | ||
* | ||
* @return string|null Maybe an ISO 8601 date string. | ||
*/ | ||
public static function maybe_serialize_json_date( $date ) { | ||
if ( null === $date ) { | ||
return null; | ||
} | ||
|
||
return $date->format( DateTime::ATOM ); | ||
} | ||
|
||
/** | ||
* Maybe deserialize a datetime string in MySQL format. | ||
* | ||
* @param string|DateTime|null $date The possible MySQL datetime to deserialize. | ||
* | ||
* @return DateTime|null Maybe a DateTime object. | ||
*/ | ||
public static function maybe_deserialize_mysql_date( $date ) { | ||
if ( null === $date ) { | ||
return null; | ||
} | ||
|
||
if ( $date instanceof DateTime ) { | ||
return $date; | ||
} | ||
|
||
if ( is_string( $date ) ) { | ||
$date = DateTime::createFromFormat( 'Y-m-d H:i:s', $date ); | ||
|
||
if ( false === $date ) { | ||
$date = null; | ||
} | ||
} | ||
|
||
return $date; | ||
} | ||
} |
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,135 @@ | ||
<?php | ||
/** | ||
* Notifications API:Channel class | ||
* | ||
* @package wordpress/wp-feature-notifications | ||
*/ | ||
|
||
namespace WP\Notifications\Model; | ||
|
||
use JsonSerializable; | ||
|
||
/** | ||
* Class representing a notification channel. | ||
* | ||
* @see register_channel() | ||
*/ | ||
class Channel implements JsonSerializable { | ||
|
||
/** | ||
* Name, including namespace, of the channel. | ||
* | ||
* @var string | ||
*/ | ||
protected string $name; | ||
|
||
/** | ||
* Human-readable label of the channel. | ||
* | ||
* @var string | ||
*/ | ||
protected string $title; | ||
|
||
// Optional properties | ||
|
||
/** | ||
* Display context of the channel. | ||
*/ | ||
protected ?string $context; | ||
|
||
/** | ||
* Detailed description of the channel. | ||
*/ | ||
protected ?string $description; | ||
|
||
/** | ||
* Icon of the channel. | ||
*/ | ||
protected ?string $icon; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* Instantiates a Channel object. | ||
* | ||
* @see register_channel() | ||
* | ||
* @param string $name Name, including namespace, of the channel. | ||
* @param string $title Human-readable label of the channel. | ||
* @param ?string $context Optional default display context of the channel. | ||
* @param ?string $description Optional detailed description of the channel. | ||
* @param ?string $icon Optional icon of the channel. | ||
* | ||
*/ | ||
public function __construct( $name, $title, $context = null, $description = null, $icon = null ) { | ||
$this->name = $name; | ||
$this->title = $title; | ||
|
||
// Optional properties | ||
|
||
$this->context = $context; | ||
$this->icon = $icon; | ||
$this->description = $description; | ||
} | ||
|
||
/** | ||
* Specifies data which should be serialized to JSON. | ||
* | ||
* @return mixed Data which can be serialized by json_encode, which is a | ||
* value of any type other than a resource. | ||
*/ | ||
public function jsonSerialize(): mixed { | ||
return array( | ||
'name' => $this->name, | ||
'title' => $this->title, | ||
'context' => $this->context, | ||
'icon' => $this->icon, | ||
'description' => $this->description, | ||
); | ||
} | ||
|
||
/** | ||
* Get the namespaced name. | ||
* | ||
* @return string The namespaced name of the channel. | ||
*/ | ||
public function get_name(): string { | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* Get the human-readable label. | ||
* | ||
* @return string The title of the channel. | ||
*/ | ||
public function get_title(): string { | ||
return $this->title; | ||
} | ||
|
||
/** | ||
* Get the default display context. | ||
* | ||
* @return ?string The context of the channel. | ||
*/ | ||
public function get_context(): ?string { | ||
return $this->context; | ||
} | ||
|
||
/** | ||
* Get the detailed description. | ||
* | ||
* @return ?string The description of the channel. | ||
*/ | ||
public function get_description(): ?string { | ||
return $this->description; | ||
} | ||
|
||
/** | ||
* Get the icon. | ||
* | ||
* @return ?string The icon of the channel. | ||
*/ | ||
public function get_icon(): ?string { | ||
return $this->icon; | ||
} | ||
} |
Oops, something went wrong.