Skip to content

Commit

Permalink
refactor: to and from methods on notification class
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmikita committed May 10, 2024
1 parent 4358f3f commit f28d22f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 38 deletions.
19 changes: 17 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,27 @@ parameters:
path: compat/src-deprecated/Adapter/JSON.php

-
message: "#^Parameter \\#1 \\$postarr of function wp_insert_post expects array\\{ID\\?\\: int, post_author\\?\\: int, post_date\\?\\: string, post_date_gmt\\?\\: string, post_content\\?\\: string, post_content_filtered\\?\\: string, post_title\\?\\: string, post_excerpt\\?\\: string, \\.\\.\\.\\}, array\\{ID\\: int, post_content\\: mixed, post_type\\: string, post_title\\: mixed, post_name\\: mixed, post_status\\: 'draft'\\|'publish'\\} given\\.$#"
message: "#^Offset 'enabled' does not exist on array\\{hash\\?\\: string, title\\?\\: string, trigger\\?\\: BracketSpace\\\\Notification\\\\Interfaces\\\\Triggerable, carriers\\?\\: array\\<string, BracketSpace\\\\Notification\\\\Interfaces\\\\Sendable\\>, enabled\\?\\: bool, extras\\?\\: array\\<string, array\\|bool\\|float\\|int\\|string\\>, version\\?\\: int\\}\\|null\\.$#"
count: 1
path: compat/src-deprecated/Adapter/WordPress.php

-
message: "#^Parameter \\#2 \\$subject of function preg_match expects string, mixed given\\.$#"
message: "#^Offset 'hash' does not exist on array\\{hash\\?\\: string, title\\?\\: string, trigger\\?\\: BracketSpace\\\\Notification\\\\Interfaces\\\\Triggerable, carriers\\?\\: array\\<string, BracketSpace\\\\Notification\\\\Interfaces\\\\Sendable\\>, enabled\\?\\: bool, extras\\?\\: array\\<string, array\\|bool\\|float\\|int\\|string\\>, version\\?\\: int\\}\\|null\\.$#"
count: 2
path: compat/src-deprecated/Adapter/WordPress.php

-
message: "#^Offset 'title' does not exist on array\\{hash\\?\\: string, title\\?\\: string, trigger\\?\\: BracketSpace\\\\Notification\\\\Interfaces\\\\Triggerable, carriers\\?\\: array\\<string, BracketSpace\\\\Notification\\\\Interfaces\\\\Sendable\\>, enabled\\?\\: bool, extras\\?\\: array\\<string, array\\|bool\\|float\\|int\\|string\\>, version\\?\\: int\\}\\|null\\.$#"
count: 1
path: compat/src-deprecated/Adapter/WordPress.php

-
message: "#^Parameter \\#1 \\$data of function BracketSpace\\\\Notification\\\\convertNotificationData expects array, array\\<string, array\\<string, array\\|bool\\|BracketSpace\\\\Notification\\\\Interfaces\\\\Sendable\\|float\\|int\\|string\\>\\|bool\\|BracketSpace\\\\Notification\\\\Interfaces\\\\Triggerable\\|int\\|string\\>\\|null given\\.$#"
count: 1
path: compat/src-deprecated/Adapter/WordPress.php

-
message: "#^Parameter \\#1 \\$postarr of function wp_insert_post expects array\\{ID\\?\\: int, post_author\\?\\: int, post_date\\?\\: string, post_date_gmt\\?\\: string, post_content\\?\\: string, post_content_filtered\\?\\: string, post_title\\?\\: string, post_excerpt\\?\\: string, \\.\\.\\.\\}, array\\{ID\\: int, post_content\\: mixed, post_type\\: string, post_title\\: string, post_name\\: string, post_status\\: 'draft'\\|'publish'\\} given\\.$#"
count: 1
path: compat/src-deprecated/Adapter/WordPress.php

Expand Down
82 changes: 48 additions & 34 deletions src/Core/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,39 +189,6 @@ public function setup($data = [])

}

/**
* Dumps the object to array
*
* @param bool $onlyEnabledCarriers If only enabled Carriers should be saved.
* @return array<mixed>
* @since 6.0.0
*/
public function toArray($onlyEnabledCarriers = false)
{
$carriers = [];
$_carriers = $onlyEnabledCarriers
? $this->getEnabledCarriers()
: $this->getCarriers();
foreach ($_carriers as $carrierSlug => $carrier) {
$carriers[$carrierSlug] = $carrier->getData();
}

$trigger = $this->getTrigger();

return [
'hash' => $this->getHash(),
'title' => $this->getTitle(),
'trigger' => $trigger
? $trigger->getSlug()
: '',
'carriers' => $carriers,
'enabled' => $this->isEnabled(),
'extras' => $this->getExtras(),
'version' => $this->getVersion(),
];

}

/**
* Checks if enabled
* Alias for `get_enabled()` method
Expand Down Expand Up @@ -613,12 +580,59 @@ public function setSourcePostId($postId)
$this->sourcePostId = $postId;
}

/**
* Dumps the object to array
*
* @since 6.0.0
* @deprecated [Next] Use Converter instead, via $notification->to('array') method
* @param bool $onlyEnabledCarriers If only enabled Carriers should be saved.
* @return NotificationData|null
*/
public function toArray($onlyEnabledCarriers = false)
{
_deprecated_function(__METHOD__, '[Next]', 'Notification::to');

$array = $this->to('array', ['onlyEnabledCarriers' => $onlyEnabledCarriers]);

if (! is_array($array)) {
return null;
}

return $array;
}

/**
* Creates Notification from a specific representation
*
* @since [Next]
* @throws \Exception When no Notification object comes back from the filter
* @param string $type The type of representation, ie. array or json
* @param string|array<mixed,mixed> $data The notification representation
* @return self
*/
public static function from(string $type, $data): Notification
{
$filterName = sprintf('notification/from/%s', $type);
$notification = apply_filters($filterName, $data);

if (! $notification instanceof self) {
throw new \Exception(
sprintf(
"The %s filter didn't return a Notification object. Make sure the filter is correctly hooked",
$filterName
)
);
}

return $notification;
}

/**
* Converts the notification to another type of representation.
*
* @since [Next]
* @param string $type The type of representation, ie. array or json
* @param array<string|int,mixed> $config The additional configuration of the adapter
* @param array<string|int,mixed> $config The additional configuration of the converter
* @return mixed
*/
public function to(string $type, array $config = [])
Expand Down
2 changes: 1 addition & 1 deletion src/Repository/Converter/ArrayConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ArrayConverter implements Convertable
* @filter notification/from/json
*
* @since [Next]
* @param array $data The notification representation
* @param NotificationData $data The notification representation
* @return Notification
*/
public function from($data): Notification
Expand Down
2 changes: 1 addition & 1 deletion src/Repository/Converter/JsonConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function to(Notification $notification, array $config = [])
? JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
: $config['jsonOptions'];

$data = $notification->toArray($onlyEnabledCarriers);
$data = $notification->to('array', ['onlyEnabledCarriers' => $onlyEnabledCarriers]);

return wp_json_encode($data, $jsonOptions);
}
Expand Down

0 comments on commit f28d22f

Please sign in to comment.