summaryrefslogtreecommitdiff
path: root/src/messages/Message.php
blob: df7464e71be32f07c9aec952ceb74ecefe2894c7 (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
<?php

namespace jobd\messages;


abstract class Message {

    const REQUEST = 0;
    const RESPONSE = 1;
    const PING = 2;
    const PONG = 3;

    protected $type;

    /**
     * Message constructor.
     * @param int $type
     */
    public function __construct(int $type)
    {
        $this->type = $type;
    }

    /**
     * @return array
     */
    abstract protected function getContent(): array;

    /**
     * @return string
     */
    public function serialize(): string
    {
        $data = [$this->type];
        $content = $this->getContent();

        if (!empty($content))
            $data[] = $content;

        return json_encode($data);
    }

}