156 lines
4.5 KiB
PHP
156 lines
4.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace LDC\LDCCRM;
|
||
|
|
|
||
|
|
use Dotenv\Dotenv;
|
||
|
|
use Illuminate\Filesystem\Filesystem;
|
||
|
|
use Illuminate\Translation\FileLoader;
|
||
|
|
use Illuminate\Translation\Translator;
|
||
|
|
use Illuminate\Validation\Factory;
|
||
|
|
use LDC\LDCCRM\Enums\HttpMethods;
|
||
|
|
use LDC\LDCCRM\Transport\Request;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This is the LDCCRM utility class
|
||
|
|
*
|
||
|
|
* @category Utility
|
||
|
|
* @uses \LDC\LDCCRM\Enums\HttpMethods
|
||
|
|
* @uses \LDC\LDCCRM\Transport\Request
|
||
|
|
*
|
||
|
|
* @package LDCCrmClient
|
||
|
|
* @subpackage utilities
|
||
|
|
*/
|
||
|
|
class Utils
|
||
|
|
{
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Package infos
|
||
|
|
*
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
private static $packageInfos;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The env config
|
||
|
|
*
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
private static $config;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create a new Validator instance.
|
||
|
|
*
|
||
|
|
* @param array $data
|
||
|
|
* @param array $rules
|
||
|
|
* @param array $messages
|
||
|
|
* @param array $customAttributes
|
||
|
|
* @return \Illuminate\Validation\Validator
|
||
|
|
*/
|
||
|
|
public static function makeValidator(
|
||
|
|
array $data,
|
||
|
|
array $rules,
|
||
|
|
array $messages = [],
|
||
|
|
array $customAttributes = []
|
||
|
|
) {
|
||
|
|
$filesystem = new Filesystem();
|
||
|
|
$fileLoader = new FileLoader($filesystem, dirname(dirname(__FILE__)) . '/resources/lang');
|
||
|
|
$fileLoader->addNamespace('lang', dirname(dirname(__FILE__)) . '/resources/lang');
|
||
|
|
$fileLoader->load('en', 'validation', 'lang');
|
||
|
|
$translator = new Translator($fileLoader, 'en');
|
||
|
|
$translator->load('lang', 'validation', 'en');
|
||
|
|
$factory = new Factory($translator);
|
||
|
|
|
||
|
|
return $factory->make($data, $rules, $messages, $customAttributes);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get config
|
||
|
|
*
|
||
|
|
* @param string $key
|
||
|
|
* @param string $default
|
||
|
|
* @return string|null
|
||
|
|
* @throws \Dotenv\Exception\InvalidPathException|\Dotenv\Exception\InvalidEncodingException|\Dotenv\Exception\InvalidFileException
|
||
|
|
*/
|
||
|
|
public static function getConfig(string $key, string $default = null): ?string
|
||
|
|
{
|
||
|
|
$value = null;
|
||
|
|
if (function_exists('config')) {
|
||
|
|
$value = \config($key, $default);
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
if (!self::$config) {
|
||
|
|
$dotenv = Dotenv::createImmutable(__DIR__ . '/..');
|
||
|
|
self::$config = $dotenv->load();
|
||
|
|
}
|
||
|
|
} catch (\Dotenv\Exception\InvalidPathException $e) {
|
||
|
|
return $value;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (isset(self::$config[$key])) {
|
||
|
|
$value = self::$config[$key];
|
||
|
|
}
|
||
|
|
|
||
|
|
return $value;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Generate a Request from the route name
|
||
|
|
*
|
||
|
|
* @param string $routeName
|
||
|
|
* @param array $query params to pass on to the route
|
||
|
|
* @param array $data http body params
|
||
|
|
* @return Request
|
||
|
|
*/
|
||
|
|
public static function getResquest(string $routeName, array $query = [], array $data = []): Request
|
||
|
|
{
|
||
|
|
// convert bool to integer
|
||
|
|
$query = \array_map(function ($v) {
|
||
|
|
return is_bool($v) ? (int)$v : $v;
|
||
|
|
}, $query);
|
||
|
|
$data = \array_map(function ($v) {
|
||
|
|
return is_bool($v) ? (int)$v : $v;
|
||
|
|
}, $data);
|
||
|
|
|
||
|
|
$routes = \json_decode(\file_get_contents(__DIR__ . '/routes.json'), true);
|
||
|
|
if (!isset($routes[$routeName])) {
|
||
|
|
throw new \Exception("Unknown route $routeName from the crm");
|
||
|
|
}
|
||
|
|
$route = $routes[$routeName];
|
||
|
|
$matches = [];
|
||
|
|
$matchedIndex = [];
|
||
|
|
\preg_match_all('/{([\w-]*)}/', $route['uri'], $matches);
|
||
|
|
if (\count($matches[0])) {
|
||
|
|
foreach ($matches[0] as $k => $match) {
|
||
|
|
if (!isset($query[$matches[1][$k]])) {
|
||
|
|
throw new \Exception(sprintf('Missing required parameter %s', $matches[1][$k]));
|
||
|
|
}
|
||
|
|
$route['uri'] = str_replace($match, $query[$matches[1][$k]], $route['uri']);
|
||
|
|
$matchedIndex[$matches[1][$k]] = $query[$matches[1][$k]];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$query = \array_flip($query);
|
||
|
|
foreach ($matchedIndex as $key) {
|
||
|
|
unset($query[$key]);
|
||
|
|
}
|
||
|
|
$query = \array_flip($query);
|
||
|
|
if (count($query)) {
|
||
|
|
$route['uri'] = sprintf('%s?%s', $route['uri'], \http_build_query($query));
|
||
|
|
}
|
||
|
|
return new Request(HttpMethods::make($route['methods'][0]), $route['uri'], [], $data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get composer.json infos
|
||
|
|
*
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public static function getPackageInfo(): array
|
||
|
|
{
|
||
|
|
if (!static::$packageInfos) {
|
||
|
|
static::$packageInfos = \json_decode(\file_get_contents(__DIR__ . '/../composer.json'), true);
|
||
|
|
}
|
||
|
|
return static::$packageInfos;
|
||
|
|
}
|
||
|
|
}
|