Skip to content

Commit

Permalink
cleanup Smile-SA#1
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Crocombe authored and rbayet committed Sep 12, 2023
1 parent 10ee13e commit c0f6442
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
26 changes: 13 additions & 13 deletions src/module-elasticsuite-catalog/Helper/ProductAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
*/
class ProductAttribute extends AbstractAttribute
{
/**
/**
* @var string Config path to determine if we should use display battern on frontend.
*/
const XML_PATH_FRONTEND_PRODUCT_DISPLAY_PATTERN_ENABLED =
'smile_elasticsuite_catalogsearch_settings/catalogsearch/frontend_product_display_pattern_enabled';
const XML_PATH_FRONTEND_PRODUCT_DISPLAY_PATTERN_ENABLED
= 'smile_elasticsuite_catalogsearch_settings/catalogsearch/frontend_product_display_pattern_enabled';

/**
* @var string Attribute column containing pattern.
Expand All @@ -45,18 +45,18 @@ class ProductAttribute extends AbstractAttribute
*/
const DISPLAY_PRECISION_COLUMN = 'display_precision';

/**
/**
* @var string Default display pattern if not otherwise specified.
*/
const DEFAULT_DISPLAY_PATTERN = '%s';

/**
/**
* @var string Round value to this many decimal places if not otherwise specified.
*/
const DEFAULT_DISPLAY_PRECISION = 2;

/**
* @var int The maximum possible replacements for each pattern in each subject string.
* @var int The maximum possible replacements for each pattern in each subject string.
*/
const REPLACEMENT_LIMIT = 1;

Expand Down Expand Up @@ -104,20 +104,20 @@ public function isFrontendProductDisplayPatternEnabled(): bool
*
* @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute Product Attribute to format
* @param int|float|string $value Product Attribute Value to format
*
*
* @return string Formatted string
*/
public function formatProductAttributeValueDisplayPattern($attribute, $value)
{
// Translate attribute pattern, or default, without variable.
$pattern = $attribute->getData(self::DISPLAY_PATTERN_COLUMN)
? (string) __($attribute->getData(self::DISPLAY_PATTERN_COLUMN))
: (string) self::DEFAULT_DISPLAY_PATTERN;
$pattern = $attribute->getData(self::DISPLAY_PATTERN_COLUMN) ?
(string) __($attribute->getData(self::DISPLAY_PATTERN_COLUMN)) :
(string) self::DEFAULT_DISPLAY_PATTERN;

// Get attribute display precision or default.
$precision = is_numeric($attribute->getData(self::DISPLAY_PRECISION_COLUMN) ?? '')
? (int) abs($attribute->getData(self::DISPLAY_PRECISION_COLUMN))
: (int) self::DEFAULT_DISPLAY_PRECISION;
$precision = is_numeric($attribute->getData(self::DISPLAY_PRECISION_COLUMN) ?? '') ?
(int) abs($attribute->getData(self::DISPLAY_PRECISION_COLUMN)) :
(int) self::DEFAULT_DISPLAY_PRECISION;

// Round value to precision and format to locale string.
$amount = (string) $this->localeFormatter->formatNumber(round((float) $value, $precision));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AttributesPlugin
/**
* @var \Smile\ElasticsuiteCatalog\Helper\ProductAttribute
*/
private $productAttributeHelper;
private $_productAttributeHelper;

/**
* Constructor.
Expand All @@ -37,7 +37,7 @@ class AttributesPlugin
public function __construct(
ProductAttribute $productAttributeHelper
) {
$this->productAttributeHelper = $productAttributeHelper;
$this->_productAttributeHelper = $productAttributeHelper;
}

/**
Expand All @@ -46,12 +46,13 @@ public function __construct(
* @param \Magento\Catalog\Block\Product\View\Attributes $subject Plugin Subject
* @param array $result Additional data
* @param string[] $excludeAttr Attribute Codes to exclude
*
* @return array Additional data
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function afterGetAdditionalData(\Magento\Catalog\Block\Product\View\Attributes $subject, $result, array $excludeAttr = [])
{
if (!$this->productAttributeHelper->isFrontendProductDisplayPatternEnabled()) {
if (!$this->_productAttributeHelper->isFrontendProductDisplayPatternEnabled()) {
return $result;
}

Expand All @@ -60,12 +61,12 @@ public function afterGetAdditionalData(\Magento\Catalog\Block\Product\View\Attri
foreach ($attributes as $attribute) {
// If attribute is already in array, then isVisibleOnFrontend = `true`.
if (isset($result[$attribute->getAttributeCode()])) {
$value = isset($result[$attribute->getAttributeCode()]['value'])
? $result[$attribute->getAttributeCode()]['value']
: '';
$value = isset($result[$attribute->getAttributeCode()]['value']) ?
$result[$attribute->getAttributeCode()]['value'] :
'';
if (is_numeric($value) && strlen($attribute->getData('display_pattern') ?? '') > 0) {
$result[$attribute->getAttributeCode()]['value'] =
$this->productAttributeHelper->formatProductAttributeValueDisplayPattern($attribute, $value);
$result[$attribute->getAttributeCode()]['value']
= $this->_productAttributeHelper->formatProductAttributeValueDisplayPattern($attribute, $value);
}
}
}
Expand Down

0 comments on commit c0f6442

Please # to comment.