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