[TASK] Re-work/simplify copyright header in PHP files - Part 3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / StringUtility.php
1 <?php
2 namespace 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 * Class with helper functions for string handling
18 *
19 * @author Susanne Moog <typo3@susanne-moog.de>
20 * @author Markus Klein <klein.t3@mfc-linz.at>
21 */
22 class StringUtility {
23
24 /**
25 * Returns TRUE if $haystack ends with $needle.
26 * The input string is not trimmed before and search
27 * is done case sensitive.
28 *
29 * @param string $haystack Full string to check
30 * @param string $needle Reference string which must be found as the "last part" of the full string
31 * @throws \InvalidArgumentException
32 * @return boolean TRUE if $needle was found to be equal to the last part of $str
33 * @deprecated since 6.3 - will be removed two versions later, use beginsWith() instead
34 */
35 static public function isLastPartOfString($haystack, $needle) {
36 GeneralUtility::logDeprecatedFunction();
37 // Sanitize $haystack and $needle
38 if (is_object($haystack) || (string)$haystack != $haystack || strlen($haystack) < 1) {
39 throw new \InvalidArgumentException(
40 '$haystack can not be interpreted as string or has no length',
41 1347135544
42 );
43 }
44 if (is_object($needle) || (string)$needle != $needle || strlen($needle) < 1) {
45 throw new \InvalidArgumentException(
46 '$needle can not be interpreted as string or has no length',
47 1347135545
48 );
49 }
50 $stringLength = strlen($haystack);
51 $needleLength = strlen($needle);
52 return strrpos((string) $haystack, (string) $needle, 0) === $stringLength - $needleLength;
53 }
54
55 /**
56 * Returns TRUE if the first part of $str matches the string $partStr
57 *
58 * @param string $haystack Full string to check
59 * @param string $needle Reference string which must be found as the "first part" of the full string
60 * @throws \InvalidArgumentException
61 * @return boolean TRUE if $needle was found to be equal to the first part of $haystack
62 */
63 static public function beginsWith($haystack, $needle) {
64 // Sanitize $haystack and $needle
65 if (is_object($haystack) || $haystack === NULL || (string)$haystack != $haystack) {
66 throw new \InvalidArgumentException(
67 '$haystack can not be interpreted as string',
68 1347135546
69 );
70 }
71 if (is_object($needle) || (string)$needle != $needle || strlen($needle) < 1) {
72 throw new \InvalidArgumentException(
73 '$needle can not be interpreted as string or has zero length',
74 1347135547
75 );
76 }
77 $haystack = (string)$haystack;
78 $needle = (string)$needle;
79 return $needle !== '' && strpos($haystack, $needle) === 0;
80 }
81
82 /**
83 * Returns TRUE if $haystack ends with $needle.
84 * The input string is not trimmed before and search is done case sensitive.
85 *
86 * @param string $haystack Full string to check
87 * @param string $needle Reference string which must be found as the "last part" of the full string
88 * @throws \InvalidArgumentException
89 * @return boolean TRUE if $needle was found to be equal to the last part of $haystack
90 */
91 static public function endsWith($haystack, $needle) {
92 // Sanitize $haystack and $needle
93 if (is_object($haystack) || $haystack === NULL || (string)$haystack != $haystack) {
94 throw new \InvalidArgumentException(
95 '$haystack can not be interpreted as string',
96 1347135544
97 );
98 }
99 if (is_object($needle) || (string)$needle != $needle || strlen($needle) < 1) {
100 throw new \InvalidArgumentException(
101 '$needle can not be interpreted as string or has no length',
102 1347135545
103 );
104 }
105 $haystackLength = strlen($haystack);
106 $needleLength = strlen($needle);
107 if (!$haystackLength || $needleLength > $haystackLength) {
108 return FALSE;
109 }
110 $position = strrpos((string)$haystack, (string)$needle);
111 return $position !== FALSE && $position === $haystackLength - $needleLength;
112 }
113
114 }