Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
JackieDo committed Mar 16, 2022
1 parent 73e2762 commit b827edc
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 90 deletions.
16 changes: 8 additions & 8 deletions src/Facades/Timezonelist.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
/**
* The Timezonelist facade.
*
* @method static \Jackiedo\Timezonelist\Timezonelist onlyGroups(array $groups=[]) Set the filter of the groups want to get.
* @method static \Jackiedo\Timezonelist\Timezonelist excludeGroups(array $groups=[]) Set the filter of the groups do not want to get.
* @method static \Jackiedo\Timezonelist\Timezonelist splitGroup(bool $status=true) Decide whether to split group or not.
* @method statis \Jackiedo\Timezonelist\Timezonelist showOffset(bool $status=true) Decide whether to show the offset or not.
* @method static \Jackiedo\Timezonelist\Timezonelist new() Return new static to reset all config.
* @method static string toSelectBox(string $name, null|string $selected=null, null|array|string $attr=null, bool $htmlencode=true) Create a select box of timezones.
* @method static string create(string $name, null|string $selected=null, null|array|string $attr=null, bool $htmlencode=true) Alias of the `toSelectBox()` method.
* @method static array toArray(bool $htmlencode=true) Create an array of timezones.
* @method static \Jackiedo\Timezonelist\Timezonelist onlyGroups(array $groups=[]) Set the filter of the groups want to get.
* @method static \Jackiedo\Timezonelist\Timezonelist excludeGroups(array $groups=[]) Set the filter of the groups do not want to get.
* @method static \Jackiedo\Timezonelist\Timezonelist splitGroup(bool $status=true) Decide whether to split group or not.
* @method statis \Jackiedo\Timezonelist\Timezonelist showOffset(bool $status=true) Decide whether to show the offset or not.
* @method static \Jackiedo\Timezonelist\Timezonelist new() Return new static to reset all config.
* @method static string toSelectBox(string $name, null|string $selected=null, null|array|string $attrs=null, bool $htmlencode=true) Create a select box of timezones.
* @method static string create(string $name, null|string $selected=null, null|array|string $attrs=null, bool $htmlencode=true) Alias of the `toSelectBox()` method.
* @method static array toArray(bool $htmlencode=true) Create an array of timezones.
*
* @see \Jackiedo\Timezonelist\Timezonelist
*/
Expand Down
163 changes: 81 additions & 82 deletions src/Timezonelist.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,55 +153,6 @@ public function new()
return new static;
}

/**
* Create a select box of timezones.
*
* @param string $name The name of the select tag
* @param null|string $selected The selected value
* @param null|array|string $attr The HTML attributes of select tag
* @param bool $htmlencode Use HTML entities for values of select tag
*
* @return string
*/
public function toSelectBox($name, $selected = null, $attr = null, $htmlencode = true)
{
// Attributes for select element
$attrSet = null;

if (!empty($attr)) {
if (is_array($attr)) {
foreach ($attr as $attr_name => $attr_value) {
$attrSet .= ' ' . $attr_name . '="' . $attr_value . '"';
}
} else {
$attrSet = ' ' . $attr;
}
}

$listbox = '<select name="' . $name . '"' . $attrSet . '>';
$listbox .= (!$this->splitGroup ? $this->genOptWithoutGroup($selected, $htmlencode) : $this->genOptWithGroup($selected, $htmlencode));
$listbox .= '</select>';

return $listbox;
}

/**
* Alias of the `toSelectBox()` method.
*
* @deprecated 6.0.0 This method name no longer matches the semantics
*
* @param string $name The name of the select tag
* @param null|string $selected The selected value
* @param null|array|string $attr The HTML attributes of select tag
* @param bool $htmlencode Use HTML entities for values of select tag
*
* @return string
*/
public function create($name, $selected = null, $attr = null, $htmlencode = true)
{
return $this->toSelectBox($name, $selected, $attr, $htmlencode);
}

/**
* Create an array of timezones.
*
Expand All @@ -213,7 +164,7 @@ public function toArray($htmlencode = true)
{
$list = [];

// If do not group the return list
// If do not split group
if (!$this->splitGroup) {
if ($this->includeGeneral()) {
foreach ($this->generalTimezones as $timezone) {
Expand All @@ -232,7 +183,7 @@ public function toArray($htmlencode = true)
return $list;
}

// If group the return list
// If split group
if ($this->includeGeneral()) {
foreach ($this->generalTimezones as $timezone) {
$list['General'][$timezone] = $this->formatTimezone($timezone, null, $htmlencode);
Expand All @@ -251,23 +202,74 @@ public function toArray($htmlencode = true)
}

/**
* Generate option tag with the optgroup tag.
* Alias of the `toSelectBox()` method.
*
* @param string $selected
* @param bool $htmlencode
* @deprecated 6.0.0 This method name no longer matches the semantics
*
* @param string $name The name of the select tag
* @param null|string $selected The selected value
* @param null|array|string $attrs The HTML attributes of select tag
* @param bool $htmlencode Use HTML entities for values of select tag
*
* @return string
*/
public function create($name, $selected = null, $attrs = null, $htmlencode = true)
{
return $this->toSelectBox($name, $selected, $attrs, $htmlencode);
}

/**
* Create a select box of timezones.
*
* @param string $name The name of the select tag
* @param null|string $selected The selected value
* @param null|array|string $attrs The HTML attributes of select tag
* @param bool $htmlencode Use HTML entities for values of select tag
*
* @return string
*/
public function toSelectBox($name, $selected = null, $attrs = null, $htmlencode = true)
{
// Attributes for select element
$attrString = null;

if (!empty($attrs)) {
if (is_array($attrs)) {
foreach ($attrs as $attr_name => $attr_value) {
$attrString .= ' ' . $attr_name . '="' . $attr_value . '"';
}
} else {
$attrString = $attrs;
}
}

if ($this->splitGroup) {
return $this->makeSelectTagWithGroup($name, $selected, $attrString, $htmlencode);
}

return $this->makeSelectTagWithoutGroup($name, $selected, $attrString, $htmlencode);
}

/**
* Generate select element with the optgroup tag.
*
* @param string $name The name of the select tag
* @param null|string $selected The selected value
* @param null|string $attrs The HTML attributes of select tag
* @param bool $htmlencode Use HTML entities for values of select tag
*
* @return string
*/
protected function genOptWithGroup($selected, $htmlencode = true)
protected function makeSelectTagWithGroup($name, $selected = null, $attrs = null, $htmlencode = true)
{
$output = null;
$attrs = !empty($attrs) ? ' ' . trim((string) $attrs) : '';
$output = '<select name="' . (string) $name . '"' . $attrs . '>';

if ($this->includeGeneral()) {
$output .= '<optgroup label="General">';

foreach ($this->generalTimezones as $timezone) {
$attrs = ($selected == $timezone) ? 'selected="selected"' : '';
$output .= $this->genOptionTag($timezone, $attrs, $this->formatTimezone($timezone, null, $htmlencode));
$output .= $this->makeOptionTag($this->formatTimezone($timezone, null, $htmlencode), $timezone, ($selected == $timezone));
}

$output .= '</optgroup>';
Expand All @@ -278,68 +280,65 @@ protected function genOptWithGroup($selected, $htmlencode = true)
$output .= '<optgroup label="' . $continent . '">';

foreach ($timezones as $timezone) {
$attrs = ($selected == $timezone) ? 'selected="selected"' : '';
$output .= $this->genOptionTag($timezone, $attrs, $this->formatTimezone($timezone, $continent, $htmlencode));
$output .= $this->makeOptionTag($this->formatTimezone($timezone, $continent, $htmlencode), $timezone, ($selected == $timezone));
}

$output .= '</optgroup>';
}

$output .= '</select>';

return $output;
}

/**
* Generate option tag without the optgroup tag.
* Generate select element without the optgroup tag.
*
* @param string $selected
* @param bool $htmlencode
* @param string $name The name of the select tag
* @param null|string $selected The selected value
* @param null|string $attrs The HTML attributes of select tag
* @param bool $htmlencode Use HTML entities for values of select tag
*
* @return string
*/
protected function genOptWithoutGroup($selected, $htmlencode = true)
protected function makeSelectTagWithoutGroup($name, $selected = null, $attrs = null, $htmlencode = true)
{
$output = null;
$attrs = !empty($attrs) ? ' ' . trim((string) $attrs) : '';
$output = '<select name="' . (string) $name . '"' . $attrs . '>';

if ($this->includeGeneral()) {
foreach ($this->generalTimezones as $timezone) {
$attrs = ($selected == $timezone) ? 'selected="selected"' : '';
$output .= $this->genOptionTag($timezone, $attrs, $this->formatTimezone($timezone, null, $htmlencode));
$output .= $this->makeOptionTag($this->formatTimezone($timezone, null, $htmlencode), $timezone, ($selected == $timezone));
}
}

// start adding all timezone of continents
foreach ($this->loadContinents() as $continent => $mask) {
$timezones = DateTimeZone::listIdentifiers($mask);

foreach ($timezones as $timezone) {
$attrs = ($selected == $timezone) ? 'selected="selected"' : '';
$output .= $this->genOptionTag($timezone, $attrs, $this->formatTimezone($timezone, null, $htmlencode));
$output .= $this->makeOptionTag($this->formatTimezone($timezone, null, $htmlencode), $timezone, ($selected == $timezone));
}
}
// end adding all timezone of continents

$output .= '</select>';

return $output;
}

/**
* Generate the option HTML tag.
*
* @param string $value
* @param string $attr
* @param string $display
* @param string $value
* @param bool $selected
*
* @return string
*/
protected function genOptionTag($value, $attr, $display)
protected function makeOptionTag($display, $value, $selected = false)
{
$attr = (string) (!empty($attr) ? ' ' . $attr : '');
$output = '';
$attrs = (bool) $selected ? ' selected="selected"' : '';

$output .= '<option value="' . (string) $value . '"' . $attr . '>';
$output .= $display;
$output .= '</option>';

return $output;
return '<option value="' . (string) $value . '"' . $attrs . '>' . (string) $display . '</option>';
}

/**
Expand Down Expand Up @@ -419,7 +418,7 @@ protected function normalizeOffset($offset, $htmlencode = true)
protected function normalizeTimezone($timezone, $htmlencode = true)
{
$search = ['St_', '/', '_'];
$replace = ['St. ', ' / ' . ' '];
$replace = ['St. ', ' / ' , ' '];

return str_replace($search, $replace, $timezone);
}
Expand Down

0 comments on commit b827edc

Please sign in to comment.