[BUGFIX] Prematurely end data array processing on invalid item
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / ClassNamingUtility.php
1 <?php
2
3 /*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16 namespace TYPO3\CMS\Core\Utility;
17
18 /**
19 * Several functions related to naming and conversions of names
20 * such as translation between Repository and Model names or
21 * exploding an objectControllerName into pieces
22 */
23 class ClassNamingUtility
24 {
25 /**
26 * Translates a model name to an appropriate repository name
27 * e.g. Tx_Extbase_Domain_Model_Foo to Tx_Extbase_Domain_Repository_FooRepository
28 * or \TYPO3\CMS\Extbase\Domain\Model\Foo to \TYPO3\CMS\Extbase\Domain\Repository\FooRepository
29 *
30 * @param string $modelName Name of the model to translate
31 * @return string Name of the repository
32 */
33 public static function translateModelNameToRepositoryName($modelName)
34 {
35 return str_replace(
36 '\\Domain\\Model',
37 '\\Domain\\Repository',
38 $modelName
39 ) . 'Repository';
40 }
41
42 /**
43 * Translates a repository name to an appropriate model name
44 * e.g. Tx_Extbase_Domain_Repository_FooRepository to Tx_Extbase_Domain_Model_Foo
45 * or \TYPO3\CMS\Extbase\Domain\Repository\FooRepository to \TYPO3\CMS\Extbase\Domain\Model\Foo
46 *
47 * @param string $repositoryName Name of the repository to translate
48 * @return string Name of the model
49 */
50 public static function translateRepositoryNameToModelName($repositoryName)
51 {
52 return preg_replace(
53 ['/\\\\Domain\\\\Repository/', '/Repository$/'],
54 ['\\Domain\\Model', ''],
55 $repositoryName
56 );
57 }
58
59 /**
60 * Explodes a controllerObjectName like \Vendor\Ext\Controller\FooController
61 * into several pieces like vendorName, extensionName, subpackageKey and controllerName
62 *
63 * @param string $controllerObjectName The controller name to be exploded
64 * @return array An array of controllerObjectName pieces
65 */
66 public static function explodeObjectControllerName($controllerObjectName)
67 {
68 $matches = [];
69
70 if (strpos($controllerObjectName, 'TYPO3\\CMS') === 0) {
71 $extensionName = '^(?P<vendorName>[^\\\\]+\\\[^\\\\]+)\\\(?P<extensionName>[^\\\\]+)';
72 } else {
73 $extensionName = '^(?P<vendorName>[^\\\\]+)\\\\(?P<extensionName>[^\\\\]+)';
74 }
75
76 preg_match(
77 '/' . $extensionName . '\\\\(Controller|Command|(?P<subpackageKey>.+)\\\\Controller)\\\\(?P<controllerName>[a-z\\\\]+)Controller$/ix',
78 $controllerObjectName,
79 $matches
80 );
81
82 return array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY);
83 }
84 }