aboutsummaryrefslogtreecommitdiffstats
path: root/lua/alpha/setter_getter_template.lua
blob: fc2099f81ba7772f758e242c13fed295f15a8a30 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
local utils = require("alpha.utils")

local M = {}

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

    return ""
end

local function getReturnTypes(nullable, types) if #types > 0 then
        return string.format(": %s%s", isNullable(nullable), table.concat(types, "|"))
    end
    return ""
end

local function getTypeHint(nullable, types) if #types > 0 then
        return string.format("%s%s ", isNullable(nullable), table.concat(types, "|"))
    end
    return ""
end


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

    return t
end

M.getGetter = function(property, nullable, types, have_dock_block)
    local comment = ""
    if have_dock_block and #types > 0 then
        comment = string.format(
            [[
    /**
     * @return %s
     */
]],
            table.concat(types, "|")
        )
    end

    local fn = string.format(
        [[
    public function get%s()%s
    {
        return $this->%s;
    }
    ]]   ,
        utils.ucfirst(property),
        getReturnTypes(nullable, types),
        property
    )

    return toTable(comment .. fn)
end

M.getSetter = function(property, nullable, types, have_dock_block)

    local comment = ""
    if have_dock_block and #types > 0 then
        comment = string.format(
            [[
    /**
     * @param %s $%s
     *
     * @return self
     */
]],
            table.concat(types, "|"),
            property
        )
    end
    local fn = string.format(
        [[
    public function set%s(%s$%s): self
    {
        $this->%s = $%s;

        return $this;
    }
    ]]   ,
        utils.ucfirst(property),
        getTypeHint(nullable, types),
        property,
        property,
        property
    )

    return toTable(comment .. fn)
end

return M