1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Namefy\Strategies; |
4
|
|
|
|
5
|
|
|
use ByTIC\Namefy\Name; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ByTICStrategy |
9
|
|
|
* @package ByTIC\Namefy\Strategies |
10
|
|
|
*/ |
11
|
|
|
class ByTICStrategy extends AbstractStrategy |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param $slug |
15
|
|
|
* @param Name $name |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
|
|
public function fromRepository($slug, Name $name) |
19
|
|
|
{ |
20
|
|
|
$slug = $this->cleanModelNamespace($slug); |
21
|
|
|
|
22
|
|
|
return inflector()->unclassify($slug); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @inheritDoc |
27
|
|
|
*/ |
28
|
|
|
public function fromModel($slug, Name $name): string |
29
|
|
|
{ |
30
|
|
|
$slug = $this->cleanModelNamespace($slug); |
31
|
|
|
$slug = inflector()->unclassify($slug); |
32
|
|
|
return inflector()->pluralize($slug); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function fromController($slug, Name $name) |
36
|
|
|
{ |
37
|
|
|
// TODO: Implement fromController() method. |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritDoc |
42
|
|
|
*/ |
43
|
|
|
public function toRepository(Name $name): string |
44
|
|
|
{ |
45
|
|
|
// TODO: Implement toRepository() method. |
46
|
|
|
} |
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritDoc |
50
|
|
|
*/ |
51
|
|
|
public function toModel(Name $name): string |
52
|
|
|
{ |
53
|
|
|
return $name; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritDoc |
58
|
|
|
*/ |
59
|
|
|
public function toController(Name $name): string |
60
|
|
|
{ |
61
|
|
|
return $name->resource(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function cleanModelNamespace($name): string |
65
|
|
|
{ |
66
|
|
|
if (strpos($name, '\\') === false) { |
67
|
|
|
return $name; |
68
|
|
|
} |
69
|
|
|
if (method_exists($name, 'getRootNamespace')) { |
70
|
|
|
$name = str_replace((new $name())->getRootNamespace(), '', $name); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$name = trim($name, '\\'); |
74
|
|
|
$parts = explode('\\', $name); |
75
|
|
|
array_pop($parts); |
76
|
|
|
|
77
|
|
|
$name = implode('\\', $parts); |
78
|
|
|
return $name; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: