Skip to content

Commit

Permalink
Add type hints.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Nov 11, 2023
1 parent 8006b39 commit 9b5c301
Show file tree
Hide file tree
Showing 26 changed files with 394 additions and 358 deletions.
75 changes: 47 additions & 28 deletions src/Accordion.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
<?php

declare(strict_types=1);

/**
* @link https://www.yiiframework.com/
*
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/

declare(strict_types=1);

namespace yii\bootstrap5;

use Exception;
use Throwable;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;

use function array_key_exists;
use function array_merge;
use function implode;
use function in_array;
use function is_int;
use function is_numeric;
use function is_object;
use function is_string;

/**
* Accordion renders an accordion bootstrap javascript component.
*
Expand Down Expand Up @@ -60,19 +71,19 @@
class Accordion extends Widget
{
/**
* @var array list of groups in the collapse widget. Each array element represents a single
* group with the following structure:
* @var array list of groups in the collapse widget. Each array element represents a single group with the following
* structure:
*
* - label: string, required, the group header label.
* - encode: bool, optional, whether this label should be HTML-encoded. This param will override
* global `$this->encodeLabels` param.
* - content: array|string|object, required, the content (HTML) of the group
* - options: array, optional, the HTML attributes of the group
* - contentOptions: optional, the HTML attributes of the group's content
* - content: array|string|object, required, the content (HTML) of the group.
* - options: array, optional, the HTML attributes of the group.
* - contentOptions: optional, the HTML attributes of the group's content.
*
* Since version 2.0.7 you may also specify this property as key-value pairs, where the key refers to the
* `label` and the value refers to `content`. If value is a string it is interpreted as label. If it is
* an array, it is interpreted as explained above.
* You may also specify this property as key-value pairs, where the key refers to the `label` and the value refers
* to `content`. If value is a string, it is interpreted as label. If it is an array, it is interpreted as explained
* above.
*
* For example:
*
Expand All @@ -91,18 +102,19 @@ class Accordion extends Widget
* ])
* ```
*/
public $items = [];
public array $items = [];
/**
* @var bool whether the labels for header items should be HTML-encoded.
*/
public $encodeLabels = true;
public bool $encodeLabels = true;
/**
* @var bool whether to close other items if an item is opened. Defaults to `true` which causes an
* accordion effect. Set this to `false` to allow keeping multiple items open at once.
* @var bool whether to close other items if an item is opened. Defaults to `true` which causes an accordion effect.
* Set this to `false` to allow keeping multiple items open at once.
*/
public $autoCloseItems = true;
public bool $autoCloseItems = true;
/**
* @var array the HTML options for the item toggle tag. Key 'tag' might be used here for the tag name specification.
*
* For example:
*
* ```php
Expand All @@ -112,12 +124,11 @@ class Accordion extends Widget
* ]
* ```
*/
public $itemToggleOptions = [];
public array $itemToggleOptions = [];

/**
* @throws InvalidConfigException
*
* @return string
* @throws Throwable
*/
public function run(): string
{
Expand All @@ -134,15 +145,22 @@ public function run(): string
/**
* Renders collapsible items as specified on [[items]].
*
* @throws InvalidConfigException if label isn't specified
* @return string the rendering result.
*
* @return string the rendering result
* @throws InvalidConfigException if label isn't specified.
* @throws Exception
* @throws Throwable
*/
public function renderItems(): string
{
$items = [];
$index = 0;
$expanded = array_search(true, ArrayHelper::getColumn(ArrayHelper::toArray($this->items), 'expand', true));
$expanded = in_array(
true,
ArrayHelper::getColumn(ArrayHelper::toArray($this->items), 'expand'),
true,
);

foreach ($this->items as $key => $item) {
if (!is_array($item)) {
$item = ['content' => $item];
Expand All @@ -169,14 +187,15 @@ public function renderItems(): string
/**
* Renders a single collapsible item group
*
* @param string $header a label of the item group [[items]]
* @param array $item a single item from [[items]]
* @param int $index the item index as each item group content must have an id
* @param string $header a label of the item group [[items]].
* @param array $item a single item from [[items]].
* @param int $index the item index as each item group content must have an id.
*
* @throws InvalidConfigException
* @throws Exception
* @return string the rendering result.
*
* @return string the rendering result
* @throws Exception
* @throws Throwable
* @throws InvalidConfigException
*/
public function renderItem(string $header, array $item, int $index): string
{
Expand All @@ -187,7 +206,7 @@ public function renderItem(string $header, array $item, int $index): string
$options['id'] = $id;
Html::addCssClass($options, ['widget' => 'collapse']);

// check if accordion expanded, if true add show class
// check if accordion expanded, of true add show class
if ($expand) {
Html::addCssClass($options, ['visibility' => 'show']);
}
Expand Down
Loading

0 comments on commit 9b5c301

Please sign in to comment.