Skip to content

Commit

Permalink
1.0 validator
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandr-diudiun committed Jul 7, 2022
1 parent 1ed3afc commit 685186c
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Oleksandr Diudiun

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# opis
DB Libriary for mysqli, mysqlnd and php 8.1
==========================
Install
--------
```composer require memcrab/db```
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "memcrab/opis",
"description": "PHP Validator Lib for Memcrab Core API",
"keywords": [
"validator",
"opis",
"lib",
"memcrab"
],
"homepage": "http://memcrab.com",
"license": "MIT",
"authors": [
{
"name": "Oleksandr Diudiun",
"email": "[email protected]"
}
],
"type": "package",
"require": {
"php": ">=8.0",
"opis/json-schema": "opis/json-schema:^2.3"
},
"require-dev": {
"phpunit/phpunit": "9.*.*"
},
"autoload": {
"psr-4": {
"Memcrab\\Opis\\": "src/"
}
}
}
4 changes: 4 additions & 0 deletions examples/validation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
12 changes: 12 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<phpunit bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="true">
<testsuites>
<testsuite name="Unit Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
75 changes: 75 additions & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Memcrab\Opis;

use \Opis\JsonSchema\Validator as OpisValidator;
use \Opis\JsonSchema\Errors\ErrorFormatter;
use \Opis\JsonSchema\Errors\ValidationError;

class Validator extends OpisValidator
{
private static $instance;

private function __construct()
{
}
private function __clone()
{
}
public function __wakeup()
{
}

public static function setSchemasFolderPath(string $path): void
{
$this->validatorsFolderPath = $path;
}

public static function obj()
{
if (!(self::$instance instanceof OpisValidator)) {
self::$instance = new self();

self::$instance->validatorsFolderPath = "./validation";
$validatorsFiles = scandir(self::$instance->validatorsFolderPath);

foreach ($validatorsFiles as $file) {
if (is_file(self::$instance->validatorsFolderPath . '/' . $file)) {
self::$instance->resolver()->registerFile('https://validators/' . $file, self::$instance->validatorsFolderPath . '/' . $file);
}
}
}
return self::$instance;
}

private function customFormat(ValidationError $error): array
{
$formatter = new ErrorFormatter;
return [
'formattedMessage' => $formatter->formatErrorMessage($error),
'contents' => $error->schema()->info()->data(),
];
}

private function customFormatKey(): string
{
return 'errors';
}

public function validateIncomeData(\stdClass $data, string $jsonFileName = ''): array
{
$result = $this->validate($data, 'https://validators/' . $jsonFileName);
if ($result->hasError()) {
$formatter = new ErrorFormatter;
$error = $result->error();
$ErroResult = $formatter->format($error, false, $this->customFormat(...), $this->customFormatKey(...));
}
return [
'status' => $result->isValid(),
'data' => isset($ErroResult['errors']['formattedMessage']) ? $ErroResult['errors']['formattedMessage'] : '',
'code' => (isset($ErroResult['errors']['contents']->apiCode)) ? (int)$ErroResult['errors']['contents']->apiCode : 400
];
}
}
15 changes: 15 additions & 0 deletions tests/Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
require_once __DIR__ . "/../vendor/autoload.php";


/**
* Corresponding Class to test Router class
*
* For each class in your library, there should be a corresponding Unit-Test for it
* Unit-Tests should be as much as possible independent from other test going on.
*
* @author Oleksandr Diudiun
*/
class Test extends PHPUnit_Framework_TestCase
{
}

0 comments on commit 685186c

Please sign in to comment.