Completed
Push — master ( 3395f4...940fa6 )
by Romain
13s
created

StringService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 72
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getExtensionRelativePath() 0 12 3
A getResourceRelativePath() 0 16 2
A sanitizeString() 0 10 2
A injectEnvironmentService() 0 4 1
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 Formz project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Service;
15
16
use Romm\Formz\Service\Traits\ExtendedFacadeInstanceTrait;
17
use TYPO3\CMS\Core\SingletonInterface;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
use TYPO3\CMS\Core\Utility\PathUtility;
20
use TYPO3\CMS\Extbase\Service\EnvironmentService;
21
22
class StringService implements SingletonInterface
23
{
24
    use ExtendedFacadeInstanceTrait;
25
26
    /**
27
     * @var EnvironmentService
28
     */
29
    protected $environmentService;
30
31
    /**
32
     * @param string $path If a string is given, it will be precessed by the extension relative path and returned.
33
     * @return string
34
     */
35
    public function getExtensionRelativePath($path)
36
    {
37
        $relativePath = ExtensionService::get()->getExtensionRelativePath();
38
39
        if ($this->environmentService->isEnvironmentInBackendMode()) {
40
            $relativePath = '../' . $relativePath;
41
        }
42
43
        return (null !== $path)
44
            ? $relativePath . $path
45
            : $relativePath;
46
    }
47
48
    /**
49
     * @param string $path
50
     * @return string
51
     */
52
    public function getResourceRelativePath($path)
53
    {
54
        $relativePath = rtrim(
55
            PathUtility::getRelativePath(
56
                GeneralUtility::getIndpEnv('TYPO3_DOCUMENT_ROOT'),
57
                GeneralUtility::getFileAbsFileName($path)
58
            ),
59
            '/'
60
        );
61
62
        if ($this->environmentService->isEnvironmentInBackendMode()) {
63
            $relativePath = '../' . $relativePath;
64
        }
65
66
        return $relativePath;
67
    }
68
69
    /**
70
     * Sanitizes a string: lower case with dash separation.
71
     *
72
     * @param string $string
73
     * @return string
74
     */
75
    public function sanitizeString($string)
76
    {
77
        $string = str_replace('_', '-', GeneralUtility::camelCaseToLowerCaseUnderscored($string));
78
79
        while (strpos($string, '--')) {
80
            $string = str_replace('--', '-', $string);
81
        }
82
83
        return $string;
84
    }
85
86
    /**
87
     * @param EnvironmentService $environmentService
88
     */
89
    public function injectEnvironmentService(EnvironmentService $environmentService)
90
    {
91
        $this->environmentService = $environmentService;
92
    }
93
}
94