43 lines
1.4 KiB
PHP
43 lines
1.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\Reward\Model\Config\Backend;
|
|
|
|
use Magento\Config\Model\Config\Backend\Serialized\ArraySerialized;
|
|
use Magento\Config\Model\ResourceModel\Config\Data as ConfigResource;
|
|
use Magento\Config\Model\ResourceModel\Config\Data\Collection as ConfigCollection;
|
|
|
|
class TierItems extends ArraySerialized
|
|
{
|
|
protected function _construct(): void
|
|
{
|
|
$this->_setResourceModel(ConfigResource::class, ConfigCollection::class);
|
|
}
|
|
|
|
public function beforeSave()
|
|
{
|
|
$items = [];
|
|
$value = $this->getValue();
|
|
if (is_array($value)) {
|
|
unset($value['__empty']);
|
|
foreach ($value as $row) {
|
|
if (!is_array($row)) {
|
|
continue;
|
|
}
|
|
$label = trim((string)($row['label'] ?? ''));
|
|
$threshold = max(0.0, (float)($row['threshold'] ?? 0));
|
|
$multiplier = (float)($row['multiplier'] ?? 0);
|
|
if ($label === '' || $multiplier <= 0) {
|
|
continue;
|
|
}
|
|
$items[] = ['label' => $label, 'threshold' => $threshold, 'multiplier' => $multiplier];
|
|
}
|
|
}
|
|
|
|
usort($items, static fn(array $left, array $right): int => $left['threshold'] <=> $right['threshold']);
|
|
$this->setValue($items);
|
|
|
|
return parent::beforeSave();
|
|
}
|
|
}
|