primo commit

This commit is contained in:
2024-12-17 17:34:10 +01:00
commit e650f8df99
16435 changed files with 2451012 additions and 0 deletions

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Jose\Component\Encryption\Compression;
/**
* @deprecated This class is deprecated and will be removed in v4.0. Compression is not recommended for JWE.
*/
interface CompressionMethod
{
/**
* Returns the name of the method.
*/
public function name(): string;
/**
* Compress the data. Throws an exception in case of failure.
*
* @param string $data The data to compress
*/
public function compress(string $data): string;
/**
* Uncompress the data. Throws an exception in case of failure.
*
* @param string $data The data to uncompress
*/
public function uncompress(string $data): string;
}

View File

@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace Jose\Component\Encryption\Compression;
use InvalidArgumentException;
use function array_key_exists;
/**
* @deprecated This class is deprecated and will be removed in v4.0. Compression is not recommended for JWE.
*/
class CompressionMethodManager
{
/**
* @var CompressionMethod[]
*/
private array $compressionMethods = [];
/**
* @param CompressionMethod[] $methods
*/
public function __construct(iterable $methods = [])
{
foreach ($methods as $method) {
$this->add($method);
}
}
/**
* Returns true if the givn compression method is supported.
*/
public function has(string $name): bool
{
return array_key_exists($name, $this->compressionMethods);
}
/**
* This method returns the compression method with the given name. Throws an exception if the method is not
* supported.
*
* @param string $name The name of the compression method
*/
public function get(string $name): CompressionMethod
{
if (! $this->has($name)) {
throw new InvalidArgumentException(sprintf('The compression method "%s" is not supported.', $name));
}
return $this->compressionMethods[$name];
}
/**
* Returns the list of compression method names supported by the manager.
*
* @return string[]
*/
public function list(): array
{
return array_keys($this->compressionMethods);
}
/**
* Add the given compression method to the manager.
*/
protected function add(CompressionMethod $compressionMethod): void
{
$name = $compressionMethod->name();
$this->compressionMethods[$name] = $compressionMethod;
}
}

View File

@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace Jose\Component\Encryption\Compression;
use InvalidArgumentException;
/**
* @deprecated This class is deprecated and will be removed in v4.0. Compression is not recommended for JWE.
*/
class CompressionMethodManagerFactory
{
/**
* @var CompressionMethod[]
*/
private array $compressionMethods = [];
/**
* This method adds a compression method to this factory. The method is uniquely identified by an alias. This allows
* the same method to be added twice (or more) using several configuration options.
*/
public function add(string $alias, CompressionMethod $compressionMethod): void
{
$this->compressionMethods[$alias] = $compressionMethod;
}
/**
* Returns the list of compression method aliases supported by the factory.
*
* @return string[]
*/
public function aliases(): array
{
return array_keys($this->compressionMethods);
}
/**
* Returns all compression methods supported by this factory.
*
* @return CompressionMethod[]
*/
public function all(): array
{
return $this->compressionMethods;
}
/**
* Creates a compression method manager using the compression methods identified by the given aliases. If one of the
* aliases does not exist, an exception is thrown.
*
* @param string[] $aliases
*/
public function create(array $aliases): CompressionMethodManager
{
$compressionMethods = [];
foreach ($aliases as $alias) {
if (! isset($this->compressionMethods[$alias])) {
throw new InvalidArgumentException(sprintf(
'The compression method with the alias "%s" is not supported.',
$alias
));
}
$compressionMethods[] = $this->compressionMethods[$alias];
}
return new CompressionMethodManager($compressionMethods);
}
}

View File

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace Jose\Component\Encryption\Compression;
use InvalidArgumentException;
use Throwable;
use function is_string;
/**
* @deprecated This class is deprecated and will be removed in v4.0. Compression is not recommended for JWE.
*/
final class Deflate implements CompressionMethod
{
private int $compressionLevel = -1;
public function __construct(int $compressionLevel = -1)
{
if ($compressionLevel < -1 || $compressionLevel > 9) {
throw new InvalidArgumentException(
'The compression level can be given as 0 for no compression up to 9 for maximum compression. If -1 given, the default compression level will be the default compression level of the zlib library.'
);
}
$this->compressionLevel = $compressionLevel;
}
public function name(): string
{
return 'DEF';
}
public function compress(string $data): string
{
try {
$bin = gzdeflate($data, $this->getCompressionLevel());
if (! is_string($bin)) {
throw new InvalidArgumentException('Unable to encode the data');
}
return $bin;
} catch (Throwable $throwable) {
throw new InvalidArgumentException('Unable to compress data.', $throwable->getCode(), $throwable);
}
}
public function uncompress(string $data): string
{
try {
$bin = gzinflate($data);
if (! is_string($bin)) {
throw new InvalidArgumentException('Unable to encode the data');
}
return $bin;
} catch (Throwable $throwable) {
throw new InvalidArgumentException('Unable to uncompress data.', $throwable->getCode(), $throwable);
}
}
private function getCompressionLevel(): int
{
return $this->compressionLevel;
}
}