Skip to content

Commit be016e0

Browse files
author
graschik
committed
Add DecodeComponentValue Model
1 parent f897dda commit be016e0

File tree

3 files changed

+121
-2
lines changed

3 files changed

+121
-2
lines changed

Block/Widget/AbstractWidget.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Grasch\AdminUi\Block\Widget;
5+
6+
use Grasch\AdminUi\Model\DecodeComponentValue;
7+
use Magento\Framework\View\Element\Template;
8+
use Magento\Framework\View\Element\Template\Context;
9+
10+
abstract class AbstractWidget extends Template
11+
{
12+
/**
13+
* @var DecodeComponentValue
14+
*/
15+
private DecodeComponentValue $decodeComponentValue;
16+
17+
/**
18+
* @param Context $context
19+
* @param DecodeComponentValue $decodeComponentValue
20+
* @param array $data
21+
*/
22+
public function __construct(
23+
Template\Context $context,
24+
DecodeComponentValue $decodeComponentValue,
25+
array $data = []
26+
) {
27+
parent::__construct($context, $data);
28+
29+
$this->decodeComponentValue = $decodeComponentValue;
30+
}
31+
32+
/**
33+
* @param $key
34+
* @param $index
35+
* @return array|mixed|string|null
36+
*/
37+
public function getData($key = '', $index = null)
38+
{
39+
$result = parent::getData($key, $index);
40+
41+
if (!$result || !is_string($result)) {
42+
return $result;
43+
}
44+
45+
return $this->decodeComponentValue->execute($result);
46+
}
47+
}

Block/Widget/ExampleOfUIComponents.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33

44
namespace Grasch\AdminUi\Block\Widget;
55

6-
use Magento\Framework\View\Element\Template;
76
use Magento\Widget\Block\BlockInterface;
87

9-
class ExampleOfUIComponents extends Template implements BlockInterface
8+
class ExampleOfUIComponents extends AbstractWidget implements BlockInterface
109
{
10+
/**
11+
* @return string
12+
*/
13+
protected function _toHtml(): string
14+
{
15+
$data = $this->getData('component_data');
16+
print_r($data);
17+
18+
return '';
19+
}
1120
}

Model/DecodeComponentValue.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © InComm, Inc. All rights reserved.
4+
*/
5+
declare(strict_types=1);
6+
7+
namespace Grasch\AdminUi\Model;
8+
9+
use Magento\Framework\Exception\LocalizedException;
10+
use Magento\Framework\Serialize\SerializerInterface;
11+
use Psr\Log\LoggerInterface;
12+
13+
class DecodeComponentValue
14+
{
15+
/**
16+
* @var SerializerInterface
17+
*/
18+
private SerializerInterface $serializer;
19+
20+
/**
21+
* @var Base64
22+
*/
23+
private Base64 $base64;
24+
25+
/**
26+
* @var LoggerInterface
27+
*/
28+
private LoggerInterface $logger;
29+
30+
/**
31+
* @param SerializerInterface $serializer
32+
* @param Base64 $base64
33+
* @param LoggerInterface $logger
34+
*/
35+
public function __construct(
36+
SerializerInterface $serializer,
37+
Base64 $base64,
38+
LoggerInterface $logger
39+
) {
40+
$this->serializer = $serializer;
41+
$this->base64 = $base64;
42+
$this->logger = $logger;
43+
}
44+
45+
/**
46+
* @param string $value
47+
* @return array
48+
*/
49+
public function execute(string $value): array
50+
{
51+
if (preg_match('/encodedComponentsData\|.*/', $value)) {
52+
$value = preg_replace('/encodedComponentsData\|/', '', $value);
53+
try {
54+
$value = $this->base64->decode($value);
55+
$value = $this->serializer->unserialize($value);
56+
} catch (LocalizedException $e) {
57+
$this->logger->error($e->getMessage());
58+
};
59+
}
60+
61+
return $value;
62+
}
63+
}

0 commit comments

Comments
 (0)