Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract helper functions #256

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/View/Helper/AbstractHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Laminas\Escaper\Exception\RuntimeException as EscaperException;
use Laminas\Form\ElementInterface;
use Laminas\Form\Exception\InvalidArgumentException;
use Laminas\Form\LabelAwareInterface;
use Laminas\I18n\View\Helper\AbstractTranslatorHelper as BaseAbstractHelper;
use Laminas\View\Helper\Doctype;
use Laminas\View\Helper\EscapeHtml;
Expand Down Expand Up @@ -558,4 +559,29 @@

return false;
}

/**
* translate the label
*
* @internal
*/
protected function translateLabel(string|int $label): string|int
mimmi20 marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->getTranslator()?->translate($label, $this->getTranslatorTextDomain()) ?? $label;

Check failure on line 570 in src/View/Helper/AbstractHelper.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

PossiblyInvalidArgument

src/View/Helper/AbstractHelper.php:570:51: PossiblyInvalidArgument: Argument 1 of Laminas\I18n\Translator\TranslatorInterface::translate expects string, but possibly different type int|string provided (see https://psalm.dev/092)
}

/**
* escape the label
*
* @internal
*/
protected function escapeLabel(ElementInterface $element, string $label): string
{
if ($element instanceof LabelAwareInterface && $element->getLabelOption('disable_html_escape')) {
return $label;
}

$escapeHtmlHelper = $this->getEscapeHtmlHelper();
return $escapeHtmlHelper($label);
}
}
14 changes: 2 additions & 12 deletions src/View/Helper/FormButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Laminas\Form\ElementInterface;
use Laminas\Form\Exception;
use Laminas\Form\LabelAwareInterface;

use function gettype;
use function is_array;
Expand Down Expand Up @@ -89,17 +88,8 @@
}
}

if (null !== ($translator = $this->getTranslator())) {
$buttonContent = $translator->translate(
$buttonContent,
$this->getTranslatorTextDomain()
);
}

if (! $element instanceof LabelAwareInterface || ! $element->getLabelOption('disable_html_escape')) {
$escapeHtmlHelper = $this->getEscapeHtmlHelper();
$buttonContent = $escapeHtmlHelper($buttonContent);
}
$buttonContent = $this->translateLabel($buttonContent);
$buttonContent = $this->escapeLabel($element, $buttonContent);

Check failure on line 92 in src/View/Helper/FormButton.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

PossiblyInvalidArgument

src/View/Helper/FormButton.php:92:55: PossiblyInvalidArgument: Argument 2 of Laminas\Form\View\Helper\FormButton::escapeLabel expects string, but possibly different type int|string provided (see https://psalm.dev/092)

return $openTag . $buttonContent . $this->closeTag();
}
Expand Down
14 changes: 2 additions & 12 deletions src/View/Helper/FormCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Laminas\Form\Element\Collection as CollectionElement;
use Laminas\Form\ElementInterface;
use Laminas\Form\FieldsetInterface;
use Laminas\Form\LabelAwareInterface;
use Laminas\View\Helper\HelperInterface;
use RuntimeException;

Expand Down Expand Up @@ -145,17 +144,8 @@
$legend = '';

if (! empty($label)) {
if (null !== ($translator = $this->getTranslator())) {
$label = $translator->translate(
$label,
$this->getTranslatorTextDomain()
);
}

if (! $element instanceof LabelAwareInterface || ! $element->getLabelOption('disable_html_escape')) {
$escapeHtmlHelper = $this->getEscapeHtmlHelper();
$label = $escapeHtmlHelper($label);
}
$label = $this->translateLabel($label);
$label = $this->escapeLabel($element, $label);

Check failure on line 148 in src/View/Helper/FormCollection.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

PossiblyInvalidArgument

src/View/Helper/FormCollection.php:148:55: PossiblyInvalidArgument: Argument 2 of Laminas\Form\View\Helper\FormCollection::escapeLabel expects string, but possibly different type int|string provided (see https://psalm.dev/092)

$legend = sprintf(
$this->labelWrapper,
Expand Down
10 changes: 2 additions & 8 deletions src/View/Helper/FormLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,8 @@
);
}

if (null !== ($translator = $this->getTranslator())) {
$label = $translator->translate($label, $this->getTranslatorTextDomain());
}

if (! $element instanceof LabelAwareInterface || ! $element->getLabelOption('disable_html_escape')) {
$escapeHtmlHelper = $this->getEscapeHtmlHelper();
$label = $escapeHtmlHelper($label);
}
$label = $this->translateLabel($label);
$label = $this->escapeLabel($element, $label);

Check failure on line 65 in src/View/Helper/FormLabel.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

PossiblyInvalidArgument

src/View/Helper/FormLabel.php:65:51: PossiblyInvalidArgument: Argument 2 of Laminas\Form\View\Helper\FormLabel::escapeLabel expects string, but possibly different type int|string provided (see https://psalm.dev/092)
}

if ($label && $labelContent) {
Expand Down
13 changes: 2 additions & 11 deletions src/View/Helper/FormMultiCheckbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@
array $selectedOptions,
array $attributes
): string {
$escapeHtmlHelper = $this->getEscapeHtmlHelper();
$labelHelper = $this->getLabelHelper();
$labelClose = $labelHelper->closeTag();
$labelPosition = $this->getLabelPosition();
Expand Down Expand Up @@ -212,16 +211,8 @@
$closingBracket
);

if (null !== ($translator = $this->getTranslator())) {
$label = $translator->translate(
$label,
$this->getTranslatorTextDomain()
);
}

if (! $element instanceof LabelAwareInterface || ! $element->getLabelOption('disable_html_escape')) {
$label = $escapeHtmlHelper($label);
}
$label = $this->translateLabel($label);
$label = $this->escapeLabel($element, $label);

Check failure on line 215 in src/View/Helper/FormMultiCheckbox.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

PossiblyInvalidArgument

src/View/Helper/FormMultiCheckbox.php:215:51: PossiblyInvalidArgument: Argument 2 of Laminas\Form\View\Helper\FormMultiCheckbox::escapeLabel expects string, but possibly different type int|string provided (see https://psalm.dev/092)

$labelOpen = $labelHelper->openTag($labelAttributes);
$template = $labelOpen . '%s%s' . $labelClose;
Expand Down
9 changes: 2 additions & 7 deletions src/View/Helper/FormRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@
*/
public function render(ElementInterface $element, ?string $labelPosition = null): string
{
$escapeHtmlHelper = $this->getEscapeHtmlHelper();
$labelHelper = $this->getLabelHelper();
$elementHelper = $this->getElementHelper();
$elementErrorsHelper = $this->getElementErrorsHelper();
Expand All @@ -129,9 +128,7 @@

if (isset($label) && '' !== $label) {
// Translate the label
if (null !== ($translator = $this->getTranslator())) {
$label = $translator->translate($label, $this->getTranslatorTextDomain());
}
$label = $this->translateLabel($label);
}

// Does this element have errors ?
Expand Down Expand Up @@ -170,9 +167,7 @@
$labelAttributes = $element->getLabelAttributes();
}

if (! $element instanceof LabelAwareInterface || ! $element->getLabelOption('disable_html_escape')) {
$label = $escapeHtmlHelper($label);
}
$label = $this->escapeLabel($element, $label);

Check failure on line 170 in src/View/Helper/FormRow.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

PossiblyInvalidArgument

src/View/Helper/FormRow.php:170:51: PossiblyInvalidArgument: Argument 2 of Laminas\Form\View\Helper\FormRow::escapeLabel expects string, but possibly different type int|non-empty-string provided (see https://psalm.dev/092)

if (empty($labelAttributes)) {
$labelAttributes = $this->labelAttributes;
Expand Down
7 changes: 1 addition & 6 deletions src/View/Helper/FormSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,7 @@ public function renderOptions(array $options, array $selectedOptions = []): stri
$selected = true;
}

if (null !== ($translator = $this->getTranslator())) {
$label = $translator->translate(
$label,
$this->getTranslatorTextDomain()
);
}
$label = $this->translateLabel($label);

$attributes = [
'value' => $value,
Expand Down