Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
05263692
Commit
05263692
authored
Nov 09, 2014
by
pana1990
Committed by
Qiang Xue
Nov 10, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #5938: add sentence method in Arrayhelper for convert the array to a comma-separated sentence
parent
88caccd6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
0 deletions
+60
-0
BaseArrayHelper.php
framework/helpers/BaseArrayHelper.php
+60
-0
No files found.
framework/helpers/BaseArrayHelper.php
View file @
05263692
...
...
@@ -566,4 +566,64 @@ class BaseArrayHelper
return
true
;
}
}
/**
* Converts the array to a comma-separated sentence where the last element
* is joined by the connector word.
*
* Below are some usage examples,
*
* ~~~
* // $array = ['Spain', 'France', 'Italy'];
* // working with array
* \yii\helpers\ArrayHelper::sentence($array);
* // output : Spain, France, and Italy
*
* // working with options for change behavior
* \yii\helpers\ArrayHelper::sentence($array, ['last_word_connector' => ' or ']);
* // output : Spain, France or Italy
* ~~~
*
* @param array $array the array to be converted into an string
* @param array $options the options in terms of name-value pairs. The following options
* are specially handled:
*
* - words_connector: The sign or word used to join the elements in arrays with two or
* more elements. By default ', ''.
*
* - two_words_connector: The sign or word used to join the elements in arrays with two
* elements. By default ' and '.
*
* - last_word_connector: The sign or word used to join the last element in arrays with
* three or more elements. By default ', and '.
*
* - locale: If i18n is available, you can set a locale and use the traslation file defined
* on the path 'messages/yii.php'.By default language defined in configuration file.
*
* @return string
*/
public
static
function
sentence
(
$array
,
$options
=
[])
{
$default_options
=
[
'words_connector'
=>
', '
,
'two_words_connector'
=>
' and '
,
'last_word_connector'
=>
', and '
,
'locale'
=>
Yii
::
$app
->
language
];
$options
=
array_merge
(
$default_options
,
$options
);
$count
=
count
(
$array
);
switch
(
$count
)
{
case
0
:
return
''
;
case
1
:
return
$array
[
0
];
case
2
:
return
$array
[
0
]
.
\Yii
::
t
(
'yii'
,
$options
[
'two_words_connector'
],
[],
$options
[
'locale'
])
.
$array
[
1
];
default
:
return
implode
(
$options
[
'words_connector'
],
array_slice
(
$array
,
0
,
-
1
))
.
\Yii
::
t
(
'yii'
,
$options
[
'last_word_connector'
],
[],
$options
[
'locale'
])
.
$array
[
$count
-
1
];
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment