diff options
Diffstat (limited to 'matrix-specification/Data')
| -rw-r--r-- | matrix-specification/Data/PushCondition.php | 56 | ||||
| -rw-r--r-- | matrix-specification/Data/PushRule.php | 32 | ||||
| -rw-r--r-- | matrix-specification/Data/Ruleset.php | 33 |
3 files changed, 121 insertions, 0 deletions
diff --git a/matrix-specification/Data/PushCondition.php b/matrix-specification/Data/PushCondition.php new file mode 100644 index 0000000..ca94de9 --- /dev/null +++ b/matrix-specification/Data/PushCondition.php @@ -0,0 +1,56 @@ +<?php + +namespace Matrix\Data; + +use Matrix\Enums\PushConditionKind; +use Matrix\Events\Event; + +class PushCondition implements \JsonSerializable +{ + /** + * @param Event[] $events + */ + public function __construct( + private PushConditionKind $kind, + private ?string $is = null, + private ?string $key = null, + private ?string $pattern = null, + private string|int|bool|null $value = null, + ) + { + if ($kind === PushConditionKind::ROOM_MEMBER_COUNT && is_null($is)) { + throw new \InvalidArgumentException("\"is\" is required for room_member_count conditions"); + } + + if ( + ( + $kind === PushConditionKind::EVENT_MATCH || + $kind === PushConditionKind::EVENT_PROPERTY_IS || + $kind === PushConditionKind::EVENT_PROPERTY_CONTAINS || + $kind === PushConditionKind::SENDER_NOTIFICATION_PERMISSION + ) + && is_null($key) + ) { + throw new \InvalidArgumentException("\"key\" is required for event_match, event_property_is and event_property_contains conditions"); + } + + if ($kind === PushConditionKind::EVENT_MATCH && is_null($pattern)) { + throw new \InvalidArgumentException("\"pattern\" is required for event_match conditions"); + } + + if (($kind === PushConditionKind::EVENT_PROPERTY_IS or $kind === PushConditionKind::EVENT_PROPERTY_CONTAINS) && is_null($value)) { + throw new \InvalidArgumentException("\"value\" is required for event_property_is and event_property_contains conditions"); + } + } + + public function jsonSerialize(): array + { + return [ + "is" => $this->is, + "key" => $this->key, + "kind" => $this->kind, + "pattern" => $this->pattern, + "value" => $this->value, + ]; + } +} diff --git a/matrix-specification/Data/PushRule.php b/matrix-specification/Data/PushRule.php new file mode 100644 index 0000000..2217a78 --- /dev/null +++ b/matrix-specification/Data/PushRule.php @@ -0,0 +1,32 @@ +<?php + +namespace Matrix\Data; + +class PushRule implements \JsonSerializable +{ + /** + * @param array<string|array> $actions + * @param PushCondition[] $conditions + */ + public function __construct( + private array $actions, + private bool $default, + private bool $enabled, + private string $ruleId, + private ?array $conditions = null, + private ?string $pattern = null, + ) + {} + + public function jsonSerialize(): array + { + return [ + "actions" => $this->actions, + "conditions" => $this->conditions, + "default" => $this->default, + "enabled" => $this->enabled, + "pattern" => $this->pattern, + "rule_id" => $this->ruleId, + ]; + } +} diff --git a/matrix-specification/Data/Ruleset.php b/matrix-specification/Data/Ruleset.php new file mode 100644 index 0000000..af75579 --- /dev/null +++ b/matrix-specification/Data/Ruleset.php @@ -0,0 +1,33 @@ +<?php + +namespace Matrix\Data; + +class Ruleset implements \JsonSerializable +{ + /** + * @param PushRule[] $content + * @param PushRule[] $override + * @param PushRule[] $room + * @param PushRule[] $sender + * @param PushRule[] $underride + */ + public function __construct( + private array $content, + private array $override, + private array $room, + private array $sender, + private array $underride, + ) + {} + + public function jsonSerialize(): array + { + return [ + "content" => $this->content, + "override" => $this->override, + "room" => $this->room, + "sender" => $this->sender, + "underride" => $this->underride, + ]; + } +} |
