chg: [helpers] Added documentation and tweakings

pull/37/head
mokaddem 2020-12-11 08:52:11 +01:00
parent aebbb3ebd8
commit 3806533abb
2 changed files with 45 additions and 34 deletions

View File

@ -3,11 +3,11 @@
* Bootstrap Tabs helper * Bootstrap Tabs helper
* Options: * Options:
* [style] * [style]
* - fill: Should the navigation occupies all space * - fill: Should the navigation items occupies all space
* - justify: Should the navigation be justified (accept: ['center', 'end']) * - justify: Should the navigation items be justified (accept: ['center', 'end'])
* - pills: Should the tabs be pills * - pills: Should the navigation items be pills
* - vertical: Should the navigation be placed on the left side of the content * - vertical: Should the navigation bar be placed on the left side of the content
* - vertical-size: How many boostrap cols be used for the navigation (only used when `vertical` is true) * - vertical-size: Specify how many boostrap's `cols` should be used for the navigation (only used when `vertical` is true)
* - card: Should the navigation be placed in a bootstrap card * - card: Should the navigation be placed in a bootstrap card
* - nav-class: additional class to add to the nav container * - nav-class: additional class to add to the nav container
* - content-class: additional class to add to the content container * - content-class: additional class to add to the content container
@ -55,8 +55,8 @@ class BootstrapHelper extends Helper
class BootstrapTabs extends Helper class BootstrapTabs extends Helper
{ {
private $defaultOptions = [ private $defaultOptions = [
'nav-fill' => false, 'fill' => false,
'nav-justify' => false, 'justify' => false,
'pills' => false, 'pills' => false,
'vertical' => false, 'vertical' => false,
'vertical-size' => 3, 'vertical-size' => 3,
@ -71,7 +71,7 @@ class BootstrapTabs extends Helper
]; ];
private $allowedOptionValues = [ private $allowedOptionValues = [
'nav-justify' => [false, 'center', 'end'], 'justify' => [false, 'center', 'end'],
]; ];
private $options = null; private $options = null;
@ -97,8 +97,8 @@ class BootstrapTabs extends Helper
]; ];
if (!empty($this->options['nav-justify'])) { if (!empty($this->options['justify'])) {
$this->bsClasses['nav'][] = 'justify-content-' . $this->options['nav-justify']; $this->bsClasses['nav'][] = 'justify-content-' . $this->options['justify'];
} }
if ($this->options['pills']) { if ($this->options['pills']) {
@ -116,10 +116,10 @@ class BootstrapTabs extends Helper
} }
} }
if ($this->options['nav-fill']) { if ($this->options['fill']) {
$this->bsClasses['nav'][] = 'nav-fill'; $this->bsClasses['nav'][] = 'nav-fill';
} }
if ($this->options['nav-justify']) { if ($this->options['justify']) {
$this->bsClasses['nav'][] = 'nav-justify'; $this->bsClasses['nav'][] = 'nav-justify';
} }
@ -160,9 +160,6 @@ class BootstrapTabs extends Helper
if (empty($this->data['navs'])) { if (empty($this->data['navs'])) {
throw new InvalidArgumentException(__('No navigation data provided')); throw new InvalidArgumentException(__('No navigation data provided'));
} }
if ($this->options['card'] && $this->options['vertical']) {
throw new InvalidArgumentException(__('`card` option can only be used on horizontal mode'));
}
} }
private function genTabs() private function genTabs()
@ -198,11 +195,11 @@ class BootstrapTabs extends Helper
private function genVerticalTabs() private function genVerticalTabs()
{ {
$html = $this->genNode('div', ['class' => ['row']]);; $html = $this->genNode('div', ['class' => ['row', ($this->options['card'] ? 'card flex-row' : '')]]);
$html .= $this->genNode('div', ['class' => 'col-' . $this->options['vertical-size']]); $html .= $this->genNode('div', ['class' => ['col-' . $this->options['vertical-size'], ($this->options['card'] ? 'card-header border-right' : '')]]);
$html .= $this->genNav(); $html .= $this->genNav();
$html .= '</div>'; $html .= '</div>';
$html .= $this->genNode('div', ['class' => 'col-' . (12 - $this->options['vertical-size'])]); $html .= $this->genNode('div', ['class' => ['col-' . (12 - $this->options['vertical-size']), ($this->options['card'] ? 'card-body2' : '')]]);
$html .= $this->genContent(); $html .= $this->genContent();
$html .= '</div>'; $html .= '</div>';
$html .= '</div>'; $html .= '</div>';

View File

@ -8,31 +8,45 @@ use Cake\Utility\Hash;
class StringFromPathHelper extends Helper class StringFromPathHelper extends Helper
{ {
private $defaultOptions = [ private $defaultOptions = [
'sanitize' => true, 'sanitize' => true, // Should the variables to be injected into the string be sanitized. (ignored with the function)
'highlight' => false, 'highlight' => false, // Should the extracted data be highlighted
]; ];
public function buildStringFromDataPath(String $str, $data=[], array $dataPaths=[], array $options=[]) /**
* buildStringFromDataPath Inject data into a string at the correct place
*
* @param String $str The string that will have its arguements replaced by their value
* @param mixed $data The data from which the value of datapath arguement will be taken
* @param array $strArgs The arguments to be injected into the string.
* - Each argument ca be of mixed type:
* - String: A cakephp's Hash datapath used to extract the data
* - array: can contain a key of either
* - `datapath`: A cakephp's Hash datapath used to extract the data
* - `raw`: A raw string to be injecte as-is
* - `function`: A function to be executed with its $strArgs being passed
* @param array $options Allows to configure the behavior of the function
* @return String
*/
public function buildStringFromDataPath(String $str, $data=[], array $strArgs=[], array $options=[])
{ {
$options = array_merge($this->defaultOptions, $options); $options = array_merge($this->defaultOptions, $options);
if (!empty($dataPaths)) { if (!empty($strArgs)) {
$extractedVars = []; $extractedVars = [];
foreach ($dataPaths as $i => $dataPath) { foreach ($strArgs as $i => $strArg) {
$varValue = ''; $varValue = '';
if (is_array($dataPath)) { if (is_array($strArg)) {
$varValue = ''; $varValue = '';
if (!empty($dataPath['datapath'])) { if (!empty($strArg['datapath'])) {
$varValue = Hash::get($data, $dataPath['datapath']); $varValue = Hash::get($data, $strArg['datapath']);
} else if (!empty($dataPath['raw'])) { } else if (!empty($strArg['raw'])) {
$varValue = $dataPath['raw']; $varValue = $strArg['raw'];
} else if (!empty($dataPath['function'])) { } else if (!empty($strArg['function'])) {
$varValue = $dataPath['function']($data, $dataPath); $varValue = $strArg['function']($data, $strArg);
} }
// $extractedVars[] = $varValue;
} else { } else {
$varValue = Hash::get($data, $dataPath); $varValue = Hash::get($data, $strArg);
} }
if (empty($dataPath['function'])) { if (empty($strArg['function'])) {
$varValue = $options['sanitize'] ? h($varValue) : $varValue; $varValue = $options['sanitize'] ? h($varValue) : $varValue;
} }
$extractedVars[] = $varValue; $extractedVars[] = $varValue;