From 7429420781f042ab19a88e6b70e68682731898ec Mon Sep 17 00:00:00 2001 From: Zi Xing Date: Sun, 17 Apr 2022 18:34:35 -0400 Subject: [PATCH] Added ProjectConfiguration test and load JSON functions --- src/ncc/Abstracts/ExceptionCodes.php | 6 ++ src/ncc/Exceptions/MalformedJsonException.php | 14 ++- src/ncc/Objects/ProjectConfiguration.php | 33 ++++++- src/ncc/Utilities/Functions.php | 89 +++++++++++++++++++ .../load_configuration.php | 3 + 5 files changed, 141 insertions(+), 4 deletions(-) diff --git a/src/ncc/Abstracts/ExceptionCodes.php b/src/ncc/Abstracts/ExceptionCodes.php index 6e96081..391b0ba 100644 --- a/src/ncc/Abstracts/ExceptionCodes.php +++ b/src/ncc/Abstracts/ExceptionCodes.php @@ -7,6 +7,7 @@ use ncc\Exceptions\FileNotFoundException; use ncc\Exceptions\InvalidProjectConfigurationException; use ncc\Exceptions\InvalidScopeException; + use ncc\Exceptions\MalformedJsonException; /** * @author Zi Xing Narrakas @@ -38,4 +39,9 @@ * @see AccessDeniedException */ const AccessDeniedException = -1704; + + /** + * @see MalformedJsonException + */ + const MalformedJsonException = -1705; } \ No newline at end of file diff --git a/src/ncc/Exceptions/MalformedJsonException.php b/src/ncc/Exceptions/MalformedJsonException.php index c3bc323..7952448 100644 --- a/src/ncc/Exceptions/MalformedJsonException.php +++ b/src/ncc/Exceptions/MalformedJsonException.php @@ -3,10 +3,18 @@ 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); } } \ No newline at end of file diff --git a/src/ncc/Objects/ProjectConfiguration.php b/src/ncc/Objects/ProjectConfiguration.php index 3aae382..d4435af 100644 --- a/src/ncc/Objects/ProjectConfiguration.php +++ b/src/ncc/Objects/ProjectConfiguration.php @@ -2,7 +2,9 @@ namespace ncc\Objects; + use ncc\Exceptions\FileNotFoundException; use ncc\Exceptions\InvalidProjectConfigurationException; + use ncc\Exceptions\MalformedJsonException; use ncc\Objects\ProjectConfiguration\Assembly; use ncc\Objects\ProjectConfiguration\Build; use ncc\Objects\ProjectConfiguration\Project; @@ -54,7 +56,7 @@ */ public function validate(bool $throw_exception=false): bool { - if($this->Assembly->validate($throw_exception) == false) + if(!$this->Assembly->validate($throw_exception)) return false; 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 * @@ -91,4 +108,18 @@ 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)); + } } \ No newline at end of file diff --git a/src/ncc/Utilities/Functions.php b/src/ncc/Utilities/Functions.php index 5950414..388956a 100644 --- a/src/ncc/Utilities/Functions.php +++ b/src/ncc/Utilities/Functions.php @@ -2,12 +2,21 @@ namespace ncc\Utilities; + use ncc\Exceptions\FileNotFoundException; + use ncc\Exceptions\MalformedJsonException; + /** * @author Zi Xing Narrakas * @copyright Copyright (C) 2022-2022. Nosial - All Rights Reserved. */ 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 * @@ -37,4 +46,84 @@ 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)); + } } \ No newline at end of file diff --git a/tests/project_configuration/load_configuration.php b/tests/project_configuration/load_configuration.php index 992fe8f..455f2cc 100644 --- a/tests/project_configuration/load_configuration.php +++ b/tests/project_configuration/load_configuration.php @@ -2,3 +2,6 @@ require(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'autoload.php'); + $object = \ncc\Objects\ProjectConfiguration::fromFile(__DIR__ . DIRECTORY_SEPARATOR . 'project.json'); + + var_dump($object); \ No newline at end of file