More code style updates
This commit is contained in:
parent
45449b6907
commit
2e67b49447
@ -5,22 +5,12 @@ use PhpCsFixer\{Config, Finder};
|
||||
|
||||
$finder = Finder::create()
|
||||
->in([
|
||||
__DIR__,
|
||||
__DIR__ . '/app',
|
||||
__DIR__ . '/src',
|
||||
__DIR__ . '/tests',
|
||||
__DIR__ . '/tools',
|
||||
])
|
||||
->exclude([
|
||||
'apidocs',
|
||||
'build',
|
||||
'coverage',
|
||||
'frontEndSrc',
|
||||
'phinx',
|
||||
'public',
|
||||
'tools',
|
||||
'tmp',
|
||||
'vendor',
|
||||
'views',
|
||||
'templates',
|
||||
]);
|
||||
|
||||
return (new Config())
|
||||
@ -45,7 +35,7 @@ return (new Config())
|
||||
'blank_line_after_opening_tag' => false,
|
||||
'blank_line_before_statement' => [
|
||||
'statements' => [
|
||||
'case',
|
||||
// 'case',
|
||||
'continue',
|
||||
'declare',
|
||||
'default',
|
||||
@ -128,12 +118,12 @@ return (new Config())
|
||||
'noise_remaining_usages_exclude' => [],
|
||||
],
|
||||
'escape_implicit_backslashes' => [
|
||||
'double_quoted' => true,
|
||||
'heredoc_syntax' => true,
|
||||
'double_quoted' => false,
|
||||
'heredoc_syntax' => false,
|
||||
'single_quoted' => false,
|
||||
],
|
||||
'explicit_indirect_variable' => true,
|
||||
'explicit_string_variable' => true,
|
||||
'explicit_indirect_variable' => false,
|
||||
'explicit_string_variable' => false,
|
||||
'final_class' => false,
|
||||
'final_internal_class' => [
|
||||
'annotation_exclude' => ['@no-final'],
|
||||
@ -167,7 +157,7 @@ return (new Config())
|
||||
],
|
||||
'group_import' => true,
|
||||
'header_comment' => false, // false by default
|
||||
'heredoc_indentation' => ['indentation' => 'start_plus_one'],
|
||||
// 'heredoc_indentation' => ['indentation' => 'start_plus_one'],
|
||||
'heredoc_to_nowdoc' => true,
|
||||
'implode_call' => true,
|
||||
'include' => true,
|
||||
@ -232,8 +222,7 @@ return (new Config())
|
||||
'allow_unused_params' => true,
|
||||
'remove_inheritdoc' => false,
|
||||
],
|
||||
'no_trailing_comma_in_list_call' => true,
|
||||
'no_trailing_comma_in_singleline_array' => true,
|
||||
'no_trailing_comma_in_singleline' => true,
|
||||
'no_trailing_whitespace' => true,
|
||||
'no_trailing_whitespace_in_comment' => true,
|
||||
'no_trailing_whitespace_in_string' => true,
|
||||
@ -270,9 +259,16 @@ return (new Config())
|
||||
'ordered_class_elements' => [
|
||||
'order' => [
|
||||
'use_trait',
|
||||
'constant',
|
||||
'property',
|
||||
'method',
|
||||
'case',
|
||||
'constant_public',
|
||||
'constant_protected',
|
||||
'constant_private',
|
||||
'property_public',
|
||||
'property_protected',
|
||||
'property_private',
|
||||
'construct',
|
||||
'destruct',
|
||||
'magic',
|
||||
],
|
||||
'sort_algorithm' => 'none',
|
||||
],
|
||||
|
@ -42,6 +42,11 @@ class Controller
|
||||
{
|
||||
use ContainerAware;
|
||||
|
||||
/**
|
||||
* The global configuration object
|
||||
*/
|
||||
public ConfigInterface $config;
|
||||
|
||||
/**
|
||||
* The authentication object
|
||||
*/
|
||||
@ -52,11 +57,6 @@ class Controller
|
||||
*/
|
||||
protected CacheInterface $cache;
|
||||
|
||||
/**
|
||||
* The global configuration object
|
||||
*/
|
||||
public ConfigInterface $config;
|
||||
|
||||
/**
|
||||
* Request object
|
||||
*/
|
||||
@ -120,7 +120,7 @@ class Controller
|
||||
Event::on(EventType::RESET_CACHE_KEY, fn (string $key) => $this->cache->delete($key));
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Set the current url in the session as the target of a future redirect
|
||||
*
|
||||
* @throws ContainerException
|
||||
@ -128,37 +128,37 @@ class Controller
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\CodeCoverageIgnore]
|
||||
public function setSessionRedirect(?string $url = NULL): void
|
||||
{
|
||||
$serverParams = $this->request->getServerParams();
|
||||
{
|
||||
$serverParams = $this->request->getServerParams();
|
||||
|
||||
if ( ! array_key_exists('HTTP_REFERER', $serverParams))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if ( ! array_key_exists('HTTP_REFERER', $serverParams))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$util = $this->container->get('util');
|
||||
$doubleFormPage = $serverParams['HTTP_REFERER'] === $this->request->getUri();
|
||||
$isLoginPage = str_contains($serverParams['HTTP_REFERER'], 'login');
|
||||
$util = $this->container->get('util');
|
||||
$doubleFormPage = $serverParams['HTTP_REFERER'] === $this->request->getUri();
|
||||
$isLoginPage = str_contains($serverParams['HTTP_REFERER'], 'login');
|
||||
|
||||
// Don't attempt to set the redirect url if
|
||||
// the page is one of the form type pages,
|
||||
// and the previous page is also a form type
|
||||
if ($doubleFormPage || $isLoginPage)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// Don't attempt to set the redirect url if
|
||||
// the page is one of the form type pages,
|
||||
// and the previous page is also a form type
|
||||
if ($doubleFormPage || $isLoginPage)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (NULL === $url)
|
||||
{
|
||||
$url = $util->isViewPage()
|
||||
? (string) $this->request->getUri()
|
||||
: $serverParams['HTTP_REFERER'];
|
||||
}
|
||||
if (NULL === $url)
|
||||
{
|
||||
$url = $util->isViewPage()
|
||||
? (string) $this->request->getUri()
|
||||
: $serverParams['HTTP_REFERER'];
|
||||
}
|
||||
|
||||
$this->session->set('redirect_url', $url);
|
||||
}
|
||||
$this->session->set('redirect_url', $url);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Redirect to the url previously set in the session
|
||||
*
|
||||
* If one is not set, redirect to default url
|
||||
@ -167,147 +167,147 @@ class Controller
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\CodeCoverageIgnore]
|
||||
public function sessionRedirect(): void
|
||||
{
|
||||
$target = $this->session->get('redirect_url') ?? '/';
|
||||
{
|
||||
$target = $this->session->get('redirect_url') ?? '/';
|
||||
|
||||
$this->redirect($target, 303);
|
||||
$this->session->set('redirect_url', NULL);
|
||||
}
|
||||
$this->redirect($target, 303);
|
||||
$this->session->set('redirect_url', NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Check if the current user is authenticated, else error and exit
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\CodeCoverageIgnore]
|
||||
protected function checkAuth(): void
|
||||
{
|
||||
if ( ! $this->auth->isAuthenticated())
|
||||
{
|
||||
$this->errorPage(
|
||||
403,
|
||||
'Forbidden',
|
||||
'You must <a href="/login">log in</a> to perform this action.'
|
||||
);
|
||||
}
|
||||
}
|
||||
{
|
||||
if ( ! $this->auth->isAuthenticated())
|
||||
{
|
||||
$this->errorPage(
|
||||
403,
|
||||
'Forbidden',
|
||||
'You must <a href="/login">log in</a> to perform this action.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Get the string output of a partial template
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\CodeCoverageIgnore]
|
||||
protected function loadPartial(HtmlView $view, string $template, array $data = []): string
|
||||
{
|
||||
$router = $this->container->get('dispatcher');
|
||||
{
|
||||
$router = $this->container->get('dispatcher');
|
||||
|
||||
if (isset($this->baseData))
|
||||
{
|
||||
$data = array_merge($this->baseData, $data);
|
||||
}
|
||||
if (isset($this->baseData))
|
||||
{
|
||||
$data = array_merge($this->baseData, $data);
|
||||
}
|
||||
|
||||
$route = $router->getRoute();
|
||||
$data['route_path'] = $route !== FALSE ? $route->path : '';
|
||||
$route = $router->getRoute();
|
||||
$data['route_path'] = $route !== FALSE ? $route->path : '';
|
||||
|
||||
$templatePath = _dir($this->config->get('view_path'), "{$template}.php");
|
||||
$templatePath = _dir($this->config->get('view_path'), "{$template}.php");
|
||||
|
||||
if ( ! is_file($templatePath))
|
||||
{
|
||||
throw new InvalidArgumentException("Invalid template : {$template}");
|
||||
}
|
||||
if ( ! is_file($templatePath))
|
||||
{
|
||||
throw new InvalidArgumentException("Invalid template : {$template}");
|
||||
}
|
||||
|
||||
return $view->renderTemplate($templatePath, $data);
|
||||
}
|
||||
return $view->renderTemplate($templatePath, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Render a template with header and footer
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\CodeCoverageIgnore]
|
||||
protected function renderFullPage(HtmlView $view, string $template, array $data): HtmlView
|
||||
{
|
||||
$csp = [
|
||||
"default-src 'self' media.kitsu.io kitsu-production-media.s3.us-west-002.backblazeb2.com",
|
||||
"object-src 'none'",
|
||||
"child-src 'self' *.youtube.com polyfill.io",
|
||||
];
|
||||
{
|
||||
$csp = [
|
||||
"default-src 'self' media.kitsu.io kitsu-production-media.s3.us-west-002.backblazeb2.com",
|
||||
"object-src 'none'",
|
||||
"child-src 'self' *.youtube.com polyfill.io",
|
||||
];
|
||||
|
||||
$view->addHeader('Content-Security-Policy', implode('; ', $csp));
|
||||
$view->appendOutput($this->loadPartial($view, 'header', $data));
|
||||
$view->addHeader('Content-Security-Policy', implode('; ', $csp));
|
||||
$view->appendOutput($this->loadPartial($view, 'header', $data));
|
||||
|
||||
if (array_key_exists('message', $data) && is_array($data['message']))
|
||||
{
|
||||
$view->appendOutput($this->loadPartial($view, 'message', $data['message']));
|
||||
}
|
||||
if (array_key_exists('message', $data) && is_array($data['message']))
|
||||
{
|
||||
$view->appendOutput($this->loadPartial($view, 'message', $data['message']));
|
||||
}
|
||||
|
||||
$view->appendOutput($this->loadPartial($view, $template, $data));
|
||||
$view->appendOutput($this->loadPartial($view, 'footer', $data));
|
||||
$view->appendOutput($this->loadPartial($view, $template, $data));
|
||||
$view->appendOutput($this->loadPartial($view, 'footer', $data));
|
||||
|
||||
return $view;
|
||||
}
|
||||
return $view;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* 404 action
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\CodeCoverageIgnore]
|
||||
public function notFound(
|
||||
string $title = 'Sorry, page not found',
|
||||
string $message = 'Page Not Found'
|
||||
): void {
|
||||
$this->outputHTML('404', [
|
||||
'title' => $title,
|
||||
'message' => $message,
|
||||
], NULL, 404);
|
||||
string $title = 'Sorry, page not found',
|
||||
string $message = 'Page Not Found'
|
||||
): void {
|
||||
$this->outputHTML('404', [
|
||||
'title' => $title,
|
||||
'message' => $message,
|
||||
], NULL, 404);
|
||||
|
||||
exit();
|
||||
}
|
||||
exit();
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Display a generic error page
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\CodeCoverageIgnore]
|
||||
public function errorPage(int $httpCode, string $title, string $message, string $longMessage = ''): void
|
||||
{
|
||||
$this->outputHTML('error', [
|
||||
'title' => $title,
|
||||
'message' => $message,
|
||||
'long_message' => $longMessage,
|
||||
], NULL, $httpCode);
|
||||
}
|
||||
{
|
||||
$this->outputHTML('error', [
|
||||
'title' => $title,
|
||||
'message' => $message,
|
||||
'long_message' => $longMessage,
|
||||
], NULL, $httpCode);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Redirect to the default controller/url from an empty path
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\CodeCoverageIgnore]
|
||||
public function redirectToDefaultRoute(): void
|
||||
{
|
||||
$defaultType = $this->config->get('default_list');
|
||||
$this->redirect($this->urlGenerator->defaultUrl($defaultType), 303);
|
||||
}
|
||||
{
|
||||
$defaultType = $this->config->get('default_list');
|
||||
$this->redirect($this->urlGenerator->defaultUrl($defaultType), 303);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Set a session flash variable to display a message on
|
||||
* next page load
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\CodeCoverageIgnore]
|
||||
public function setFlashMessage(string $message, string $type = 'info'): void
|
||||
{
|
||||
static $messages;
|
||||
{
|
||||
static $messages;
|
||||
|
||||
if ( ! $messages)
|
||||
{
|
||||
$messages = [];
|
||||
}
|
||||
if ( ! $messages)
|
||||
{
|
||||
$messages = [];
|
||||
}
|
||||
|
||||
$messages[] = [
|
||||
'message_type' => $type,
|
||||
'message' => $message,
|
||||
];
|
||||
$messages[] = [
|
||||
'message_type' => $type,
|
||||
'message' => $message,
|
||||
];
|
||||
|
||||
$this->session->setFlash('message', $messages);
|
||||
}
|
||||
$this->session->setFlash('message', $messages);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper for consistent page titles
|
||||
@ -319,38 +319,38 @@ class Controller
|
||||
return implode(' · ', $parts);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Add a message box to the page
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\CodeCoverageIgnore]
|
||||
protected function showMessage(HtmlView $view, string $type, string $message): string
|
||||
{
|
||||
return $this->loadPartial($view, 'message', [
|
||||
'message_type' => $type,
|
||||
'message' => $message,
|
||||
]);
|
||||
}
|
||||
{
|
||||
return $this->loadPartial($view, 'message', [
|
||||
'message_type' => $type,
|
||||
'message' => $message,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Output a template to HTML, using the provided data
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\CodeCoverageIgnore]
|
||||
protected function outputHTML(string $template, array $data = [], ?HtmlView $view = NULL, int $code = 200): void
|
||||
{
|
||||
if (NULL === $view)
|
||||
{
|
||||
$view = new HtmlView($this->container);
|
||||
}
|
||||
{
|
||||
if (NULL === $view)
|
||||
{
|
||||
$view = new HtmlView($this->container);
|
||||
}
|
||||
|
||||
$view->setStatusCode($code);
|
||||
$this->renderFullPage($view, $template, $data)->send();
|
||||
}
|
||||
$view->setStatusCode($code);
|
||||
$this->renderFullPage($view, $template, $data)->send();
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Output a JSON Response
|
||||
*
|
||||
* @param int $code - the http status code
|
||||
@ -358,23 +358,23 @@ class Controller
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\CodeCoverageIgnore]
|
||||
protected function outputJSON(mixed $data, int $code): void
|
||||
{
|
||||
JsonView::new()
|
||||
->setOutput($data)
|
||||
->setStatusCode($code)
|
||||
->send();
|
||||
}
|
||||
{
|
||||
JsonView::new()
|
||||
->setOutput($data)
|
||||
->setStatusCode($code)
|
||||
->send();
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Redirect to the selected page
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\CodeCoverageIgnore]
|
||||
protected function redirect(string $url, int $code): void
|
||||
{
|
||||
HttpView::new()
|
||||
->redirect($url, $code)
|
||||
->send();
|
||||
}
|
||||
{
|
||||
HttpView::new()
|
||||
->redirect($url, $code)
|
||||
->send();
|
||||
}
|
||||
}
|
||||
|
||||
// End of BaseController.php
|
||||
|
@ -69,37 +69,6 @@ final class Dispatcher extends RoutingBase
|
||||
$this->outputRoutes = $this->setupRoutes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current route object, if one matches
|
||||
*/
|
||||
public function getRoute(): Route|false
|
||||
{
|
||||
$logger = $this->container->getLogger();
|
||||
|
||||
$rawRoute = $this->request->getUri()->getPath();
|
||||
$routePath = '/' . trim($rawRoute, '/');
|
||||
|
||||
if ($logger !== NULL)
|
||||
{
|
||||
$logger->info('Dispatcher - Routing data from get_route method');
|
||||
$logger->info(print_r([
|
||||
'route_path' => $routePath,
|
||||
], TRUE));
|
||||
}
|
||||
|
||||
return $this->matcher->match($this->request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of routes applied
|
||||
*
|
||||
* @return mixed[]
|
||||
*/
|
||||
public function getOutputRoutes(): array
|
||||
{
|
||||
return $this->outputRoutes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the current route
|
||||
*
|
||||
@ -141,6 +110,37 @@ final class Dispatcher extends RoutingBase
|
||||
$this->call($controllerName, $actionMethod, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current route object, if one matches
|
||||
*/
|
||||
public function getRoute(): Route|false
|
||||
{
|
||||
$logger = $this->container->getLogger();
|
||||
|
||||
$rawRoute = $this->request->getUri()->getPath();
|
||||
$routePath = '/' . trim($rawRoute, '/');
|
||||
|
||||
if ($logger !== NULL)
|
||||
{
|
||||
$logger->info('Dispatcher - Routing data from get_route method');
|
||||
$logger->info(print_r([
|
||||
'route_path' => $routePath,
|
||||
], TRUE));
|
||||
}
|
||||
|
||||
return $this->matcher->match($this->request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of routes applied
|
||||
*
|
||||
* @return mixed[]
|
||||
*/
|
||||
public function getOutputRoutes(): array
|
||||
{
|
||||
return $this->outputRoutes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse out the arguments for the appropriate controller for
|
||||
* the current route
|
||||
|
@ -83,16 +83,16 @@ final class FormGenerator
|
||||
];
|
||||
$params['strict'] = TRUE;
|
||||
unset($params['attribs']['id']);
|
||||
break;
|
||||
break;
|
||||
|
||||
case 'string':
|
||||
$params['type'] = 'text';
|
||||
break;
|
||||
break;
|
||||
|
||||
case 'select':
|
||||
$params['type'] = 'select';
|
||||
$params['options'] = array_flip($form['options']);
|
||||
break;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
|
@ -36,6 +36,19 @@ final class MenuGenerator extends UrlGenerator
|
||||
*/
|
||||
protected ServerRequestInterface $request;
|
||||
|
||||
/**
|
||||
* MenuGenerator constructor.
|
||||
*
|
||||
* @throws ContainerException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
private function __construct(ContainerInterface $container)
|
||||
{
|
||||
parent::__construct($container);
|
||||
$this->helper = $container->get('html-helper');
|
||||
$this->request = $container->get('request');
|
||||
}
|
||||
|
||||
public static function new(ContainerInterface $container): self
|
||||
{
|
||||
return new self($container);
|
||||
@ -80,19 +93,6 @@ final class MenuGenerator extends UrlGenerator
|
||||
return (string) $this->helper->ul();
|
||||
}
|
||||
|
||||
/**
|
||||
* MenuGenerator constructor.
|
||||
*
|
||||
* @throws ContainerException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
private function __construct(ContainerInterface $container)
|
||||
{
|
||||
parent::__construct($container);
|
||||
$this->helper = $container->get('html-helper');
|
||||
$this->request = $container->get('request');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the full menu structure from the config files
|
||||
*
|
||||
|
@ -20,37 +20,6 @@ use Stringable;
|
||||
|
||||
abstract class AbstractType implements ArrayAccess, Countable, Stringable
|
||||
{
|
||||
/**
|
||||
* Populate values for un-serializing data
|
||||
*/
|
||||
public static function __set_state(mixed $properties): self
|
||||
{
|
||||
return new static($properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the shape of the object, and return the array equivalent
|
||||
*/
|
||||
final public static function check(array $data = []): ?array
|
||||
{
|
||||
$currentClass = static::class;
|
||||
|
||||
if (get_parent_class($currentClass) !== FALSE)
|
||||
{
|
||||
return static::class::from($data)->toArray();
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static constructor
|
||||
*/
|
||||
final public static function from(mixed $data): static
|
||||
{
|
||||
return new static($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the properties by using the constructor
|
||||
*/
|
||||
@ -73,6 +42,14 @@ abstract class AbstractType implements ArrayAccess, Countable, Stringable
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate values for un-serializing data
|
||||
*/
|
||||
public static function __set_state(mixed $properties): self
|
||||
{
|
||||
return new static($properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* See if a property is set
|
||||
*/
|
||||
@ -123,6 +100,29 @@ abstract class AbstractType implements ArrayAccess, Countable, Stringable
|
||||
return print_r($this, TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the shape of the object, and return the array equivalent
|
||||
*/
|
||||
final public static function check(array $data = []): ?array
|
||||
{
|
||||
$currentClass = static::class;
|
||||
|
||||
if (get_parent_class($currentClass) !== FALSE)
|
||||
{
|
||||
return static::class::from($data)->toArray();
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static constructor
|
||||
*/
|
||||
final public static function from(mixed $data): static
|
||||
{
|
||||
return new static($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementing ArrayAccess
|
||||
*/
|
||||
@ -203,23 +203,23 @@ abstract class AbstractType implements ArrayAccess, Countable, Stringable
|
||||
|
||||
#[\PHPUnit\Framework\Attributes\CodeCoverageIgnore]
|
||||
final protected function fromObject(mixed $parent = NULL): float|null|bool|int|array|string
|
||||
{
|
||||
$object = $parent ?? $this;
|
||||
{
|
||||
$object = $parent ?? $this;
|
||||
|
||||
if (is_scalar($object) || $object === NULL)
|
||||
{
|
||||
return $object;
|
||||
}
|
||||
if (is_scalar($object) || $object === NULL)
|
||||
{
|
||||
return $object;
|
||||
}
|
||||
|
||||
$output = [];
|
||||
$output = [];
|
||||
|
||||
foreach ($object as $key => $value)
|
||||
{
|
||||
$output[$key] = (is_scalar($value) || empty($value))
|
||||
? $value
|
||||
: $this->fromObject((array) $value);
|
||||
}
|
||||
foreach ($object as $key => $value)
|
||||
{
|
||||
$output[$key] = (is_scalar($value) || empty($value))
|
||||
? $value
|
||||
: $this->fromObject((array) $value);
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
@ -65,14 +65,6 @@ class ArrayType
|
||||
'pop' => 'array_pop',
|
||||
];
|
||||
|
||||
/**
|
||||
* Create an ArrayType wrapper class from an array
|
||||
*/
|
||||
public static function from(array $arr): ArrayType
|
||||
{
|
||||
return new ArrayType($arr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an ArrayType wrapper class
|
||||
*/
|
||||
@ -108,6 +100,14 @@ class ArrayType
|
||||
throw new InvalidArgumentException("Method '{$method}' does not exist");
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an ArrayType wrapper class from an array
|
||||
*/
|
||||
public static function from(array $arr): ArrayType
|
||||
{
|
||||
return new ArrayType($arr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the passed key exist in the current array?
|
||||
*/
|
||||
|
@ -41,7 +41,8 @@ use const MB_CASE_TITLE;
|
||||
/**
|
||||
* Vendored, slightly modernized version of Stringy
|
||||
*/
|
||||
abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess
|
||||
{
|
||||
/**
|
||||
* An instance's string.
|
||||
*/
|
||||
@ -80,10 +81,20 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
);
|
||||
}
|
||||
|
||||
$this->str = (string)$str;
|
||||
$this->str = (string) $str;
|
||||
$this->encoding = $encoding ?: mb_internal_encoding();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value in $str.
|
||||
*
|
||||
* @return string The current value of the $str property
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Stringy object and assigns both str and encoding properties
|
||||
* the supplied values. $str is cast to a string prior to assignment, and if
|
||||
@ -93,25 +104,15 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
*
|
||||
* @param mixed $str Value to modify, after being cast to string
|
||||
* @param string|null $encoding The character encoding
|
||||
* @return static A Stringy object
|
||||
* @throws InvalidArgumentException if an array or object without a
|
||||
* __toString method is passed as the first argument
|
||||
* @return static A Stringy object
|
||||
*/
|
||||
public static function create(mixed $str = '', ?string $encoding = NULL): self
|
||||
{
|
||||
return new static($str, $encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value in $str.
|
||||
*
|
||||
* @return string The current value of the $str property
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string with $string appended.
|
||||
*
|
||||
@ -190,7 +191,7 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
|
||||
$stringy->str = preg_replace_callback(
|
||||
'/[\d]+(.)?/u',
|
||||
static fn($match) => mb_strtoupper($match[0], $encoding),
|
||||
static fn ($match) => mb_strtoupper($match[0], $encoding),
|
||||
$stringy->str
|
||||
);
|
||||
|
||||
@ -395,7 +396,7 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
$endOfStr = mb_strtolower($endOfStr, $this->encoding);
|
||||
}
|
||||
|
||||
return (string)$substring === $endOfStr;
|
||||
return (string) $substring === $endOfStr;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -585,8 +586,8 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
{
|
||||
return \mb_strpos(
|
||||
$this->str,
|
||||
(string)$needle,
|
||||
(int)$offset,
|
||||
(string) $needle,
|
||||
(int) $offset,
|
||||
$this->encoding
|
||||
);
|
||||
}
|
||||
@ -605,8 +606,8 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
{
|
||||
return mb_strrpos(
|
||||
$this->str,
|
||||
(string)$needle,
|
||||
(int)$offset,
|
||||
(string) $needle,
|
||||
(int) $offset,
|
||||
$this->encoding
|
||||
);
|
||||
}
|
||||
@ -813,7 +814,8 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
if ($char === mb_substr($otherStr, $i, 1, $encoding))
|
||||
{
|
||||
$longestCommonPrefix .= $char;
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
@ -842,7 +844,8 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
if ($char === mb_substr($otherStr, -$i, 1, $encoding))
|
||||
{
|
||||
$longestCommonSuffix = $char . $longestCommonSuffix;
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
@ -898,7 +901,8 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
$len = $table[$i][$j];
|
||||
$end = $i;
|
||||
}
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
$table[$i][$j] = 0;
|
||||
}
|
||||
@ -941,7 +945,7 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
public function offsetExists(mixed $offset): bool
|
||||
{
|
||||
$length = $this->length();
|
||||
$offset = (int)$offset;
|
||||
$offset = (int) $offset;
|
||||
|
||||
if ($offset >= 0)
|
||||
{
|
||||
@ -958,13 +962,13 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
* does not exist.
|
||||
*
|
||||
* @param mixed $offset The index from which to retrieve the char
|
||||
* @return string The character at the specified index
|
||||
* @throws OutOfBoundsException If the positive or negative offset does
|
||||
* not exist
|
||||
* @return string The character at the specified index
|
||||
*/
|
||||
public function offsetGet(mixed $offset): string
|
||||
{
|
||||
$offset = (int)$offset;
|
||||
$offset = (int) $offset;
|
||||
$length = $this->length();
|
||||
|
||||
if (($offset >= 0 && $length <= $offset) || $length < abs($offset))
|
||||
@ -1012,9 +1016,9 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $padType One of 'left', 'right', 'both'
|
||||
* @return static Object with a padded $str
|
||||
* @throws /InvalidArgumentException If $padType isn't one of 'right',
|
||||
* 'left' or 'both'
|
||||
* @return static Object with a padded $str
|
||||
*/
|
||||
public function pad(int $length, string $padStr = ' ', string $padType = 'right'): self
|
||||
{
|
||||
@ -1313,7 +1317,7 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
$startOfStr = mb_strtolower($startOfStr, $this->encoding);
|
||||
}
|
||||
|
||||
return (string)$substring === $startOfStr;
|
||||
return (string) $substring === $startOfStr;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1359,13 +1363,16 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
if ($end === NULL)
|
||||
{
|
||||
$length = $this->length();
|
||||
} elseif ($end >= 0 && $end <= $start)
|
||||
}
|
||||
elseif ($end >= 0 && $end <= $start)
|
||||
{
|
||||
return static::create('', $this->encoding);
|
||||
} elseif ($end < 0)
|
||||
}
|
||||
elseif ($end < 0)
|
||||
{
|
||||
$length = $this->length() + $end - $start;
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
$length = $end - $start;
|
||||
}
|
||||
@ -1412,7 +1419,8 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
if ($functionExists)
|
||||
{
|
||||
$array = mb_split($pattern, $this->str, $limit);
|
||||
} elseif ($this->supportsEncoding())
|
||||
}
|
||||
elseif ($this->supportsEncoding())
|
||||
{
|
||||
$array = \preg_split("/{$pattern}/", $this->str, $limit);
|
||||
}
|
||||
@ -1549,7 +1557,7 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
|
||||
$stringy = static::create($match[0], $encoding);
|
||||
|
||||
return (string)$stringy->toLowerCase()->upperCaseFirst();
|
||||
return (string) $stringy->toLowerCase()->upperCaseFirst();
|
||||
},
|
||||
$stringy->str
|
||||
);
|
||||
@ -1624,10 +1632,10 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
}
|
||||
if (is_numeric($this->str))
|
||||
{
|
||||
return (int)($this->str) > 0;
|
||||
return (int) ($this->str) > 0;
|
||||
}
|
||||
|
||||
return (bool)$this->regexReplace('[[:space:]]', '')->str;
|
||||
return (bool) $this->regexReplace('[[:space:]]', '')->str;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2024,7 +2032,7 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
],
|
||||
];
|
||||
|
||||
$charsArray[$language] = isset($languageSpecific[$language]) ? $languageSpecific[$language] : [];
|
||||
$charsArray[$language] = $languageSpecific[$language] ?? [];
|
||||
|
||||
return $charsArray[$language];
|
||||
}
|
||||
@ -2104,7 +2112,7 @@ abstract class Stringy implements Countable, IteratorAggregate, ArrayAccess {
|
||||
}
|
||||
if ($this->supportsEncoding())
|
||||
{
|
||||
$option = str_replace('r', '', (string)$option);
|
||||
$option = str_replace('r', '', (string) $option);
|
||||
|
||||
return \preg_replace("/{$pattern}/u{$option}", $replacement, $string);
|
||||
}
|
||||
|
@ -3,10 +3,6 @@
|
||||
use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector;
|
||||
use Rector\CodeQuality\Rector\For_\{ForRepeatedCountToOwnVariableRector, ForToForeachRector};
|
||||
use Rector\CodeQuality\Rector\If_\{ConsecutiveNullCompareReturnsToNullCoalesceQueueRector, SimplifyIfElseToTernaryRector, SimplifyIfReturnBoolRector};
|
||||
use Rector\CodingStyle\Rector\String_\SymplifyQuoteEscapeRector;
|
||||
use Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector;
|
||||
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
|
||||
use Rector\Set\ValueObject\LevelSetList;
|
||||
use Rector\CodeQuality\Rector\Ternary\{SimplifyTautologyTernaryRector, SwitchNegatedTernaryRector};
|
||||
use Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector;
|
||||
use Rector\CodingStyle\Rector\Class_\AddArrayDefaultToArrayPropertyRector;
|
||||
@ -19,6 +15,7 @@ use Rector\CodingStyle\Rector\FuncCall\
|
||||
CountArrayToEmptyArrayComparisonRector,
|
||||
VersionCompareFuncCallToConstantRector};
|
||||
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
|
||||
use Rector\CodingStyle\Rector\String_\SymplifyQuoteEscapeRector;
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\DeadCode\Rector\ClassMethod\{RemoveUselessParamTagRector, RemoveUselessReturnTagRector};
|
||||
use Rector\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector;
|
||||
@ -26,7 +23,10 @@ use Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector;
|
||||
use Rector\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector;
|
||||
use Rector\EarlyReturn\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector;
|
||||
use Rector\EarlyReturn\Rector\If_\{ChangeIfElseValueAssignToEarlyReturnRector, RemoveAlwaysElseRector};
|
||||
use Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector;
|
||||
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
|
||||
use Rector\PHPUnit\Set\PHPUnitSetList;
|
||||
use Rector\Set\ValueObject\LevelSetList;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\{AddMethodCallBasedStrictParamTypeRector, ParamTypeByMethodCallTypeRector, ParamTypeByParentCallTypeRector};
|
||||
use Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector;
|
||||
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
|
||||
@ -34,7 +34,7 @@ use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
|
||||
return static function (RectorConfig $config): void {
|
||||
// Import names with use statements
|
||||
$config->importNames();
|
||||
$config->importShortClasses(false);
|
||||
$config->importShortClasses(FALSE);
|
||||
|
||||
$config->sets([
|
||||
LevelSetList::UP_TO_PHP_81,
|
||||
|
Loading…
Reference in New Issue
Block a user