From 5650168638d86459d956cd842fa6d53fc0566a69 Mon Sep 17 00:00:00 2001 From: Julien Turbide Date: Wed, 21 Feb 2024 19:54:36 -0500 Subject: [PATCH] moved utils to support to match phalcon 5 structure --- src/Bootstrap/Config.php | 21 +++--- src/Cli/Module.php | 2 +- src/Cli/Task.php | 4 +- src/Di/Injectable.php | 1 + src/Di/InjectableTrait.php | 2 +- src/Events/EventsAwareTrait.php | 2 +- src/Exception.php | 2 +- src/Identity.php | 7 +- .../Api/Controllers/AuthController.php | 4 +- .../Api/Controllers/TestController.php | 2 +- src/Modules/Cli/Task.php | 2 +- src/Modules/Cli/Tasks/DataLifeCycleTask.php | 2 +- src/Modules/Cli/Tasks/DatabaseTask.php | 2 +- src/Modules/Cli/Tasks/ScaffoldTask.php | 4 +- src/Modules/Cli/Tasks/TestTask.php | 2 +- src/Modules/Cli/Tasks/Traits/UserTrait.php | 3 +- .../Frontend/Controllers/CheckController.php | 6 +- src/Mvc/Controller.php | 2 +- src/Mvc/Controller/Model.php | 2 +- src/Mvc/Controller/Rest.php | 24 +++---- src/Mvc/Model/Slug.php | 2 +- src/Mvc/Module.php | 2 +- src/Mvc/Router/ModuleRoute.php | 2 +- src/Mvc/View.php | 2 +- src/Provider/Env/ServiceProvider.php | 2 +- src/Provider/Helper/ServiceProvider.php | 11 +-- src/Provider/Oauth2Server/ServiceProvider.php | 10 +-- src/Provider/Utils/ServiceProvider.php | 2 +- src/{Utils => Support}/Env.php | 3 +- src/Support/Helper.php | 71 +++---------------- .../Helper/Arr/FlattenKeys.php} | 20 ++++-- src/Support/Helper/Str/Slugify.php | 25 +++++++ src/Support/HelperFactory.php | 40 +++++++++++ .../RecursiveIterator/Filter/Files.php | 2 +- .../RecursiveIterator/Filter/Folders.php | 2 +- .../RecursiveIterator/Filter/Visible.php | 2 +- src/{Utils => Support}/Slug.php | 2 +- src/{ => Support}/Utils.php | 2 +- 38 files changed, 161 insertions(+), 137 deletions(-) rename src/{Utils => Support}/Env.php (99%) rename src/{Utils/Transform.php => Support/Helper/Arr/FlattenKeys.php} (78%) create mode 100644 src/Support/Helper/Str/Slugify.php create mode 100644 src/Support/HelperFactory.php rename src/{Utils => Support}/RecursiveIterator/Filter/Files.php (92%) rename src/{Utils => Support}/RecursiveIterator/Filter/Folders.php (92%) rename src/{Utils => Support}/RecursiveIterator/Filter/Visible.php (90%) rename src/{Utils => Support}/Slug.php (98%) rename src/{ => Support}/Utils.php (98%) diff --git a/src/Bootstrap/Config.php b/src/Bootstrap/Config.php index 61ae79df..61f6eb7b 100644 --- a/src/Bootstrap/Config.php +++ b/src/Bootstrap/Config.php @@ -12,21 +12,20 @@ namespace Zemit\Bootstrap; use PDO; - +use Phalcon\Config\Config as PhalconConfig; use Phalcon\Db\Column; use Phalcon\Encryption\Security; -use Phalcon\Config\Config as PhalconConfig; use Phalcon\Support\Version as PhalconVersion; +use Zemit\Bootstrap\Permissions\TableConfig; +use Zemit\Bootstrap\Permissions\WorkspaceConfig; use Zemit\Locale; -use Zemit\Support\Version; -use Zemit\Provider; -use Zemit\Utils\Env; use Zemit\Models; -use Zemit\Modules\Cli; use Zemit\Modules\Api; +use Zemit\Modules\Cli; use Zemit\Mvc\Controller\Behavior; -use Zemit\Bootstrap\Permissions\TableConfig; -use Zemit\Bootstrap\Permissions\WorkspaceConfig; +use Zemit\Provider; +use Zemit\Support\Env; +use Zemit\Support\Version; /** * Global Zemit Configuration @@ -457,6 +456,12 @@ public function __construct(array $data = [], bool $insensitive = false) Provider\LoremIpsum\ServiceProvider::class => Env::get('PROVIDER_LOREM_IPSUM', Provider\LoremIpsum\ServiceProvider::class), ], + /** + * Helper Services + */ + 'helpers' => [ + ], + /** * Logger Configuration */ diff --git a/src/Cli/Module.php b/src/Cli/Module.php index 89eb9e19..a2935f85 100644 --- a/src/Cli/Module.php +++ b/src/Cli/Module.php @@ -15,7 +15,7 @@ use Phalcon\Di\DiInterface; use Phalcon\Mvc\ModuleDefinitionInterface; use Zemit\Bootstrap\Config; -use Zemit\Utils; +use Zemit\Support\Utils; class Module implements ModuleDefinitionInterface { diff --git a/src/Cli/Task.php b/src/Cli/Task.php index 4a104e7d..58e6543a 100644 --- a/src/Cli/Task.php +++ b/src/Cli/Task.php @@ -24,7 +24,7 @@ use Zemit\Locale; use Zemit\Mvc\View; use Zemit\Provider\Jwt\Jwt; -use Zemit\Utils; +use Zemit\Support\Utils; /** * @property Bootstrap\Config $config @@ -41,7 +41,7 @@ * @property Identity $identity * @property Locale $locale * @property Logger $logger - * @property Utils $utils + * @property \Zemit\Support\Utils $utils * @property LoremIpsum $loremIpsum * @property OpenAi $openAi */ diff --git a/src/Di/Injectable.php b/src/Di/Injectable.php index 112bec48..ecaaa315 100644 --- a/src/Di/Injectable.php +++ b/src/Di/Injectable.php @@ -46,6 +46,7 @@ // * @property \Zemit\Tag $tag * * @property \Zemit\Acl\Acl $acl + * @property \Zemit\Support\HelperFactory $helper */ class Injectable extends \Phalcon\Di\Injectable implements \Phalcon\Di\InjectionAwareInterface { diff --git a/src/Di/InjectableTrait.php b/src/Di/InjectableTrait.php index 4f9886c5..5333bd6c 100644 --- a/src/Di/InjectableTrait.php +++ b/src/Di/InjectableTrait.php @@ -45,7 +45,7 @@ * @property \Zemit\Debug $debug * @property \Zemit\Identity $identity * @property \Zemit\Locale $locale - * @property \Zemit\Utils $utils + * @property \Zemit\Support\Utils $utils * @property \Zemit\Profiler $profiler * @property \Zemit\Logger $logger * @property \Zemit\Jwt $jwt diff --git a/src/Events/EventsAwareTrait.php b/src/Events/EventsAwareTrait.php index 6a640035..dab580f8 100644 --- a/src/Events/EventsAwareTrait.php +++ b/src/Events/EventsAwareTrait.php @@ -13,7 +13,7 @@ use Phalcon\Di\Di; use Phalcon\Events\ManagerInterface; -use Zemit\Utils\Slug; +use Zemit\Support\Slug; /** * The EventsAwareTrait provides methods for managing events within a class. diff --git a/src/Exception.php b/src/Exception.php index 026878f5..804ccd14 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -13,7 +13,7 @@ /** * Zemit\Exception - * All framework exceptions should use or extend this exception + * All Zemit exceptions should use or extend this exception */ class Exception extends \Exception implements \Throwable { diff --git a/src/Identity.php b/src/Identity.php index b6c6fd8b..ede35592 100644 --- a/src/Identity.php +++ b/src/Identity.php @@ -13,23 +13,24 @@ use Phalcon\Acl\Role; use Phalcon\Db\Column; -use Phalcon\Messages\Message; -use Phalcon\Mvc\ModelInterface; use Phalcon\Encryption\Security\Exception; use Phalcon\Encryption\Security\JWT\Exceptions\ValidatorException; use Phalcon\Filter\Validation\Validator\Confirmation; use Phalcon\Filter\Validation\Validator\Numericality; use Phalcon\Filter\Validation\Validator\PresenceOf; +use Phalcon\Messages\Message; +use Phalcon\Mvc\ModelInterface; use Phalcon\Support\Helper\Str\Random; use Zemit\Di\Injectable; +use Zemit\Filter\Validation; use Zemit\Models\Interfaces\RoleInterface; use Zemit\Models\Interfaces\SessionInterface; use Zemit\Models\Interfaces\UserInterface; use Zemit\Models\User; +use Zemit\Mvc\Model\Behavior\Security as SecurityBehavior; use Zemit\Support\ModelsMap; use Zemit\Support\Options\Options; use Zemit\Support\Options\OptionsInterface; -use Zemit\Mvc\Model\Behavior\Security as SecurityBehavior; /** * Identity Management diff --git a/src/Modules/Api/Controllers/AuthController.php b/src/Modules/Api/Controllers/AuthController.php index c6de0044..eb9e285e 100644 --- a/src/Modules/Api/Controllers/AuthController.php +++ b/src/Modules/Api/Controllers/AuthController.php @@ -10,7 +10,6 @@ namespace Zemit\Modules\Api\Controllers; -use Phalcon\Security\Exception; use Zemit\Mvc\Controller\Identity; class AuthController extends AbstractController @@ -19,7 +18,7 @@ class AuthController extends AbstractController getAction as traitGetAction; } - public function indexAction($id = null): bool + public function indexAction(string|int $id = null): bool { return $this->getAction(); } @@ -30,7 +29,6 @@ public function indexAction($id = null): bool * @param bool $refresh * * @return bool - * @throws Exception */ public function getAction($refresh = false): bool { diff --git a/src/Modules/Api/Controllers/TestController.php b/src/Modules/Api/Controllers/TestController.php index 0c6248bc..e1a40687 100644 --- a/src/Modules/Api/Controllers/TestController.php +++ b/src/Modules/Api/Controllers/TestController.php @@ -11,7 +11,7 @@ namespace Zemit\Modules\Api\Controllers; use Zemit\Modules\Api\Controller; -use Zemit\Utils; +use Zemit\Support\Utils; class TestController extends Controller { diff --git a/src/Modules/Cli/Task.php b/src/Modules/Cli/Task.php index e8592c92..738178c8 100644 --- a/src/Modules/Cli/Task.php +++ b/src/Modules/Cli/Task.php @@ -15,7 +15,7 @@ use Zemit\Exception\CliException; use Zemit\Http\StatusCode; use Zemit\Support\Helper; -use Zemit\Utils; +use Zemit\Support\Utils; class Task extends \Zemit\Cli\Task { diff --git a/src/Modules/Cli/Tasks/DataLifeCycleTask.php b/src/Modules/Cli/Tasks/DataLifeCycleTask.php index 6cf234a2..e1843e72 100644 --- a/src/Modules/Cli/Tasks/DataLifeCycleTask.php +++ b/src/Modules/Cli/Tasks/DataLifeCycleTask.php @@ -14,7 +14,7 @@ use Phalcon\Mvc\Model\ResultsetInterface; use Zemit\Modules\Cli\Task; use Zemit\Mvc\Model; -use Zemit\Utils; +use Zemit\Support\Utils; class DataLifeCycleTask extends Task { diff --git a/src/Modules/Cli/Tasks/DatabaseTask.php b/src/Modules/Cli/Tasks/DatabaseTask.php index 22fcd971..af259c52 100644 --- a/src/Modules/Cli/Tasks/DatabaseTask.php +++ b/src/Modules/Cli/Tasks/DatabaseTask.php @@ -14,7 +14,7 @@ use Zemit\Bootstrap\Deployment; use Zemit\Modules\Cli\Task; use Zemit\Modules\Cli\Tasks\Traits\DatabaseTrait; -use Zemit\Utils; +use Zemit\Support\Utils; class DatabaseTask extends Task { diff --git a/src/Modules/Cli/Tasks/ScaffoldTask.php b/src/Modules/Cli/Tasks/ScaffoldTask.php index 75d69837..2c40715c 100644 --- a/src/Modules/Cli/Tasks/ScaffoldTask.php +++ b/src/Modules/Cli/Tasks/ScaffoldTask.php @@ -14,10 +14,10 @@ use Phalcon\Db\Column; use Phalcon\Db\ColumnInterface; use Phalcon\Mvc\Model\Relation; +use Zemit\Modules\Cli\Task; use Zemit\Mvc\Model; use Zemit\Support\Helper; -use Zemit\Modules\Cli\Task; -use Zemit\Utils\Slug; +use Zemit\Support\Slug; class ScaffoldTask extends Task { diff --git a/src/Modules/Cli/Tasks/TestTask.php b/src/Modules/Cli/Tasks/TestTask.php index 11a77f38..9d5d7adb 100644 --- a/src/Modules/Cli/Tasks/TestTask.php +++ b/src/Modules/Cli/Tasks/TestTask.php @@ -12,7 +12,7 @@ namespace Zemit\Modules\Cli\Tasks; use Zemit\Modules\Cli\Task; -use Zemit\Utils; +use Zemit\Support\Utils; class TestTask extends Task { diff --git a/src/Modules/Cli/Tasks/Traits/UserTrait.php b/src/Modules/Cli/Tasks/Traits/UserTrait.php index c640fd3a..d7c570b6 100644 --- a/src/Modules/Cli/Tasks/Traits/UserTrait.php +++ b/src/Modules/Cli/Tasks/Traits/UserTrait.php @@ -12,11 +12,10 @@ namespace Zemit\Modules\Cli\Tasks\Traits; use Phalcon\Db\Column; -use Zemit\Models\Interfaces\UserInterface; use Zemit\Models\Role; use Zemit\Models\User; use Zemit\Models\UserRole; -use Zemit\Utils; +use Zemit\Support\Utils; trait UserTrait { diff --git a/src/Modules/Frontend/Controllers/CheckController.php b/src/Modules/Frontend/Controllers/CheckController.php index 80ffd86e..81c7bb90 100644 --- a/src/Modules/Frontend/Controllers/CheckController.php +++ b/src/Modules/Frontend/Controllers/CheckController.php @@ -12,13 +12,13 @@ namespace Zemit\Modules\Frontend\Controllers; use Phalcon\Db\Adapter\Pdo\Mysql; +use Phalcon\Filter; +use Phalcon\Filter\Validation; use Phalcon\Http\Response; use Phalcon\Messages\Message; -use Phalcon\Filter\Validation; use Zemit\Bootstrap; use Zemit\Bootstrap\Config; use Zemit\Escaper; -use Phalcon\Filter; use Zemit\Http\Request; use Zemit\Identity; use Zemit\Locale; @@ -26,7 +26,7 @@ use Zemit\Mvc\Dispatcher; use Zemit\Mvc\Router; use Zemit\Mvc\Url; -use Zemit\Utils; +use Zemit\Support\Utils; class CheckController extends AbstractController { diff --git a/src/Mvc/Controller.php b/src/Mvc/Controller.php index aabb7fe2..c2311f07 100644 --- a/src/Mvc/Controller.php +++ b/src/Mvc/Controller.php @@ -19,8 +19,8 @@ use Zemit\Filter; use Zemit\Http\Request; use Zemit\Identity; +use Zemit\Support\Utils; use Zemit\Tag; -use Zemit\Utils; /** * Class Controller diff --git a/src/Mvc/Controller/Model.php b/src/Mvc/Controller/Model.php index ae628ca6..d52a1ca9 100644 --- a/src/Mvc/Controller/Model.php +++ b/src/Mvc/Controller/Model.php @@ -20,7 +20,7 @@ use Zemit\Identity; use Zemit\Support\Exposer\Exposer; use Zemit\Support\Helper; -use Zemit\Utils\Slug; +use Zemit\Support\Slug; /** * Trait Model diff --git a/src/Mvc/Controller/Rest.php b/src/Mvc/Controller/Rest.php index 83bd40df..a7fe4265 100644 --- a/src/Mvc/Controller/Rest.php +++ b/src/Mvc/Controller/Rest.php @@ -25,8 +25,8 @@ use Shuchkin\SimpleXLSXGen; use Zemit\Di\Injectable; use Zemit\Http\StatusCode; -use Zemit\Utils; -use Zemit\Utils\Slug; +use Zemit\Support\Slug; +use Zemit\Support\Utils; class Rest extends \Zemit\Mvc\Controller { @@ -36,7 +36,7 @@ class Rest extends \Zemit\Mvc\Controller /** * @throws Exception */ - public function indexAction(string|int $id = null): void + public function indexAction(string|int $id = null) { $this->restForwarding($id); } @@ -74,7 +74,7 @@ public function getSingleAction(string|int $id = null): false|ResponseInterface /** * Retrieving a single record */ - public function getAction(string|int $id = null): false|ResponseInterface + public function getAction(string|int $id = null): bool|ResponseInterface { $modelName = $this->getModelClassName(); $single = $this->getSingle($id, $modelName, null); @@ -692,14 +692,14 @@ public function reorderAction(string|int $id = null, int $position = null): fals } /** - * Sending an error as an http response + * Set the REST response error * - * @param null $error - * @param null $response - * - * @return ResponseInterface + * @param int $code The HTTP status code (default: 400) + * @param string $status The status message (default: 'Bad Request') + * @param mixed $response The response body (default: null) + * @return ResponseInterface The REST response object */ - public function setRestErrorResponse($code = 400, $status = 'Bad Request', $response = null) + public function setRestErrorResponse(int $code = 400, string $status = 'Bad Request', mixed $response = null): ResponseInterface { return $this->setRestResponse($response, $code, $status); } @@ -715,7 +715,7 @@ public function setRestErrorResponse($code = 400, $status = 'Bad Request', $resp * * @return ResponseInterface */ - public function setRestResponse($response = null, int $code = null, string $status = null, int $jsonOptions = 0, int $depth = 512): ResponseInterface + public function setRestResponse(mixed $response = null, int $code = null, string $status = null, int $jsonOptions = 0, int $depth = 512): ResponseInterface { $debug = $this->isDebugEnabled(); @@ -726,7 +726,7 @@ public function setRestResponse($response = null, int $code = null, string $stat $status ??= $reasonPhrase ?: StatusCode::getMessage($code); $view = $this->view->getParamsToView(); - $hash = hash('sha512', json_encode($view)); + $hash = hash('sha512', json_encode($view)); // @todo store hash in cache layer with response content // set response status code $this->response->setStatusCode($code, $code . ' ' . $status); diff --git a/src/Mvc/Model/Slug.php b/src/Mvc/Model/Slug.php index 5fb4f790..a5e4eee5 100644 --- a/src/Mvc/Model/Slug.php +++ b/src/Mvc/Model/Slug.php @@ -35,7 +35,7 @@ public function initializeSlug(?array $options = null): void 'beforeValidation' => [ $field => function (Model $model, $field) { $value = $model->readAttribute($field); - return $value && is_string($value) ? \Zemit\Utils\Slug::generate($value) : $value; + return $value && is_string($value) ? \Zemit\Support\Slug::generate($value) : $value; }, ], ])); diff --git a/src/Mvc/Module.php b/src/Mvc/Module.php index d80ced66..3ffea7e2 100644 --- a/src/Mvc/Module.php +++ b/src/Mvc/Module.php @@ -17,7 +17,7 @@ use Phalcon\Mvc\View; use Zemit\Bootstrap\Config; use Zemit\Di\Injectable; -use Zemit\Utils; +use Zemit\Support\Utils; /** * {@inheritDoc} diff --git a/src/Mvc/Router/ModuleRoute.php b/src/Mvc/Router/ModuleRoute.php index 9300f497..476576c6 100644 --- a/src/Mvc/Router/ModuleRoute.php +++ b/src/Mvc/Router/ModuleRoute.php @@ -12,7 +12,7 @@ namespace Zemit\Mvc\Router; use Phalcon\Mvc\Router\Group as RouterGroup; -use Zemit\Utils\Slug; +use Zemit\Support\Slug; class ModuleRoute extends RouterGroup { diff --git a/src/Mvc/View.php b/src/Mvc/View.php index 0f5af402..2534bfdc 100644 --- a/src/Mvc/View.php +++ b/src/Mvc/View.php @@ -12,7 +12,7 @@ namespace Zemit\Mvc; use Zemit\Support\Helper; -use Zemit\Utils\Slug; +use Zemit\Support\Slug; /** * {@inheritdoc} diff --git a/src/Provider/Env/ServiceProvider.php b/src/Provider/Env/ServiceProvider.php index cc6e52ba..de37d1f2 100644 --- a/src/Provider/Env/ServiceProvider.php +++ b/src/Provider/Env/ServiceProvider.php @@ -13,7 +13,7 @@ use Phalcon\Di\DiInterface; use Zemit\Provider\AbstractServiceProvider; -use Zemit\Utils\Env; +use Zemit\Support\Env; class ServiceProvider extends AbstractServiceProvider { diff --git a/src/Provider/Helper/ServiceProvider.php b/src/Provider/Helper/ServiceProvider.php index 4be84f32..6de65580 100644 --- a/src/Provider/Helper/ServiceProvider.php +++ b/src/Provider/Helper/ServiceProvider.php @@ -12,7 +12,8 @@ namespace Zemit\Provider\Helper; use Phalcon\Di\DiInterface; -use Phalcon\Support\HelperFactory; +use Zemit\Support\HelperFactory; +use Zemit\Config\ConfigInterface; use Zemit\Provider\AbstractServiceProvider; class ServiceProvider extends AbstractServiceProvider @@ -23,11 +24,11 @@ public function register(DiInterface $di): void { $di->setShared($this->getName(), function () use ($di) { - $helper = new HelperFactory(); + $config = $di->get('config'); + assert($config instanceof ConfigInterface); + $helperServices = $config->pathToArray('helpers', []); - // @todo fetch config to allow to push classes into the helper factory - - return $helper; + return new HelperFactory($helperServices); }); } } diff --git a/src/Provider/Oauth2Server/ServiceProvider.php b/src/Provider/Oauth2Server/ServiceProvider.php index cad8af1d..9968e31c 100644 --- a/src/Provider/Oauth2Server/ServiceProvider.php +++ b/src/Provider/Oauth2Server/ServiceProvider.php @@ -11,16 +11,16 @@ namespace Zemit\Provider\Oauth2Server; -use Phalcon\Di\DiInterface; -use Zemit\Provider\AbstractServiceProvider; -use Zemit\Utils\Env; -use League\OAuth2\Server\CryptKey; use League\OAuth2\Server\AuthorizationServer; +use League\OAuth2\Server\CryptKey; use League\OAuth2\Server\Grant\AuthCodeGrant; +use League\OAuth2\Server\Grant\ClientCredentialsGrant; use League\OAuth2\Server\Grant\ImplicitGrant; use League\OAuth2\Server\Grant\PasswordGrant; use League\OAuth2\Server\Grant\RefreshTokenGrant; -use League\OAuth2\Server\Grant\ClientCredentialsGrant; +use Phalcon\Di\DiInterface; +use Zemit\Provider\AbstractServiceProvider; +use Zemit\Support\Env; /** * @todo diff --git a/src/Provider/Utils/ServiceProvider.php b/src/Provider/Utils/ServiceProvider.php index 4ab2ebe0..3146319f 100644 --- a/src/Provider/Utils/ServiceProvider.php +++ b/src/Provider/Utils/ServiceProvider.php @@ -12,8 +12,8 @@ namespace Zemit\Provider\Utils; use Phalcon\Di\DiInterface; -use Zemit\Utils; use Zemit\Provider\AbstractServiceProvider; +use Zemit\Support\Utils; class ServiceProvider extends AbstractServiceProvider { diff --git a/src/Utils/Env.php b/src/Support/Env.php similarity index 99% rename from src/Utils/Env.php rename to src/Support/Env.php index 5843d1ed..d5feaf54 100644 --- a/src/Utils/Env.php +++ b/src/Support/Env.php @@ -9,11 +9,10 @@ * file that was distributed with this source code. */ -namespace Zemit\Utils; +namespace Zemit\Support; use Dotenv\Dotenv; use Phalcon\Di\DiInterface; -use Zemit\Support\Helper; /** * Allow to access environment variable easily diff --git a/src/Support/Helper.php b/src/Support/Helper.php index eb01b5fc..6966139a 100644 --- a/src/Support/Helper.php +++ b/src/Support/Helper.php @@ -11,80 +11,27 @@ namespace Zemit\Support; -use Phalcon\Di\DiInterface; -use Phalcon\Support\HelperFactory; - /** - * @method static string basename(string $uri, string $suffix = null) - * @method static array blacklist(array $collection, array $blackList) - * @method static string camelize(string $text, string $delimiters = null, bool $lowerFirst = false) - * @method static array chunk(array $collection, int $size, bool $preserveKeys = false) - * @method static string concat(string $delimiter, string $first, string $second, string ...$arguments) - * @method static int countVowels(string $text) - * @method static string decapitalize(string $text, bool $upperRest = false, string $encoding = 'UTF-8') - * @method static string decode(string $data, bool $associative = false, int $depth = 512, int $options = 0) - * @method static string decrement(string $text, string $separator = '_') - * @method static string dirFromFile(string $file) - * @method static string dirSeparator(string $directory) - * @method static string encode($data, int $options = 0, int $depth = 512) - * @method static bool endsWith(string $haystack, string $needle, bool $ignoreCase = true) - * @method static mixed first(array $collection, callable $method = null) - * @method static string firstBetween(string $text, string $start, string $end) - * @method static mixed firstKey(array $collection, callable $method = null) - * @method static string friendly(string $text, string $separator = '-', bool $lowercase = true, $replace = null) - * @method static array flatten(array $collection, bool $deep = false) - * @method static mixed get(array $collection, $index, $defaultValue = null, string $cast = null) - * @method static array group(array $collection, $method) - * @method static bool has(array $collection, $index) - * @method static string humanize(string $text) - * @method static bool includes(string $haystack, string $needle) - * @method static string increment(string $text, string $separator = '_') - * @method static bool isAnagram(string $first, string $second) - * @method static bool isBetween(int $value, int $start, int $end) - * @method static bool isLower(string $text, string $encoding = 'UTF-8') - * @method static bool isPalindrome(string $text) - * @method static bool isUnique(array $collection) - * @method static bool isUpper(string $text, string $encoding = 'UTF-8') - * @method static string kebabCase(string $text, string $delimiters = null) - * @method static mixed last(array $collection, callable $method = null) - * @method static mixed lastKey(array $collection, callable $method = null) - * @method static int len(string $text, string $encoding = 'UTF-8') - * @method static string lower(string $text, string $encoding = 'UTF-8') - * @method static array order(array $collection, $attribute, string $order = 'asc') - * @method static string pascalCase(string $text, string $delimiters = null) - * @method static array pluck(array $collection, string $element) - * @method static string prefix($text, string $prefix) - * @method static string random(int $type = 0, int $length = 8) - * @method static string reduceSlashes(string $text) - * @method static array set(array $collection, $value, $index = null) - * @method static array sliceLeft(array $collection, int $elements = 1) - * @method static array sliceRight(array $collection, int $elements = 1) - * @method static string snakeCase(string $text, string $delimiters = null) - * @method static array split(array $collection) - * @method static bool startsWith(string $haystack, string $needle, bool $ignoreCase = true) - * @method static string suffix($text, string $suffix) - * @method static object toObject(array $collection) - * @method static bool validateAll(array $collection, callable $method) - * @method static bool validateAny(array $collection, callable $method) - * @method static string ucwords(string $text, string $encoding = 'UTF-8') - * @method static string uncamelize(string $text, string $delimiters = '_') - * @method static string underscore(string $text) - * @method static string upper(string $text, string $encoding = 'UTF-8') - * @method static array whitelist(array $collection, array $whiteList) + * Class Helper + * + * This class is responsible for providing helper methods and functions. */ class Helper extends \Phalcon\Di\Injectable { - // @todo review this fix for unit testing public function getHelper(): HelperFactory { if ($this->container) { - return $this->getDI()->get('helper'); + $helper = $this->getDI()->get('helper'); + + if ($helper) { + return $helper; + } } return new HelperFactory(); } - public static function __callStatic($name, $arguments) + public static function __callStatic(string $name, array $arguments): mixed { return (new self())->getHelper()->$name(...$arguments); } diff --git a/src/Utils/Transform.php b/src/Support/Helper/Arr/FlattenKeys.php similarity index 78% rename from src/Utils/Transform.php rename to src/Support/Helper/Arr/FlattenKeys.php index f8ab9ee7..433b16af 100644 --- a/src/Utils/Transform.php +++ b/src/Support/Helper/Arr/FlattenKeys.php @@ -9,25 +9,33 @@ * file that was distributed with this source code. */ -namespace Zemit\Utils; +namespace Zemit\Support\Helper\Arr; -class Transform +/** + * + */ +class FlattenKeys { + public function __invoke(array $collection = [], string $delimiter = '.', bool $lowerKey = true): array + { + return self::process($collection, $delimiter, $lowerKey); + } + /** * Here to parse the columns parameter into some kind of flatten array with * the key path separated by dot "my.path" and the value true, false or a callback function * including the ExposeBuilder object */ - public static function flattenKeys(?array $array = [], ?string $delimiter = '.', ?bool $lowerKey = true, ?string $context = null): ?array + public static function process(array $collection = [], string $delimiter = '.', bool $lowerKey = true, string $context = null): ?array { // nothing passed - if (!isset($array)) { + if (!isset($collection)) { return null; } $ret = []; - foreach ($array as $key => $value) { + foreach ($collection as $key => $value) { // handle our special attribute if (is_bool($key)) { @@ -63,7 +71,7 @@ public static function flattenKeys(?array $array = [], ?string $delimiter = '.', } if (is_array($value)) { - $subRet = self::flattenKeys($value, $delimiter, $lowerKey, $currentKey); + $subRet = self::process($value, $delimiter, $lowerKey, $currentKey); if (is_array($subRet)) { $ret = array_merge_recursive($ret, $subRet); } diff --git a/src/Support/Helper/Str/Slugify.php b/src/Support/Helper/Str/Slugify.php new file mode 100644 index 00000000..37999019 --- /dev/null +++ b/src/Support/Helper/Str/Slugify.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Zemit\Support\Helper\Str; + +use Zemit\Support\Slug; + +/** + * Creates a slug to be used for pretty URLs + */ +class Slugify +{ + public function __invoke(string $string, array $replace = [], string $delimiter = '-'): string + { + return Slug::generate($string, $replace, $delimiter); + } +} diff --git a/src/Support/HelperFactory.php b/src/Support/HelperFactory.php new file mode 100644 index 00000000..48c8476f --- /dev/null +++ b/src/Support/HelperFactory.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Zemit\Support; + +use Zemit\Support\Helper\Arr\FlattenKeys; +use Zemit\Support\Helper\Str\Slugify; + +/** + * HelperFactory Class + * + * This class extends the Phalcon\Support\HelperFactory class and provides additional helper services. + * {@inheritdoc} + * + * @method string flattenKeys(array $collection = [], string $delimiter = '.', bool $lowerKey = true) + * @method string slugify(string $string, array $replace = [], string $delimiter = '-') + */ +class HelperFactory extends \Phalcon\Support\HelperFactory +{ + /** + * Returns the available adapters + * + * @return string[] + */ + protected function getServices(): array + { + return array_merge(parent::getServices(), [ + 'flattenKeys' => FlattenKeys::class, + 'slugify' => Slugify::class, + ]); + } +} diff --git a/src/Utils/RecursiveIterator/Filter/Files.php b/src/Support/RecursiveIterator/Filter/Files.php similarity index 92% rename from src/Utils/RecursiveIterator/Filter/Files.php rename to src/Support/RecursiveIterator/Filter/Files.php index 69458cbf..ad62fb69 100644 --- a/src/Utils/RecursiveIterator/Filter/Files.php +++ b/src/Support/RecursiveIterator/Filter/Files.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Zemit\Utils\RecursiveIterator\Filter; +namespace Zemit\Support\RecursiveIterator\Filter; class Files extends \RecursiveFilterIterator { diff --git a/src/Utils/RecursiveIterator/Filter/Folders.php b/src/Support/RecursiveIterator/Filter/Folders.php similarity index 92% rename from src/Utils/RecursiveIterator/Filter/Folders.php rename to src/Support/RecursiveIterator/Filter/Folders.php index b9f554cc..a9b8e897 100644 --- a/src/Utils/RecursiveIterator/Filter/Folders.php +++ b/src/Support/RecursiveIterator/Filter/Folders.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Zemit\Utils\RecursiveIterator\Filter; +namespace Zemit\Support\RecursiveIterator\Filter; class Folders extends \RecursiveFilterIterator { diff --git a/src/Utils/RecursiveIterator/Filter/Visible.php b/src/Support/RecursiveIterator/Filter/Visible.php similarity index 90% rename from src/Utils/RecursiveIterator/Filter/Visible.php rename to src/Support/RecursiveIterator/Filter/Visible.php index 7f9f8a82..b09e7640 100644 --- a/src/Utils/RecursiveIterator/Filter/Visible.php +++ b/src/Support/RecursiveIterator/Filter/Visible.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Zemit\Utils\RecursiveIterator\Filter; +namespace Zemit\Support\RecursiveIterator\Filter; class Visible extends \RecursiveFilterIterator { diff --git a/src/Utils/Slug.php b/src/Support/Slug.php similarity index 98% rename from src/Utils/Slug.php rename to src/Support/Slug.php index 78516423..3d49b977 100644 --- a/src/Utils/Slug.php +++ b/src/Support/Slug.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Zemit\Utils; +namespace Zemit\Support; use Transliterator; diff --git a/src/Utils.php b/src/Support/Utils.php similarity index 98% rename from src/Utils.php rename to src/Support/Utils.php index 8b7fe5b7..c32e7590 100644 --- a/src/Utils.php +++ b/src/Support/Utils.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Zemit; +namespace Zemit\Support; class Utils {