Initial xentral_oss_20.3.c9ffacf

This commit is contained in:
Alex
2021-05-21 08:49:41 +02:00
parent 5406e4a551
commit 34e5ac43d9
18884 changed files with 2109867 additions and 0 deletions
@@ -0,0 +1,27 @@
<?php
namespace Xentral\Components\Exporter\Json;
final class JsonConfig
{
/** @var int $options */
private $options;
/**
* @see https://www.php.net/manual/en/json.constants.php
*
* @param int $options
*/
public function __construct($options = 0)
{
$this->options = $options;
}
/**
* @return int
*/
public function getOptions()
{
return $this->options;
}
}
@@ -0,0 +1,69 @@
<?php
namespace Xentral\Components\Exporter\Json;
use Xentral\Components\Exporter\Exception\FileExistsException;
use Xentral\Components\Exporter\Exception\InvalidResourceException;
final class JsonExporter
{
/** @var JsonConfig $config */
private $config;
/**
* @param JsonConfig|null $config
*/
public function __construct(JsonConfig $config = null)
{
if ($config === null) {
$config = new JsonConfig();
}
$this->config = $config;
}
/**
* @param string $filePath Resource used for writing
* @param array $data Multi-dimentional array
*
* @throws \Xentral\Components\Exporter\Exception\InvalidJsonException
* @throws \Xentral\Components\Exporter\Exception\ResourceWriteException
* @return void
*/
public function export($filePath, $data)
{
$resource = $this->exportToResource($filePath, $data);
fclose($resource);
}
/**
* Same as ::export() beside that the created resource will be returned
*
* @param string $filePath Resource used for writing
* @param array $data Multi-dimentional array
*
* @throws \Xentral\Components\Exporter\Exception\InvalidJsonException
* @throws \Xentral\Components\Exporter\Exception\ResourceWriteException
* @return resource
*/
public function exportToResource($filePath, $data)
{
if (is_file($filePath)) {
throw new FileExistsException(sprintf('File creation failed. File "%s" already exists.', $filePath));
}
// 'x+' = Create and open for reading and writing.
// File pointer will be placed at the beginning of the file.
// If the file already exists `fopen` will return false.
// 'b' = Enable binary mode
$resource = @fopen($filePath, 'x+b');
if ($resource === false) {
throw new InvalidResourceException(sprintf('Failed to open resource for file path "%s".', $filePath));
}
$writer = new JsonWriter($resource, $this->config);
$writer->write($data);
return $resource;
}
}
@@ -0,0 +1,90 @@
<?php
namespace Xentral\Components\Exporter\Json;
use JsonSerializable;
use Xentral\Components\Exporter\Exception\InvalidJsonException;
use Xentral\Components\Exporter\Exception\InvalidResourceException;
use Xentral\Components\Exporter\Exception\PhpExtensionMissingException;
use Xentral\Components\Exporter\Exception\ResourceWriteException;
final class JsonWriter
{
/** @var resource $handle */
private $handle;
/** @var JsonConfig $config */
private $config;
/**
* @param resource $handle
* @param JsonConfig|null $config
*
* @throws InvalidResourceException If resource is not writable or invalid
* @throws PhpExtensionMissingException If mbstring is missing
*/
public function __construct($handle, JsonConfig $config = null)
{
if (!is_resource($handle)) {
throw new InvalidResourceException('First parameter is not a valid resource.');
}
if (!$this->isStreamWritable($handle)) {
throw new InvalidResourceException('Resource is not writable.');
}
if (!function_exists('mb_convert_encoding')) {
throw new PhpExtensionMissingException('Required PHP extension "mbstring" is missing.');
}
if ($config === null) {
$config = new JsonConfig();
}
$this->config = $config;
$this->handle = $handle;
}
/**
* @param array|JsonSerializable $data
*
* @throws PhpExtensionMissingException If json is missing
* @throws ResourceWriteException
* @throws InvalidJsonException
*/
public function write($data)
{
if (!function_exists('json_encode')) {
throw new PhpExtensionMissingException('Required PHP extension "json" is missing.');
}
$jsonOptions = $this->config->getOptions();
$jsonString = @json_encode($data, $jsonOptions);
if ($jsonString === false) {
throw InvalidJsonException::fromJsonError(json_last_error());
}
$writeResult = @fwrite($this->handle, $jsonString);
if ($writeResult === false) {
throw new ResourceWriteException("JSON could not be written to resource.");
}
}
/**
* @param resource $handle
*
* @return bool
*/
private function isStreamWritable($handle)
{
$meta = stream_get_meta_data($handle);
$currentMode = $meta['mode'];
$writeModes = ['r+', 'w', 'w+', 'a', 'a+', 'x', 'x+', 'c', 'c+'];
foreach ($writeModes as $writeMode) {
if (strpos($currentMode, $writeMode) !== false) {
return true;
}
}
return false;
}
}