Issues (114)

src/Foundation/DirectiveParser.php (2 issues)

1
<?php
2
3
namespace Maestriam\Samurai\Foundation;
4
5
use Maestriam\Samurai\Entities\Directive;
6
use Maestriam\Samurai\Entities\Theme;
7
use Maestriam\Samurai\Exceptions\InvalidTypeDirectiveException;
8
use Maestriam\Samurai\Contracts\Foundation\DirectiveParserContract;
9
use Maestriam\Samurai\Entities\Component;
10
use Maestriam\Samurai\Entities\Includer;
11
12
class DirectiveParser implements DirectiveParserContract
13
{
14
15
   
16
    private SyntaxValidator $validator;
17
18
    private Theme $theme;
19
20
    private string $type;
21
22
    private string $sentence;
23
24
    private string $relative;
25
26
    private string $name;
27
28
    public function __construct(Theme $theme)
29
    {
30
        $this->setValidator()->setTheme($theme);
31
    }
32
33
    /**
34
     * Recupera todas as informações de um diretiva atráves do seu caminho completo.  
35
     *
36
     * @param  string $file
37
     * @return void
38
     */
39
    public function parse(string $file) : DirectiveParser
40
    {
41
        $this->setFile($file)
42
            ->setRelative()
43
            ->setNameAndType()
44
            ->setSentence();
45
            
46
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Maestriam\Samurai\Foundation\DirectiveParser which is incompatible with the documented return type void.
Loading history...
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function toObject() : object
53
    {
54
        return (object) [
55
            'relative' => $this->relative,
56
            'sentence' => $this->sentence,
57
            'name'     => $this->name,
58
            'type'     => $this->type
59
        ];
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function toDirective() : Component|Includer
66
    {
67
        return ($this->type == 'component') ? $this->toComponent() : $this->toIncluder();
68
    }
69
70
    /**
71
     * Retorna a instância de uma diretiva includer, de acordo com o tema e a sentença.  
72
     *
73
     * @return Includer
74
     */
75
    private function toIncluder() : Includer
76
    {
77
        return new Includer($this->theme(), $this->sentence);
78
    }
79
80
    /**
81
     * Retorna a instância de uma diretiva component, de acordo com o tema e a sentença.  
82
     *
83
     * @return Component
84
     */
85
    private function toComponent() : Component
86
    {
87
        return new Component($this->theme(), $this->sentence);
88
    }
89
90
    /**
91
     * Retorna o tema específico
92
     *
93
     * @return Theme
94
     */
95
    private function theme() : Theme
96
    {
97
        return $this->theme;
98
    }
99
100
    /**
101
     * Define o tema que será utilizado para manipulação
102
     *
103
     * @param  Theme $theme
104
     * @return DirectiveParser
105
     */
106
    private function setTheme(Theme $theme) : DirectiveParser
107
    {
108
        $this->theme = $theme;
109
        return $this;
110
    }
111
112
    /**
113
     * Retorna o validador de nomenclatura de entidades do pacote.  
114
     *
115
     * @return SyntaxValidator
116
     */
117
    private function validator() : SyntaxValidator
118
    {        
119
        return $this->validator;
120
    }
121
    
122
    /**
123
     * Define a instancia do validador
124
     *
125
     * @return DirectiveParser
126
     */
127
    private function setValidator() : DirectiveParser
128
    {
129
        $this->validator = new SyntaxValidator();
130
        return $this;
131
    }
132
133
    /**
134
     * Define o arquivo que será interpretado.  
135
     *
136
     * @param  string $file
137
     * @return DirectiveParser
138
     */
139
    private function setFile(string $file) : DirectiveParser
140
    {
141
        $this->file = $file;
0 ignored issues
show
Bug Best Practice introduced by
The property file does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
142
        return $this;
143
    }
144
145
    /**
146
     * Extrai o caminho relativo da diretiva através do caminho completo 
147
     * do arquivo.  
148
     *
149
     * @return DirectiveParser
150
     */
151
    private function setRelative() : DirectiveParser
152
    {
153
        $source = $this->theme()->paths()->source();
154
155
        $this->relative = str_replace($source, '', $this->file);
156
        return $this;
157
    }
158
159
    /**
160
     * Extrai o tipo de diretiva atráves do caminho absoluto.  
161
     *
162
     * @return DirectiveParser
163
     */
164
    private function setNameAndType() : DirectiveParser
165
    {
166
        $file = str_replace('.php', '', $this->relative);
167
        $info = pathinfo($file);
168
        $part = explode('-', $info['filename']);
169
170
        $this->type = array_pop($part);
171
172
        if (! $this->validator()->type($this->type)) {
173
            throw new InvalidTypeDirectiveException($this->type);
174
        }
175
176
        $this->name = implode('-', $part);
177
        
178
        return $this;
179
    }
180
181
    /**
182
     * Extrai a sentença da diretiva.  
183
     *
184
     * @return DirectiveParser
185
     */
186
    private function setSentence() : DirectiveParser
187
    {
188
        $search = '-' . $this->type. '.blade';
189
190
        $sentence = str_replace($search, '', $this->relative);
191
        $sentence = str_replace('.php', '', $sentence);
192
193
        $this->sentence = str_replace('\\', '/', $sentence);
194
195
        return $this;
196
    }
197
}
198