aboutsummaryrefslogtreecommitdiff
path: root/src/Message.php
blob: d5df6abfd1a1d19b31fb8615581396d850ed080c (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;


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

}