Added ProjectConfiguration test and load JSON functions

This commit is contained in:
Zi Xing 2022-04-17 18:34:35 -04:00
parent 75aae6f486
commit 7429420781
5 changed files with 141 additions and 4 deletions

View file

@ -7,6 +7,7 @@
use ncc\Exceptions\FileNotFoundException; use ncc\Exceptions\FileNotFoundException;
use ncc\Exceptions\InvalidProjectConfigurationException; use ncc\Exceptions\InvalidProjectConfigurationException;
use ncc\Exceptions\InvalidScopeException; use ncc\Exceptions\InvalidScopeException;
use ncc\Exceptions\MalformedJsonException;
/** /**
* @author Zi Xing Narrakas * @author Zi Xing Narrakas
@ -38,4 +39,9 @@
* @see AccessDeniedException * @see AccessDeniedException
*/ */
const AccessDeniedException = -1704; const AccessDeniedException = -1704;
/**
* @see MalformedJsonException
*/
const MalformedJsonException = -1705;
} }

View file

@ -3,10 +3,18 @@
namespace ncc\Exceptions; namespace ncc\Exceptions;
class MalformedJsonException extends \Exception use Exception;
use ncc\Abstracts\ExceptionCodes;
use Throwable;
class MalformedJsonException extends Exception
{ {
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null) /**
* @param string $message
* @param Throwable|null $previous
*/
public function __construct(string $message = "", ?Throwable $previous = null)
{ {
parent::__construct($message, $code, $previous); parent::__construct($message, ExceptionCodes::MalformedJsonException, $previous);
} }
} }

View file

@ -2,7 +2,9 @@
namespace ncc\Objects; namespace ncc\Objects;
use ncc\Exceptions\FileNotFoundException;
use ncc\Exceptions\InvalidProjectConfigurationException; use ncc\Exceptions\InvalidProjectConfigurationException;
use ncc\Exceptions\MalformedJsonException;
use ncc\Objects\ProjectConfiguration\Assembly; use ncc\Objects\ProjectConfiguration\Assembly;
use ncc\Objects\ProjectConfiguration\Build; use ncc\Objects\ProjectConfiguration\Build;
use ncc\Objects\ProjectConfiguration\Project; use ncc\Objects\ProjectConfiguration\Project;
@ -54,7 +56,7 @@
*/ */
public function validate(bool $throw_exception=false): bool public function validate(bool $throw_exception=false): bool
{ {
if($this->Assembly->validate($throw_exception) == false) if(!$this->Assembly->validate($throw_exception))
return false; return false;
return true; return true;
@ -75,6 +77,21 @@
]; ];
} }
/**
* Writes a json representation of the object to a file
*
* @param string $path
* @param bool $bytecode
* @return void
* @throws MalformedJsonException
* @noinspection PhpMissingReturnTypeInspection
* @noinspection PhpUnused
*/
public function toFile(string $path, bool $bytecode=false)
{
Functions::encodeJsonFile($this->toArray($bytecode), $path, Functions::FORCE_ARRAY);
}
/** /**
* Constructs the object from an array representation * Constructs the object from an array representation
* *
@ -91,4 +108,18 @@
return $ProjectConfigurationObject; return $ProjectConfigurationObject;
} }
/**
* Loads the object from a file representation
*
* @param string $path
* @return ProjectConfiguration
* @throws FileNotFoundException
* @throws MalformedJsonException
* @noinspection PhpUnused
*/
public static function fromFile(string $path): ProjectConfiguration
{
return ProjectConfiguration::fromArray(Functions::loadJsonFile($path, Functions::FORCE_ARRAY));
}
} }

View file

@ -2,12 +2,21 @@
namespace ncc\Utilities; namespace ncc\Utilities;
use ncc\Exceptions\FileNotFoundException;
use ncc\Exceptions\MalformedJsonException;
/** /**
* @author Zi Xing Narrakas * @author Zi Xing Narrakas
* @copyright Copyright (C) 2022-2022. Nosial - All Rights Reserved. * @copyright Copyright (C) 2022-2022. Nosial - All Rights Reserved.
*/ */
class Functions class Functions
{ {
public const FORCE_ARRAY = 0b0001;
public const PRETTY = 0b0010;
public const ESCAPE_UNICODE = 0b0100;
/** /**
* Calculates a byte-code representation of the input using CRC32 * Calculates a byte-code representation of the input using CRC32
* *
@ -37,4 +46,84 @@
return null; return null;
} }
/**
* Loads a json file
*
* @param string $path
* @param int $flags
* @return mixed
* @throws FileNotFoundException
* @throws MalformedJsonException
* @noinspection PhpMissingReturnTypeInspection
*/
public static function loadJsonFile(string $path, int $flags=0)
{
if(!file_exists($path))
{
throw new FileNotFoundException($path);
}
return self::loadJson(file_get_contents($path), $flags);
}
/**
* Parses a json string
*
* @param string $json
* @param int $flags
* @return mixed
* @throws MalformedJsonException
* @noinspection PhpMissingReturnTypeInspection
*/
public static function loadJson(string $json, int $flags=0)
{
$forceArray = (bool) ($flags & self::FORCE_ARRAY);
$json_decoded = json_decode($json, $forceArray, 512, JSON_BIGINT_AS_STRING);
if($json_decoded == null && json_last_error() !== JSON_ERROR_NONE)
{
throw new MalformedJsonException(json_last_error_msg() . ' (' . json_last_error() . ')');
}
return $json_decoded;
}
/**
* Returns the JSON representation of a value. Accepts flag Json::PRETTY.
*
* @param mixed $value
* @param int $flags
* @return string
* @throws MalformedJsonException
* @noinspection PhpMissingParamTypeInspection
*/
public static function encodeJson($value, int $flags=0): string
{
$flags = ($flags & self::ESCAPE_UNICODE ? 0 : JSON_UNESCAPED_UNICODE)
| JSON_UNESCAPED_SLASHES
| ($flags & self::PRETTY ? JSON_PRETTY_PRINT : 0)
| (defined('JSON_PRESERVE_ZERO_FRACTION') ? JSON_PRESERVE_ZERO_FRACTION : 0); // since PHP 5.6.6 & PECL JSON-C 1.3.7
$json = json_encode($value, $flags);
if ($error = json_last_error())
{
throw new MalformedJsonException(json_last_error_msg() . ' (' . json_last_error() . ')');
}
return $json;
}
/**
* Writes a json file to disk
*
* @param $value
* @param string $path
* @param int $flags
* @return void
* @throws MalformedJsonException
*/
public static function encodeJsonFile($value, string $path, int $flags=0)
{
file_put_contents($path, self::encodeJson($value, $flags));
}
} }

View file

@ -2,3 +2,6 @@
require(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'autoload.php'); require(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'autoload.php');
$object = \ncc\Objects\ProjectConfiguration::fromFile(__DIR__ . DIRECTORY_SEPARATOR . 'project.json');
var_dump($object);