Skip to content

Commit

Permalink
Add Automatic checks and service actions structure
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandr-diudiun authored Aug 10, 2022
1 parent 21c5b90 commit 7918fc9
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
class Validator extends OpisValidator
{
private static $instance;
private static string $validatorsFolderPath = './validators';
private static $files = [];
private static string $validatorsFolderPath = 'validators';
private static string $schemaUrl = 'https://validators';

private function __construct()
Expand All @@ -24,21 +25,39 @@ public function __wakeup()
{
}

public static function initiateValidationSchemas(string $path): void
public static function isValidatorExists(string $serviceActionFilePath): bool
{
return isset(self::$files[self::$validatorsFolderPath . '/' . $serviceActionFilePath]);
}

public static function initiateValidationSchemas(string $rootPath): void
{
self::$instance = new OpisValidator();

self::$validatorsFolderPath = $path;
$validatorsFiles = scandir(self::$validatorsFolderPath);
self::$validatorsFolderPath = $rootPath;
self::scanValidatorsInFolder(self::$validatorsFolderPath);
}

private static function scanValidatorsInFolder(string $scanPath)
{
$validatorsFiles = scandir($scanPath);

foreach ($validatorsFiles as $file) {
if (is_file(self::$validatorsFolderPath . '/' . $file)) {
self::$instance->loader()->resolver()->registerFile('https://validators/' . $file, self::$validatorsFolderPath . '/' . $file);
if (!in_array($file, [".", ".."])) {

$path = $scanPath . '/' . $file;

if (is_file($path)) {
self::$instance->loader()->resolver()->registerFile(self::$schemaUrl . '/' . $path, $path);
self::$files[$path] = self::$schemaUrl . '/' . $path;
} elseif (is_dir($path)) {
self::scanValidatorsInFolder($path);
}
}
}
}

public static function obj()
public static function obj(): self
{
if (!(self::$instance instanceof OpisValidator)) {
throw new \Exception("Please initiate Validation schema first by calling `initiateValidationSchemas` method", 500);
Expand Down Expand Up @@ -67,7 +86,7 @@ private static function customFormatKey(): string

public static function validateIncomeData(\stdClass $data, string $jsonFileName = ''): void
{
$result = self::$instance->validate($data, self::$schemaUrl . '/' . $jsonFileName);
$result = self::$instance->validate($data, self::$schemaUrl . '/' . self::$validatorsFolderPath . '/' . $jsonFileName);
if ($result->hasError()) {
$formatter = new ErrorFormatter;
$error = $result->error();
Expand Down

0 comments on commit 7918fc9

Please sign in to comment.