ModCfg/Application/Model/Encoding/d3encoder.php

209 lignes
4.8 KiB
PHP
Brut Vue normale Historique

2024-04-19 16:15:46 +02:00
<?php
/**
* This Software is the property of Data Development and is protected
* by copyright law - it is NOT Freeware.
*
* Any unauthorized use of this software without a valid license
* is a violation of the license agreement and will be prosecuted by
* civil and criminal law.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link http://www.oxidmodule.com
*/
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-06-26 13:49:30 +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-06-26 13:49:30 +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-06-26 13:49:30 +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-06-26 13:49:30 +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-06-26 13:49:30 +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-06-26 13:49:30 +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-06-26 13:49:30 +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-06-26 13:49:30 +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-06-26 13:49:30 +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-06-26 13:49:30 +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-06-26 13:49:30 +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
}
}