ModCfg/Application/Model/Encoding/d3encoder.php

207 lines
4.7 KiB
PHP
Raw Normal View History

2024-04-19 16:15:46 +02:00
<?php
/**
2024-09-02 08:48:43 +02:00
* Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
2024-04-19 16:15:46 +02:00
*
2024-09-02 08:48:43 +02:00
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
2024-04-19 16:15:46 +02:00
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
2024-09-02 08:48:43 +02:00
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
2024-04-19 16:15:46 +02:00
*/
namespace D3\ModCfg\Application\Model\Encoding;
2024-06-26 13:49:30 +02:00
use JsonException;
2024-04-19 16:15:46 +02:00
class d3encoder
{
public const ENC_DEFAULT = 'default';
public const ENC_JSON = 'json';
public const ENC_UTF8 = 'utf-8';
public const ENC_GZIP = 'gzip';
public const ENC_SERIALIZE = 'serialize';
public const ENC_RAWURL = 'rawurl';
public const ENC_URL = 'url';
public const ENC_BASE64 = 'base64';
public const ENC_UUENC = 'uuencode';
public const ENC_QUOTED = 'quoted';
/**
* @param string $_sEncodingType
*/
2024-06-26 13:49:30 +02:00
public function __construct(protected string $_sEncodingType = self::ENC_DEFAULT)
2024-04-19 16:15:46 +02:00
{
}
/**
* @return string
*/
2024-06-26 13:49:30 +02:00
public function getEncodingType(): string
2024-04-19 16:15:46 +02:00
{
return $this->_sEncodingType;
}
/**
2024-06-26 13:49:30 +02:00
* @param mixed $decodedValue
2024-04-19 16:15:46 +02:00
*
* @return string
2024-06-26 13:49:30 +02:00
* @throws JsonException
2024-04-19 16:15:46 +02:00
*/
2024-09-02 08:48:43 +02:00
public function encode(mixed $decodedValue): string
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return match (strtolower($this->getEncodingType())) {
self::ENC_JSON => $this->encodeJson($decodedValue),
self::ENC_UTF8 => $this->encodeUtf8($decodedValue),
self::ENC_GZIP => $this->encodeGZip($decodedValue),
self::ENC_SERIALIZE => $this->encodeSerialize($decodedValue),
self::ENC_RAWURL => $this->encodeRawUrl($decodedValue),
self::ENC_URL => $this->encodeUrl($decodedValue),
self::ENC_BASE64 => $this->encodeBase64($decodedValue),
self::ENC_UUENC => $this->encodeUUEncode($decodedValue),
self::ENC_QUOTED => $this->encodeQuotedPrintable($decodedValue),
default => $this->encodeDefault($decodedValue)
};
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param mixed $decodedValue
2024-04-19 16:15:46 +02:00
*
* @return string
*/
2024-09-02 08:48:43 +02:00
public function encodeDefault(mixed $decodedValue): string
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return base64_encode(
rawurlencode(
serialize(
$decodedValue
)
)
);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param mixed $decodedValue
2024-04-19 16:15:46 +02:00
*
* @return string
2024-06-26 13:49:30 +02:00
* @throws JsonException
2024-04-19 16:15:46 +02:00
*/
2024-09-02 08:48:43 +02:00
public function encodeJson(mixed $decodedValue): string
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return json_encode($decodedValue, JSON_THROW_ON_ERROR);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param mixed $decodedValue
2024-04-19 16:15:46 +02:00
*
* @return string
*/
2024-09-02 08:48:43 +02:00
public function encodeUtf8(mixed $decodedValue): string
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return utf8_encode(
serialize(
$decodedValue
)
);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param mixed $decodedValue
2024-04-19 16:15:46 +02:00
*
* @return string
*/
2024-09-02 08:48:43 +02:00
public function encodeSerialize(mixed $decodedValue): string
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return serialize(
$decodedValue
);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param mixed $decodedValue
2024-04-19 16:15:46 +02:00
*
* @return string
*/
2024-09-02 08:48:43 +02:00
public function encodeUrl(mixed $decodedValue): string
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return urlencode(
serialize(
$decodedValue
)
);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param mixed $decodedValue
2024-04-19 16:15:46 +02:00
*
* @return string
*/
2024-09-02 08:48:43 +02:00
public function encodeRawUrl(mixed $decodedValue): string
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return rawurlencode(
serialize(
$decodedValue
)
);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param mixed $decodedValue
2024-04-19 16:15:46 +02:00
*
* @return string
*/
2024-09-02 08:48:43 +02:00
public function encodeBase64(mixed $decodedValue): string
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return base64_encode(
serialize(
$decodedValue
)
);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param mixed $decodedValue
2024-04-19 16:15:46 +02:00
*
* @return string
*/
2024-09-02 08:48:43 +02:00
public function encodeGZip(mixed $decodedValue): string
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return gzencode(
serialize(
$decodedValue
)
);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param mixed $decodedValue
2024-04-19 16:15:46 +02:00
*
* @return string
*/
2024-09-02 08:48:43 +02:00
public function encodeUUEncode(mixed $decodedValue): string
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return convert_uuencode(
serialize(
$decodedValue
)
);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param mixed $decodedValue
2024-04-19 16:15:46 +02:00
*
* @return string
*/
2024-09-02 08:48:43 +02:00
public function encodeQuotedPrintable(mixed $decodedValue): string
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return quoted_printable_encode(
serialize(
$decodedValue
)
);
2024-04-19 16:15:46 +02:00
}
}