|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scaffolder\Compilers\AngularJs; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\File; |
|
6
|
|
|
use Scaffolder\Compilers\AbstractCompiler; |
|
7
|
|
|
use Scaffolder\Compilers\Support\FileToCompile; |
|
8
|
|
|
use Scaffolder\Compilers\Support\PathParser; |
|
9
|
|
|
use Scaffolder\Support\Directory; |
|
10
|
|
|
use Scaffolder\Support\CamelCase; |
|
11
|
|
|
use stdClass ; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class TranslateCompiler extends AbstractCompiler |
|
15
|
|
|
{ |
|
16
|
|
|
protected $cachePrefix = 'translate_'; |
|
17
|
|
|
protected $stubFilename = 'Translate.js' ; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct($scaffolderConfig, $modelData = null) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->stubsDirectory = __DIR__ . '/../../../../stubs/AngularJs/'; |
|
22
|
|
|
parent::__construct($scaffolderConfig, $modelData); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Replace and store the Stub. |
|
27
|
|
|
* |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
public function replaceAndStore() |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
|
|
return $this->replaceColumns() |
|
|
|
|
|
|
34
|
|
|
->replaceArea() |
|
35
|
|
|
->store(new FileToCompile(false, $this->modelData->modelHash)); |
|
36
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* replace columns |
|
41
|
|
|
* |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
|
|
public function replaceArea(){ |
|
45
|
|
|
$this->stub = str_replace('{{singular}}', ucwords($this->modelData->tableName), $this->stub); |
|
46
|
|
|
$this->stub = str_replace('{{plural}}', ucwords(CamelCase::pluralize($this->modelData->tableName)), $this->stub); |
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* replace columns |
|
52
|
|
|
* |
|
53
|
|
|
* @return $this |
|
54
|
|
|
*/ |
|
55
|
|
|
public function replaceColumns(){ |
|
56
|
|
|
|
|
57
|
|
|
$columns = []; |
|
58
|
|
|
|
|
59
|
|
|
foreach ($this->modelData->fields as $field) |
|
60
|
|
|
{ |
|
61
|
|
|
array_push($columns, "\t\t".sprintf('"%s" : "%s"', $field->name, CamelCase::convertToCamelCase($field->name) )); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->stub = str_replace('{{columns}}', join(",\n", $columns), $this->stub); |
|
65
|
|
|
|
|
66
|
|
|
return $this ; |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get output filename |
|
72
|
|
|
* |
|
73
|
|
|
* |
|
74
|
|
|
* @return $this |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function getOutputFilename() |
|
77
|
|
|
{ |
|
78
|
|
|
$folder = PathParser::parse($this->scaffolderConfig->generator->paths->pages).$this->modelData->tableName.'/i18n/' ; |
|
79
|
|
|
|
|
80
|
|
|
Directory::createIfNotExists($folder, 0755, true); |
|
81
|
|
|
|
|
82
|
|
|
return $folder . 'en.json'; |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.