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: * Creates a fixture name
26: *
27: * @param string $name Model class name
28: * @return string Singular model key
29: */
30: protected function _fixtureName($name)
31: {
32: return Inflector::camelize($name);
33: }
34:
35: /**
36: * Creates the proper entity name (singular) for the specified name
37: *
38: * @param string $name Name
39: * @return string Camelized and plural model name
40: */
41: protected function _entityName($name)
42: {
43: return Inflector::singularize(Inflector::camelize($name));
44: }
45:
46: /**
47: * Creates the proper underscored model key for associations
48: *
49: * If the input contains a dot, assume that the right side is the real table name.
50: *
51: * @param string $name Model class name
52: * @return string Singular model key
53: */
54: protected function _modelKey($name)
55: {
56: list(, $name) = pluginSplit($name);
57:
58: return Inflector::underscore(Inflector::singularize($name)) . '_id';
59: }
60:
61: /**
62: * Creates the proper model name from a foreign key
63: *
64: * @param string $key Foreign key
65: * @return string Model name
66: */
67: protected function _modelNameFromKey($key)
68: {
69: $key = str_replace('_id', '', $key);
70:
71: return Inflector::camelize(Inflector::pluralize($key));
72: }
73:
74: /**
75: * Creates the singular name for use in views.
76: *
77: * @param string $name Name to use
78: * @return string Variable name
79: */
80: protected function _singularName($name)
81: {
82: return Inflector::variable(Inflector::singularize($name));
83: }
84:
85: /**
86: * Creates the plural variable name for views
87: *
88: * @param string $name Name to use
89: * @return string Plural name for views
90: */
91: protected function _variableName($name)
92: {
93: return Inflector::variable($name);
94: }
95:
96: /**
97: * Creates the singular human name used in views
98: *
99: * @param string $name Controller name
100: * @return string Singular human name
101: */
102: protected function _singularHumanName($name)
103: {
104: return Inflector::humanize(Inflector::underscore(Inflector::singularize($name)));
105: }
106:
107: /**
108: * Creates a camelized version of $name
109: *
110: * @param string $name name
111: * @return string Camelized name
112: */
113: protected function _camelize($name)
114: {
115: return Inflector::camelize($name);
116: }
117:
118: /**
119: * Creates the plural human name used in views
120: *
121: * @param string $name Controller name
122: * @return string Plural human name
123: */
124: protected function _pluralHumanName($name)
125: {
126: return Inflector::humanize(Inflector::underscore($name));
127: }
128:
129: /**
130: * Find the correct path for a plugin. Scans $pluginPaths for the plugin you want.
131: *
132: * @param string $pluginName Name of the plugin you want ie. DebugKit
133: * @return string path path to the correct plugin.
134: */
135: protected function _pluginPath($pluginName)
136: {
137: if (Plugin::isLoaded($pluginName)) {
138: return Plugin::path($pluginName);
139: }
140:
141: return current(App::path('Plugin')) . $pluginName . DIRECTORY_SEPARATOR;
142: }
143:
144: /**
145: * Return plugin's namespace
146: *
147: * @param string $pluginName Plugin name
148: * @return string Plugin's namespace
149: */
150: protected function _pluginNamespace($pluginName)
151: {
152: return str_replace('/', '\\', $pluginName);
153: }
154: }
155: