dolibarr 25.0.0-alpha
NamingContract.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2026 ATM Consulting <support@atm-consulting.fr>
3 * Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
24require_once DOL_DOCUMENT_ROOT . '/core/lib/functions.lib.php';
25
37final class NamingContract
38{
40 public $moduleNameCase;
41
43 public $moduleNameLower;
44
46 public $moduleNameUpper;
47
49 public $objectNameCase;
50
52 public $objectNameLower;
53
55 public $objectNameUpper;
56
62 public function __construct(string $moduleName, string $objectName = '')
63 {
64 $this->moduleNameCase = dol_ucfirst($moduleName);
65 $this->moduleNameLower = dol_strtolower($moduleName);
66 $this->moduleNameUpper = dol_strtoupper($moduleName);
67
68 if ($objectName === '') {
69 $this->objectNameCase = '';
70 $this->objectNameLower = '';
71 $this->objectNameUpper = '';
72 return;
73 }
74
75 if (dol_strtolower($moduleName) === dol_strtolower($objectName)) {
76 throw new \InvalidArgumentException(
77 'Module and object names cannot be identical (case-insensitive match): "'
78 . $moduleName . '" vs "' . $objectName . '"'
79 );
80 }
81
82 $this->objectNameCase = dol_ucfirst($objectName);
83 $this->objectNameLower = dol_strtolower($objectName);
84 $this->objectNameUpper = dol_strtoupper($objectName);
85 }
86
96 public function getSubstitutionMap(): array
97 {
98 $map = [
99 'MYMODULE' => $this->moduleNameUpper,
100 'MyModule' => $this->moduleNameCase,
101 'My module' => $this->moduleNameCase,
102 'my module' => $this->moduleNameLower,
103 'Mon module' => $this->moduleNameCase,
104 'mon module' => $this->moduleNameLower,
105 'mymodule' => $this->moduleNameLower,
106 ];
107
108 if ($this->objectNameLower !== '') {
109 $map['MYOBJECT'] = $this->objectNameUpper;
110 $map['MyObject'] = $this->objectNameCase;
111 $map['My Object'] = $this->objectNameCase;
112 $map['my object'] = $this->objectNameLower;
113 $map['myobject'] = $this->objectNameLower;
114 }
115
116 return $map;
117 }
118
125 public function applyTo(string $content): string
126 {
127 $map = $this->getSubstitutionMap();
128 return str_replace(array_keys($map), array_values($map), $content);
129 }
130
140 public function applyToFilename(string $filename): string
141 {
142 $search = ['mymodule'];
143 $replace = [$this->moduleNameLower];
144
145 if ($this->objectNameLower !== '') {
146 $search[] = 'myobject';
147 $replace[] = $this->objectNameLower;
148 }
149
150 return str_replace($search, $replace, $filename);
151 }
152}
Immutable value object holding all case variants of a module/object pair.
getSubstitutionMap()
Returns the canonical, ordered substitution map.
applyTo(string $content)
Apply the canonical substitution map to a string (file content).
applyToFilename(string $filename)
Apply substitution to a filename (lowercase tokens only).
__construct(string $moduleName, string $objectName='')
dol_ucfirst($string, $encoding="UTF-8")
Convert first character of the first word of a string to upper.
dol_strtolower($string, $encoding="UTF-8")
Convert a string to lower.
dol_strtoupper($string, $encoding="UTF-8")
Convert a string to upper.