1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EloquentJs\ScriptGenerator\Model; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Config\Repository; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
|
8
|
|
|
class Inspector |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $excludeScopes = ['scopeScope', 'scopeEloquentJs']; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Repository |
17
|
|
|
*/ |
18
|
|
|
protected $config; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Create a new Inspector instance. |
22
|
|
|
* |
23
|
|
|
* @param Repository $config |
24
|
|
|
*/ |
25
|
|
|
public function __construct(Repository $config) |
26
|
|
|
{ |
27
|
|
|
$this->config = $config; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Inspect a model class and return its metadata. |
32
|
|
|
* |
33
|
|
|
* @param Model $instance |
34
|
|
|
* @return Metadata |
35
|
|
|
*/ |
36
|
|
|
public function inspect(Model $instance) |
37
|
|
|
{ |
38
|
|
|
return new Metadata( |
39
|
|
|
class_basename($instance), |
40
|
|
|
$this->findEndpoint($instance), |
41
|
|
|
$this->findDateColumns($instance), |
42
|
|
|
$this->findScopeMethods($instance) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Find the endpoint for this model. |
48
|
|
|
* |
49
|
|
|
* @param Model $instance |
50
|
|
|
* @return string|null |
51
|
|
|
*/ |
52
|
|
|
protected function findEndpoint(Model $instance) |
53
|
|
|
{ |
54
|
|
|
if ($instance->getEndpoint()) { |
55
|
|
|
return $instance->getEndpoint(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->readModelConfig($instance, 'endpoint'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get any additional date columns for this model. |
63
|
|
|
* |
64
|
|
|
* @param Model $instance |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
protected function findDateColumns(Model $instance) |
68
|
|
|
{ |
69
|
|
|
return array_values( |
70
|
|
|
array_diff($instance->getDates(), ['created_at', 'updated_at', 'deleted_at']) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the scope methods for this model with 'scope' prefix removed. |
76
|
|
|
* |
77
|
|
|
* @param Model $instance |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
protected function findScopeMethods(Model $instance) |
81
|
|
|
{ |
82
|
|
|
return array_map( |
83
|
|
|
function($method) { |
84
|
|
|
return lcfirst(substr($method, 5)); |
85
|
|
|
}, |
86
|
|
|
array_values( |
87
|
|
|
array_filter( |
88
|
|
|
get_class_methods($instance), |
89
|
|
|
function($method) { |
90
|
|
|
return substr($method, 0, 5) === 'scope' and ! in_array($method, $this->excludeScopes); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
) |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Read from a model config value from the eloquentjs.php config file. |
99
|
|
|
* |
100
|
|
|
* @param Model $instance |
101
|
|
|
* @param string $key |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
|
|
protected function readModelConfig(Model $instance, $key) |
105
|
|
|
{ |
106
|
|
|
$className = get_class($instance); |
107
|
|
|
|
108
|
|
|
return $this->config->get("eloquentjs.generator.{$className}.{$key}"); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.