-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ed3afc
commit 685186c
Showing
8 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |