aboutsummaryrefslogtreecommitdiffstats
path: root/lua/alpha/setter_getter_template.lua
blob: f4a6f81dd728322382a1efee70c5832bcd68d7a9 (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
57
58
local utils = require("alpha.utils")

local M = {}

local function isNullable(nullable)
    if nullable then
        return "?"
    end

    return ""
end

local function toTable(fn)
    local t = {}
    for s in fn:gmatch("[^\r\n]+") do
        table.insert(t, s)
    end

    return t
end

M.getGetter = function(property, nullable, types)
    local fn = string.format([[
    public function get%s(): %s%s
    {
        return $this->%s;
    }
    ]],
        utils.ucfirst(property),
        isNullable(nullable),
        table.concat(types, "|"),
        property
    )

    return toTable(fn)
end

M.getSetter = function(property, nullable, types)
    local fn = string.format([[
    public function set%s(%s%s $%s): self
    {
        $this->%s = $%s;

        return $this;
    }
    ]],
        utils.ucfirst(property),
        isNullable(nullable),
        table.concat(types, "|"),
        property,
        property,
        property
    )

    return toTable(fn)
end

return M