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

namespace jobd;


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);
    }

}