summaryrefslogtreecommitdiff
path: root/matrix-specification/Data/PushCondition.php
blob: ca94de99daaf6889927aa89a369bf00480e386eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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,
    ];
  }
}