Issues (150)

src/Blocks/FormRequestModel.php (1 issue)

Severity
1
<?php
2
3
namespace SoliDry\Blocks;
4
5
use SoliDry\ApiGenerator;
6
use SoliDry\Types\ModelsInterface;
7
use SoliDry\Types\PhpInterface;
8
use SoliDry\Types\ApiInterface;
9
10
/**
11
 * @property ApiGenerator generator
12
 * @property string sourceCode
13
 */
14
abstract class FormRequestModel
15
{
16
    use ContentManager;
0 ignored issues
show
The trait SoliDry\Blocks\ContentManager requires some properties which are not provided by SoliDry\Blocks\FormRequestModel: $options, $version, $modulesDir, $className
Loading history...
17
18
    public const CHECK_MANY_BRACKETS = '[]';
19
20
    /**
21
     * @var array
22
     */
23
    private array $legalTypes = [
24
        ApiInterface::RAML_TYPE_DATETIME,
25
        ApiInterface::RAML_TYPE_STRING,
26
        ApiInterface::RAML_TYPE_INTEGER,
27
        ApiInterface::RAML_TYPE_BOOLEAN,
28
        ApiInterface::RAML_TYPE_ARRAY,
29
    ];
30
31
    /**
32
     * @var array
33
     */
34
    private array $excludedKeys = [
35
        ApiInterface::RAML_KEY_DESCRIPTION,
36
        ApiInterface::RAML_KEY_DEFAULT,
37
    ];
38
39
    protected function setPropertyFilters()
40
    {
41
        $attrCnt =
42
            count($this->generator->types[$this->generator->objectProps[ApiInterface::RAML_ATTRS]][ApiInterface::RAML_PROPS]);
43
        foreach($this->generator->types[$this->generator->objectProps[ApiInterface::RAML_ATTRS]]
44
                [ApiInterface::RAML_PROPS] as $attrKey => $attrVal)
45
        {
46
            --$attrCnt;
47
            // determine attr
48
            if(is_array($attrVal))
49
            {
50
                $this->setDescription($attrVal);
51
                $this->openRule($attrKey);
52
                $cnt = count($attrVal);
53
                $this->setFilters($attrVal, $cnt);
54
                $this->closeRule();
55
                if($attrCnt > 0)
56
                {
57
                    $this->sourceCode .= PHP_EOL;
58
                }
59
            }
60
        }
61
    }
62
63
    /**
64
     * @param array $attrVal
65
     * @param int $cnt
66
     */
67
    private function setFilters(array $attrVal, int $cnt)
68
    {
69
        foreach($attrVal as $k => $v)
70
        {
71
            --$cnt;
72
            if($k === ApiInterface::RAML_KEY_REQUIRED && (bool)$v === false)
73
            {
74
                continue;
75
            }
76
            $this->setRequired($k, $v);
77
            $this->setType($k, $v);
78
            $this->setEnum($k, $v);
79
            $this->setPattern($k, $v);
80
            $this->setMinMax($k, $v);
81
            if($cnt > 0 && in_array($k, $this->excludedKeys) === false)
82
            {
83
                $this->sourceCode .= PhpInterface::PIPE;
84
            }
85
        }
86
    }
87
88
    /**
89
     * @param string $k
90
     * @param mixed $v
91
     */
92
    private function setRequired(string $k, $v)
93
    {
94
        if($k === ApiInterface::RAML_KEY_REQUIRED && (bool)$v === true)
95
        {
96
            $this->sourceCode .= ApiInterface::RAML_KEY_REQUIRED;
97
        }
98
    }
99
100
    /**
101
     * @param string $k
102
     * @param mixed $v
103
     */
104
    private function setPattern(string $k, $v)
105
    {
106
        if($k === ApiInterface::RAML_PATTERN)
107
        {
108
            $this->sourceCode .= ModelsInterface::LARAVEL_FILTER_REGEX . PhpInterface::COLON . $v;
109
        }
110
    }
111
112
    /**
113
     * @param string $k
114
     * @param mixed $v
115
     */
116
    private function setEnum(string $k, $v)
117
    {
118
        if($k === ApiInterface::RAML_ENUM)
119
        {
120
            $this->sourceCode .= ModelsInterface::LARAVEL_FILTER_ENUM . PhpInterface::COLON . implode(PhpInterface::COMMA, $v);
121
        }
122
    }
123
124
    /**
125
     * @param string $k
126
     * @param mixed $v
127
     */
128
    private function setType(string $k, $v)
129
    {
130
        if($k === ApiInterface::RAML_TYPE && in_array($v, $this->legalTypes))
131
        {
132
            $this->sourceCode .= $v;
133
        }
134
    }
135
136
    /**
137
     * @param string $k
138
     * @param mixed $v
139
     */
140
    private function setMinMax(string $k, $v)
141
    {
142
        if($k === ApiInterface::RAML_STRING_MIN || $k === ApiInterface::RAML_INTEGER_MIN)
143
        {
144
            $this->sourceCode .= ModelsInterface::LARAVEL_FILTER_MIN . PhpInterface::COLON . $v;
145
        }
146
        else if($k === ApiInterface::RAML_STRING_MAX || $k === ApiInterface::RAML_INTEGER_MAX)
147
        {
148
            $this->sourceCode .= ModelsInterface::LARAVEL_FILTER_MAX . PhpInterface::COLON . $v;
149
        }
150
    }
151
}