Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Stud.IP
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Marcus Eibrink-Lunzenauer
Stud.IP
Commits
a54b6f39
Commit
a54b6f39
authored
10 months ago
by
Moritz Strohm
Browse files
Options
Downloads
Patches
Plain Diff
functions.php: added studip_interpolate, closes #3555
Closes #3555 Merge request
studip/studip!2442
parent
d066e349
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/functions.php
+31
-0
31 additions, 0 deletions
lib/functions.php
tests/unit/lib/FunctionsTest.php
+19
-1
19 additions, 1 deletion
tests/unit/lib/FunctionsTest.php
with
50 additions
and
1 deletion
lib/functions.php
+
31
−
0
View file @
a54b6f39
...
@@ -1859,3 +1859,34 @@ function xml_escape($string)
...
@@ -1859,3 +1859,34 @@ function xml_escape($string)
$string
=
preg_replace
(
'/[\x00-\x08\x0b\x0c\x0e-\x1f]/'
,
''
,
$string
);
$string
=
preg_replace
(
'/[\x00-\x08\x0b\x0c\x0e-\x1f]/'
,
''
,
$string
);
return
htmlspecialchars
(
$string
,
ENT_QUOTES
,
'UTF-8'
);
return
htmlspecialchars
(
$string
,
ENT_QUOTES
,
'UTF-8'
);
}
}
/**
* This function mimics the functionality of the $gettextInterpolate function in JavaScript.
* This makes it easier to format text in translatable strings.
*
* Note that the behavior of this function is simplified in comparison with $gettextInterpolate:
* - All placeholders that have a value are replaced with the string value of that value.
* Numbers must be pre-formatted before added to the parameters.
* - All placeholders that have no replacements in the parameters array are output.
*
* @param string $gettext_string The translation string to be interpolated.
*
* @param array $parameters The parameters that replace the placeholders in the translation string.
* Array keys are the names of the placeholders while array values are the values that are
* placed inside the string.
*
* @return string The interpolated translation string.
*/
function
studip_interpolate
(
string
$gettext_string
,
array
$parameters
)
:
string
{
return
preg_replace_callback
(
'/%\{\s*(\w+)\s*\}/'
,
function
(
$match
)
use
(
$parameters
):
string
{
if
(
!
isset
(
$parameters
[
$match
[
1
]]))
{
throw
new
Exception
(
'The parameter for the placeholder '
.
$match
[
1
]
.
' is missing.'
);
}
return
$parameters
[
$match
[
1
]];
},
$gettext_string
);
}
This diff is collapsed.
Click to expand it.
tests/unit/lib/FunctionsTest.php
+
19
−
1
View file @
a54b6f39
...
@@ -80,11 +80,29 @@ class FunctionsTest extends \Codeception\Test\Unit
...
@@ -80,11 +80,29 @@ class FunctionsTest extends \Codeception\Test\Unit
public
function
testTrailsControllerExtractActionAndArgs
()
public
function
testTrailsControllerExtractActionAndArgs
()
{
{
$controller
=
new
Trails_Controller
(
null
);
$controller
=
new
Trails_Controller
(
null
);
list
(
$action
,
$args
,
$format
)
=
$controller
->
extract_action_and_args
(
'foo/bar//42.html'
);
[
$action
,
$args
,
$format
]
=
$controller
->
extract_action_and_args
(
'foo/bar//42.html'
);
$this
->
assertEquals
(
'foo'
,
$action
);
$this
->
assertEquals
(
'foo'
,
$action
);
$this
->
assertEquals
([
'bar'
,
''
,
'42'
],
$args
);
$this
->
assertEquals
([
'bar'
,
''
,
'42'
],
$args
);
$this
->
assertEquals
(
'html'
,
$format
);
$this
->
assertEquals
(
'html'
,
$format
);
}
}
/**
* @covers ::studip_interpolate
*/
public
function
testStudipInterpolate
()
{
$this
->
assertEquals
(
'12bar34'
,
studip_interpolate
(
'12%{foo}34'
,
[
'foo'
=>
'bar'
])
);
$this
->
assertEquals
(
'foo'
,
studip_interpolate
(
'%{ bar }'
,
[
'bar'
=>
'foo'
])
);
$this
->
expectException
(
Exception
::
class
);
studip_interpolate
(
'%{foo}'
,
[]);
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment