|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Records\Traits\HasModelName; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Trait HasModelNameRecordsTrait |
|
7
|
|
|
* @package Nip\Records\Traits\HasModelName |
|
8
|
|
|
*/ |
|
9
|
|
|
trait HasModelNameRecordsTrait |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Model class name |
|
14
|
|
|
* @var null|string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $model = null; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var null|string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $modelNamespacePath = null; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
10 |
|
public function getModelNamespacePath() |
|
27
|
|
|
{ |
|
28
|
10 |
|
if ($this->modelNamespacePath == null) { |
|
|
|
|
|
|
29
|
10 |
|
$this->initModelNamespacePath(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
10 |
|
return $this->modelNamespacePath; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
10 |
|
public function initModelNamespacePath() |
|
36
|
|
|
{ |
|
37
|
10 |
|
if ($this->isNamespaced()) { |
|
|
|
|
|
|
38
|
10 |
|
$path = $this->generateModelNamespacePathFromClassName() . '\\'; |
|
39
|
|
|
} else { |
|
40
|
|
|
$controller = $this->generateControllerGeneric(); |
|
|
|
|
|
|
41
|
|
|
$path = inflector()->classify($controller) . '\\'; |
|
42
|
|
|
} |
|
43
|
10 |
|
$this->modelNamespacePath = $path; |
|
44
|
10 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
10 |
|
protected function generateModelNamespacePathFromClassName() |
|
50
|
|
|
{ |
|
51
|
10 |
|
$className = $this->getClassName(); |
|
|
|
|
|
|
52
|
10 |
|
$rootNamespace = $this->getRootNamespace(); |
|
|
|
|
|
|
53
|
10 |
|
$path = str_replace($rootNamespace, '', $className); |
|
54
|
|
|
|
|
55
|
10 |
|
$nsParts = explode('\\', $path); |
|
56
|
10 |
|
array_pop($nsParts); |
|
57
|
|
|
|
|
58
|
10 |
|
return implode($nsParts, '\\'); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
6 |
|
public function getModel() |
|
65
|
|
|
{ |
|
66
|
6 |
|
if ($this->model == null) { |
|
|
|
|
|
|
67
|
2 |
|
$this->inflectModel(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
6 |
|
return $this->model; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param null $model |
|
|
|
|
|
|
75
|
|
|
*/ |
|
76
|
2 |
|
public function setModel($model) |
|
77
|
|
|
{ |
|
78
|
2 |
|
$this->model = $model; |
|
79
|
2 |
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
protected function inflectModel() |
|
82
|
|
|
{ |
|
83
|
2 |
|
$class = $this->getClassName(); |
|
84
|
2 |
|
$this->model = $this->generateModelClass($class); |
|
85
|
2 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $class |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
3 |
|
public function generateModelClass($class = null) |
|
92
|
|
|
{ |
|
93
|
3 |
|
$class = $class ? $class : get_class($this); |
|
94
|
|
|
|
|
95
|
3 |
|
if (strpos($class, '\\')) { |
|
96
|
3 |
|
$nsParts = explode('\\', $class); |
|
97
|
3 |
|
$class = array_pop($nsParts); |
|
98
|
|
|
|
|
99
|
3 |
|
if ($class == 'Table') { |
|
100
|
1 |
|
$class = 'Row'; |
|
101
|
|
|
} else { |
|
102
|
2 |
|
$class = ucfirst(inflector()->singularize($class)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
3 |
|
return implode($nsParts, '\\') . '\\' . $class; |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
return ucfirst(inflector()->singularize($class)); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|