Skip to content

Commit

Permalink
feature: add initial model classes
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhooks committed Apr 20, 2023
1 parent 4739646 commit 7d4a4b7
Show file tree
Hide file tree
Showing 6 changed files with 821 additions and 1 deletion.
60 changes: 60 additions & 0 deletions includes/helper/class-serde.php
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;
}
}
135 changes: 135 additions & 0 deletions includes/model/class-channel.php
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;
}
}
Loading

0 comments on commit 7d4a4b7

Please sign in to comment.