Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(mvp): Stage 2 - add initial model classes #289

Merged
merged 3 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "wordpress/wp-feature-notifications",
"description": "Notifications for WordPress (Feature Plugin)",
"type": "wordpress-plugin",
"require": {
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.6",
"yoast/phpunit-polyfills": "1.0.5",
Expand Down
18 changes: 10 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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;
}
}
141 changes: 141 additions & 0 deletions includes/model/class-channel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php
/**
* Notifications API:Channel class
*
* @package wordpress/wp-feature-notifications
*/

namespace WP\Notifications\Model;

use JsonSerializable;

/**
* Class representing a notification channel.
*
* @see \WP\Notifications\register_channel()
*/
class Channel implements JsonSerializable {

// Required properties

/**
* Name, including namespace, of the channel.
*/
protected ?string $name;

/**
* Human-readable label of the channel.
*/
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 = null,
$title = null,
$context = null,
$description = null,
$icon = null
) {
// Required properties

$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 array Data which can be serialized by json_encode, which is a
* value of any type other than a resource.
*/
public function jsonSerialize() {
return array(
'context' => $this->context,
'description' => $this->description,
'icon' => $this->icon,
'name' => $this->name,
'title' => $this->title,
);
}

/**
* 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