[CORE][UTIL] Moved classes from util to core
And splitted up Common
This commit is contained in:
parent
f628665589
commit
dd559402cd
@ -23,12 +23,12 @@ services:
|
|||||||
resource: '../src/Controller'
|
resource: '../src/Controller'
|
||||||
tags: ['controller.service_arguments']
|
tags: ['controller.service_arguments']
|
||||||
|
|
||||||
App\Util\RouteLoader:
|
App\Core\RouteLoader:
|
||||||
tags: ['routing.loader']
|
tags: ['routing.loader']
|
||||||
|
|
||||||
|
|
||||||
# Wrapper arround Doctrine's StaticPHP metadata driver
|
# Wrapper arround Doctrine's StaticPHP metadata driver
|
||||||
app.util.schemadef_driver:
|
app.core.schemadef_driver:
|
||||||
class: App\Util\SchemaDefDriver
|
class: App\Core\SchemaDefDriver
|
||||||
arguments:
|
arguments:
|
||||||
- '%kernel.project_dir%/src/Entity'
|
- '%kernel.project_dir%/src/Entity'
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
|
|
||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
use App\Util\GSEvent as Event;
|
use App\Core\GSEvent as Event;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
|
||||||
class NetworkPublic extends AbstractController
|
class NetworkPublic extends AbstractController
|
||||||
|
@ -38,7 +38,7 @@
|
|||||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace App\Util;
|
namespace App\Core;
|
||||||
|
|
||||||
use Psr\Container\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
@ -29,7 +29,7 @@
|
|||||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace App\Util;
|
namespace App\Core;
|
||||||
|
|
||||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||||
use Symfony\Component\EventDispatcher\GenericEvent;
|
use Symfony\Component\EventDispatcher\GenericEvent;
|
||||||
@ -43,7 +43,6 @@ abstract class GSEvent
|
|||||||
* bool next - Allow the other handlers to process the event
|
* bool next - Allow the other handlers to process the event
|
||||||
*/
|
*/
|
||||||
public const stop = false;
|
public const stop = false;
|
||||||
|
|
||||||
public const next = true;
|
public const next = true;
|
||||||
|
|
||||||
private static EventDispatcherInterface $dispatcher;
|
private static EventDispatcherInterface $dispatcher;
|
@ -32,7 +32,7 @@
|
|||||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace App\Util;
|
namespace App\Core;
|
||||||
|
|
||||||
// Locale category constants are usually predefined, but may not be
|
// Locale category constants are usually predefined, but may not be
|
||||||
// on some systems such as Win32.
|
// on some systems such as Win32.
|
@ -29,7 +29,7 @@
|
|||||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace App\Util;
|
namespace App\Core;
|
||||||
|
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
@ -44,6 +44,7 @@ class Log
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple static wrappers around Monolog's functions
|
* Simple static wrappers around Monolog's functions
|
||||||
|
*
|
||||||
* @param string $msg
|
* @param string $msg
|
||||||
*/
|
*/
|
||||||
public static function emergency(string $msg): void
|
public static function emergency(string $msg): void
|
@ -31,10 +31,10 @@
|
|||||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace App\Util;
|
namespace App\Core;
|
||||||
|
|
||||||
use App\Util\GSEvent as Event;
|
|
||||||
use Functional as F;
|
use Functional as F;
|
||||||
|
use GSEvent as Event;
|
||||||
|
|
||||||
abstract class ModulesManager
|
abstract class ModulesManager
|
||||||
{
|
{
|
||||||
@ -54,7 +54,7 @@ abstract class ModulesManager
|
|||||||
|
|
||||||
// Register event handlers
|
// Register event handlers
|
||||||
$methods = get_class_methods($class);
|
$methods = get_class_methods($class);
|
||||||
$events = F\select($methods, F\partial_right('App\Util\Common::startsWith', 'on'));
|
$events = F\select($methods, F\partial_right('App\Util\Formatting::startsWith', 'on'));
|
||||||
F\map($events,
|
F\map($events,
|
||||||
function (string $m) use ($class) {
|
function (string $m) use ($class) {
|
||||||
Event::addHandler(substr($m, 2), [$class, $m]);
|
Event::addHandler(substr($m, 2), [$class, $m]);
|
@ -30,7 +30,7 @@
|
|||||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace App\Util;
|
namespace App\Core;
|
||||||
|
|
||||||
use Symfony\Component\Config\Loader\Loader;
|
use Symfony\Component\Config\Loader\Loader;
|
||||||
use Symfony\Component\Routing\Route;
|
use Symfony\Component\Routing\Route;
|
@ -28,7 +28,7 @@
|
|||||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace App\Util;
|
namespace App\Core;
|
||||||
|
|
||||||
use Doctrine\Persistence\Mapping\ClassMetadata;
|
use Doctrine\Persistence\Mapping\ClassMetadata;
|
||||||
use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver;
|
use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver;
|
@ -44,7 +44,7 @@ class SchemaDefPass implements CompilerPassInterface
|
|||||||
{
|
{
|
||||||
$container->findDefinition('doctrine.orm.default_metadata_driver')
|
$container->findDefinition('doctrine.orm.default_metadata_driver')
|
||||||
->addMethodCall('addDriver',
|
->addMethodCall('addDriver',
|
||||||
[new Reference('app.util.schemadef_driver'), 'App\\Entity']
|
[new Reference('app.core.schemadef_driver'), 'App\\Entity']
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,7 @@
|
|||||||
namespace App;
|
namespace App;
|
||||||
|
|
||||||
use App\DependencyInjection\Compiler\SchemaDefPass;
|
use App\DependencyInjection\Compiler\SchemaDefPass;
|
||||||
|
|
||||||
use const PHP_VERSION_ID;
|
use const PHP_VERSION_ID;
|
||||||
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
|
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
|
||||||
use Symfony\Component\Config\Loader\LoaderInterface;
|
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
// {{{ License
|
||||||
* This file is part of GNU social - https://www.gnu.org/software/social
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
||||||
*
|
//
|
||||||
* GNU social is free software: you can redistribute it and/or modify
|
// GNU social is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
// (at your option) any later version.
|
||||||
*
|
//
|
||||||
* GNU social is distributed in the hope that it will be useful,
|
// GNU social is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU Affero General Public License for more details.
|
// GNU Affero General Public License for more details.
|
||||||
*
|
//
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
* along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
// }}}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define social's main routes
|
* Define social's main routes
|
||||||
@ -31,7 +31,7 @@
|
|||||||
namespace App\Routes;
|
namespace App\Routes;
|
||||||
|
|
||||||
use App\Controller\NetworkPublic;
|
use App\Controller\NetworkPublic;
|
||||||
use App\Util\RouteLoader;
|
use App\Core\RouteLoader;
|
||||||
|
|
||||||
abstract class Main
|
abstract class Main
|
||||||
{
|
{
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
// {{{ License
|
||||||
* This file is part of GNU social - https://www.gnu.org/software/social
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
||||||
*
|
//
|
||||||
* GNU social is free software: you can redistribute it and/or modify
|
// GNU social is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
// (at your option) any later version.
|
||||||
*
|
//
|
||||||
* GNU social is distributed in the hope that it will be useful,
|
// GNU social is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU Affero General Public License for more details.
|
// GNU Affero General Public License for more details.
|
||||||
*
|
//
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
* along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
// }}}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Common utility functions
|
* Common utility functions
|
||||||
@ -30,11 +30,6 @@
|
|||||||
|
|
||||||
namespace App\Util;
|
namespace App\Util;
|
||||||
|
|
||||||
use const DIRECTORY_SEPARATOR;
|
|
||||||
use Functional as F;
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use Symfony\Component\Config\Definition\Exception\Exception;
|
|
||||||
|
|
||||||
abstract class Common
|
abstract class Common
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -50,152 +45,4 @@ abstract class Common
|
|||||||
// TODO: implement it x)
|
// TODO: implement it x)
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Normalize path by converting \ to /
|
|
||||||
*
|
|
||||||
* @param string $path
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function normalizePath(string $path): string
|
|
||||||
{
|
|
||||||
if (DIRECTORY_SEPARATOR !== '/') {
|
|
||||||
$path = strtr($path, DIRECTORY_SEPARATOR, '/');
|
|
||||||
}
|
|
||||||
return $path;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get plugin name from it's path, or null if not a plugin
|
|
||||||
*
|
|
||||||
* @param string $path
|
|
||||||
*
|
|
||||||
* @return null|string
|
|
||||||
*/
|
|
||||||
public static function pluginFromPath(string $path): ?string
|
|
||||||
{
|
|
||||||
$plug = strpos($path, '/plugins/');
|
|
||||||
if ($plug === false) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
$cut = $plug + strlen('/plugins/');
|
|
||||||
$cut2 = strpos($path, '/', $cut);
|
|
||||||
if ($cut2) {
|
|
||||||
$final = substr($path, $cut, $cut2 - $cut);
|
|
||||||
} else {
|
|
||||||
// We might be running directly from the plugins dir?
|
|
||||||
// If so, there's no place to store locale info.
|
|
||||||
throw new Exception('The GNU social install dir seems to contain a piece named plugin');
|
|
||||||
}
|
|
||||||
return $final;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check whether $haystack starts with $needle
|
|
||||||
*
|
|
||||||
* @param array|string $haystack if array, check that all strings start with $needle
|
|
||||||
* @param string $needle
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public static function startsWith($haystack, string $needle): bool
|
|
||||||
{
|
|
||||||
if (is_string($haystack)) {
|
|
||||||
$length = strlen($needle);
|
|
||||||
return substr($haystack, 0, $length) === $needle;
|
|
||||||
}
|
|
||||||
return F\every($haystack,
|
|
||||||
function ($haystack) use ($needle) {
|
|
||||||
return self::startsWith($haystack, $needle);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check whether $haystack ends with $needle
|
|
||||||
*
|
|
||||||
* @param array|string $haystack if array, check that all strings end with $needle
|
|
||||||
* @param string $needle
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public static function endsWith($haystack, string $needle)
|
|
||||||
{
|
|
||||||
if (is_string($haystack)) {
|
|
||||||
$length = strlen($needle);
|
|
||||||
if ($length == 0) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return substr($haystack, -$length) === $needle;
|
|
||||||
}
|
|
||||||
return F\every($haystack,
|
|
||||||
function ($haystack) use ($needle) {
|
|
||||||
return self::endsWith($haystack, $needle);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Call $func with only abs($count) arguments, taken either from the
|
|
||||||
* left or right depending on the sign
|
|
||||||
*
|
|
||||||
* @param callable $func
|
|
||||||
* @param int $count
|
|
||||||
*
|
|
||||||
* @return callable
|
|
||||||
*/
|
|
||||||
public static function arity(callable $func, int $count): callable
|
|
||||||
{
|
|
||||||
return function (...$args) use ($func, $count) {
|
|
||||||
if ($count > 0) {
|
|
||||||
return call_user_func_array($func, F\take_left($args, $count));
|
|
||||||
}
|
|
||||||
return call_user_func_array($func, F\take_right($args, -$count));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $str
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function camelCaseToSnakeCase(string $str): string
|
|
||||||
{
|
|
||||||
return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $str));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $str
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function snakeCaseToCamelCase(string $str): string
|
|
||||||
{
|
|
||||||
return implode('', F\map(preg_split('/[\b_]/', $str), self::arity('ucfirst', 1)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Indent $in, a string or array, $level levels
|
|
||||||
*
|
|
||||||
* @param array|string $in
|
|
||||||
* @param int $level
|
|
||||||
* @param int $count
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function indent($in, int $level = 1, int $count = 2): string
|
|
||||||
{
|
|
||||||
if (is_string($in)) {
|
|
||||||
return self::indent(explode("\n", $in), $level, $count);
|
|
||||||
} elseif (is_array($in)) {
|
|
||||||
$indent = str_repeat(' ', $count * $level);
|
|
||||||
return implode("\n", F\map(F\select($in,
|
|
||||||
self::arity(function ($s) {
|
|
||||||
return $s != '';
|
|
||||||
}, 1)),
|
|
||||||
function ($val) use ($indent) {
|
|
||||||
return F\concat($indent . $val);
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
throw new InvalidArgumentException('Common:indent first parameter must be either an array or a string. Input was: ' . $in);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
169
src/Util/Formatting.php
Normal file
169
src/Util/Formatting.php
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// {{{ License
|
||||||
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
||||||
|
//
|
||||||
|
// GNU social is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// GNU social is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* String formatting utilities
|
||||||
|
*
|
||||||
|
* @package GNUsocial
|
||||||
|
* @category Util
|
||||||
|
*
|
||||||
|
* @author Hugo Sales <hugo@fc.up.pt>
|
||||||
|
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
|
||||||
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Util;
|
||||||
|
|
||||||
|
use const DIRECTORY_SEPARATOR;
|
||||||
|
use Functional;
|
||||||
|
use Functional as F;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use Symfony\Component\Config\Definition\Exception\Exception;
|
||||||
|
|
||||||
|
abstract class Formatting
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Normalize path by converting \ to /
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function normalizePath(string $path): string
|
||||||
|
{
|
||||||
|
if (DIRECTORY_SEPARATOR !== '/') {
|
||||||
|
$path = strtr($path, DIRECTORY_SEPARATOR, '/');
|
||||||
|
}
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get plugin name from it's path, or null if not a plugin
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
*
|
||||||
|
* @return null|string
|
||||||
|
*/
|
||||||
|
public static function pluginFromPath(string $path): ?string
|
||||||
|
{
|
||||||
|
$plug = strpos($path, '/plugins/');
|
||||||
|
if ($plug === false) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
$cut = $plug + strlen('/plugins/');
|
||||||
|
$cut2 = strpos($path, '/', $cut);
|
||||||
|
if ($cut2) {
|
||||||
|
$final = substr($path, $cut, $cut2 - $cut);
|
||||||
|
} else {
|
||||||
|
// We might be running directly from the plugins dir?
|
||||||
|
// If so, there's no place to store locale info.
|
||||||
|
throw new Exception('The GNU social install dir seems to contain a piece named plugin');
|
||||||
|
}
|
||||||
|
return $final;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether $haystack starts with $needle
|
||||||
|
*
|
||||||
|
* @param array|string $haystack if array, check that all strings start with $needle
|
||||||
|
* @param string $needle
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function startsWith($haystack, string $needle): bool
|
||||||
|
{
|
||||||
|
if (is_string($haystack)) {
|
||||||
|
$length = strlen($needle);
|
||||||
|
return substr($haystack, 0, $length) === $needle;
|
||||||
|
}
|
||||||
|
return F\every($haystack,
|
||||||
|
function ($haystack) use ($needle) {
|
||||||
|
return self::startsWith($haystack, $needle);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether $haystack ends with $needle
|
||||||
|
*
|
||||||
|
* @param array|string $haystack if array, check that all strings end with $needle
|
||||||
|
* @param string $needle
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function endsWith($haystack, string $needle)
|
||||||
|
{
|
||||||
|
if (is_string($haystack)) {
|
||||||
|
$length = strlen($needle);
|
||||||
|
if ($length == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return substr($haystack, -$length) === $needle;
|
||||||
|
}
|
||||||
|
return F\every($haystack,
|
||||||
|
function ($haystack) use ($needle) {
|
||||||
|
return self::endsWith($haystack, $needle);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $str
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function camelCaseToSnakeCase(string $str): string
|
||||||
|
{
|
||||||
|
return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $str));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $str
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function snakeCaseToCamelCase(string $str): string
|
||||||
|
{
|
||||||
|
return implode('', F\map(preg_split('/[\b_]/', $str), Functional::arity('ucfirst', 1)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indent $in, a string or array, $level levels
|
||||||
|
*
|
||||||
|
* @param array|string $in
|
||||||
|
* @param int $level
|
||||||
|
* @param int $count
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function indent($in, int $level = 1, int $count = 2): string
|
||||||
|
{
|
||||||
|
if (is_string($in)) {
|
||||||
|
return self::indent(explode("\n", $in), $level, $count);
|
||||||
|
} elseif (is_array($in)) {
|
||||||
|
$indent = str_repeat(' ', $count * $level);
|
||||||
|
return implode("\n", F\map(F\select($in,
|
||||||
|
Functional::arity(function ($s) {
|
||||||
|
return $s != '';
|
||||||
|
}, 1)),
|
||||||
|
function ($val) use ($indent) {
|
||||||
|
return F\concat($indent . $val);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
throw new InvalidArgumentException('Formatting::indent\'s first parameter must be either an array or a string. Input was: ' . $in);
|
||||||
|
}
|
||||||
|
}
|
55
src/Util/Functional.php
Normal file
55
src/Util/Functional.php
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// {{{ License
|
||||||
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
||||||
|
//
|
||||||
|
// GNU social is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// GNU social is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Functional utilities
|
||||||
|
*
|
||||||
|
* @package GNUsocial
|
||||||
|
* @category Util
|
||||||
|
*
|
||||||
|
* @author Hugo Sales <hugo@fc.up.pt>
|
||||||
|
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
|
||||||
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Util;
|
||||||
|
|
||||||
|
use Functional as F;
|
||||||
|
|
||||||
|
abstract class Functional
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Call $func with only abs($count) arguments, taken either from the
|
||||||
|
* left or right depending on the sign
|
||||||
|
*
|
||||||
|
* @param callable $func
|
||||||
|
* @param int $count
|
||||||
|
*
|
||||||
|
* @return callable
|
||||||
|
*/
|
||||||
|
public static function arity(callable $func, int $count): callable
|
||||||
|
{
|
||||||
|
return function (...$args) use ($func, $count) {
|
||||||
|
if ($count > 0) {
|
||||||
|
return call_user_func_array($func, F\take_left($args, $count));
|
||||||
|
}
|
||||||
|
return call_user_func_array($func, F\take_right($args, -$count));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
// {{{ License
|
// {{{ License
|
||||||
// This file is part of GNU social - https://www.gnu.org/software/soci
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
||||||
//
|
//
|
||||||
// GNU social is free software: you can redistribute it and/or modify
|
// GNU social is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
@ -16,10 +17,6 @@
|
|||||||
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
namespace App\Util;
|
|
||||||
|
|
||||||
use Functional as F;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HTML Abstraction
|
* HTML Abstraction
|
||||||
*
|
*
|
||||||
@ -27,6 +24,11 @@ use Functional as F;
|
|||||||
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
|
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
|
||||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
namespace App\Util;
|
||||||
|
|
||||||
|
use Functional as F;
|
||||||
|
|
||||||
abstract class HTML
|
abstract class HTML
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user