1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Core;
16:
17: use Cake\Utility\Inflector;
18:
19: /**
20: * Provides methods that allow other classes access to conventions based inflections.
21: */
22: trait ConventionsTrait
23: {
24:
25: /**
26: * Creates a fixture name
27: *
28: * @param string $name Model class name
29: * @return string Singular model key
30: */
31: protected function _fixtureName($name)
32: {
33: return Inflector::camelize($name);
34: }
35:
36: /**
37: * Creates the proper entity name (singular) for the specified name
38: *
39: * @param string $name Name
40: * @return string Camelized and plural model name
41: */
42: protected function _entityName($name)
43: {
44: return Inflector::singularize(Inflector::camelize($name));
45: }
46:
47: /**
48: * Creates the proper underscored model key for associations
49: *
50: * If the input contains a dot, assume that the right side is the real table name.
51: *
52: * @param string $name Model class name
53: * @return string Singular model key
54: */
55: protected function _modelKey($name)
56: {
57: list(, $name) = pluginSplit($name);
58:
59: return Inflector::underscore(Inflector::singularize($name)) . '_id';
60: }
61:
62: /**
63: * Creates the proper model name from a foreign key
64: *
65: * @param string $key Foreign key
66: * @return string Model name
67: */
68: protected function _modelNameFromKey($key)
69: {
70: $key = str_replace('_id', '', $key);
71:
72: return Inflector::camelize(Inflector::pluralize($key));
73: }
74:
75: /**
76: * Creates the singular name for use in views.
77: *
78: * @param string $name Name to use
79: * @return string Variable name
80: */
81: protected function _singularName($name)
82: {
83: return Inflector::variable(Inflector::singularize($name));
84: }
85:
86: /**
87: * Creates the plural variable name for views
88: *
89: * @param string $name Name to use
90: * @return string Plural name for views
91: */
92: protected function _variableName($name)
93: {
94: return Inflector::variable($name);
95: }
96:
97: /**
98: * Creates the singular human name used in views
99: *
100: * @param string $name Controller name
101: * @return string Singular human name
102: */
103: protected function _singularHumanName($name)
104: {
105: return Inflector::humanize(Inflector::underscore(Inflector::singularize($name)));
106: }
107:
108: /**
109: * Creates a camelized version of $name
110: *
111: * @param string $name name
112: * @return string Camelized name
113: */
114: protected function _camelize($name)
115: {
116: return Inflector::camelize($name);
117: }
118:
119: /**
120: * Creates the plural human name used in views
121: *
122: * @param string $name Controller name
123: * @return string Plural human name
124: */
125: protected function _pluralHumanName($name)
126: {
127: return Inflector::humanize(Inflector::underscore($name));
128: }
129:
130: /**
131: * Find the correct path for a plugin. Scans $pluginPaths for the plugin you want.
132: *
133: * @param string $pluginName Name of the plugin you want ie. DebugKit
134: * @return string path path to the correct plugin.
135: */
136: protected function _pluginPath($pluginName)
137: {
138: if (Plugin::isLoaded($pluginName)) {
139: return Plugin::path($pluginName);
140: }
141:
142: return current(App::path('Plugin')) . $pluginName . DIRECTORY_SEPARATOR;
143: }
144:
145: /**
146: * Return plugin's namespace
147: *
148: * @param string $pluginName Plugin name
149: * @return string Plugin's namespace
150: */
151: protected function _pluginNamespace($pluginName)
152: {
153: return str_replace('/', '\\', $pluginName);
154: }
155: }
156: