Skip to content

Commit

Permalink
updating uploads helper
Browse files Browse the repository at this point in the history
  • Loading branch information
iruzevic committed Jan 5, 2024
1 parent 626baa0 commit 3aaa9fe
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 121 deletions.
9 changes: 4 additions & 5 deletions src/Helpers/ApiHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
/**
* Class that holds all api helpers used in classes.
*
* @package EightshiftLibs\Helpers
* @package EightshiftFormsUtils\Helpers
*/

declare(strict_types=1);

namespace EightshiftForms\Helpers;
namespace EightshiftFormsUtils\Helpers;

use EightshiftFormsUtils\Config\UtilsConfig;
use EightshiftFormsUtilsVendor\EightshiftLibs\Helpers\Components;

/**
* ApiHelper class.
Expand Down Expand Up @@ -61,7 +60,7 @@ public static function getIntegrationApiReponseDetails(
$code = $response['response']['code'] ?? 200;
$body = $response['body'] ?? '';

if (Components::isJson($body)) {
if (Helper::isJson($body)) {
$body = \json_decode($body, true) ?? [];
}
}
Expand All @@ -75,7 +74,7 @@ public static function getIntegrationApiReponseDetails(
}

return [
'integration' => Components::kebabToCamelCase($integration, '-'),
'integration' => Helper::kebabToCamelCase($integration, '-'),
'params' => $params,
'files' => $files,
'response' => $response['response'] ?? [],
Expand Down
160 changes: 57 additions & 103 deletions src/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
/**
* Class that holds all generic helpers.
*
* @package EightshiftForms\Helpers
* @package EightshiftFormsUtils\Helpers
*/

declare(strict_types=1);

namespace EightshiftForms\Helpers;
namespace EightshiftFormsUtils\Helpers;

use EightshiftFormsUtils\Config\UtilsConfig;
use EightshiftFormsUtilsVendor\EightshiftLibs\Helpers\Components;
Expand Down Expand Up @@ -244,7 +244,7 @@ public static function getBlockNameDetails(string $blockName): array
return [
'namespace' => $block[0] ?? '',
'name' => $blockName,
'nameAttr' => Components::kebabToCamelCase($blockName),
'nameAttr' => Helper::kebabToCamelCase($blockName),
];
}

Expand Down Expand Up @@ -435,7 +435,7 @@ public static function getFormDetailsById(string $formId): array
foreach ($output['fieldsOnly'] as $item) {
$blockItemName = self::getBlockNameDetails($item['blockName'])['nameAttr'];

$value = $item['attrs'][Components::kebabToCamelCase("{$blockItemName}-{$blockItemName}-Name")] ?? '';
$value = $item['attrs'][Helper::kebabToCamelCase("{$blockItemName}-{$blockItemName}-Name")] ?? '';

if (!$value) {
continue;
Expand Down Expand Up @@ -486,8 +486,8 @@ public static function getFormDetailsById(string $formId): array
$name = $blockName['name'];

if ($name === 'step') {
$stepCurrent = $block['attrs'][Components::kebabToCamelCase("{$name}-{$name}Name")] ?? '';
$stepLabel = $block['attrs'][Components::kebabToCamelCase("{$name}-{$name}Label")] ?? '';
$stepCurrent = $block['attrs'][Helper::kebabToCamelCase("{$name}-{$name}Name")] ?? '';
$stepLabel = $block['attrs'][Helper::kebabToCamelCase("{$name}-{$name}Label")] ?? '';

if (!$stepLabel) {
$stepLabel = $stepCurrent;
Expand All @@ -504,7 +504,7 @@ public static function getFormDetailsById(string $formId): array
continue;
}

$itemName = $block['attrs'][Components::kebabToCamelCase("{$name}-{$name}Name")] ?? '';
$itemName = $block['attrs'][Helper::kebabToCamelCase("{$name}-{$name}Name")] ?? '';
if (!$itemName) {
continue;
}
Expand Down Expand Up @@ -738,31 +738,6 @@ public static function getBlockAdditionalContentViaFilter(string $name, array $a
return '';
}

/**
* Return main manifest.json file.
*
* @return array<mixed>
*/
public static function getUtilsManifest(): array
{
$sep = \DIRECTORY_SEPARATOR;
$filePath = dirname(__FILE__, 2) . "{$sep}manifest.json";

return \json_decode(\implode(' ', (array)\file($filePath)), true);
}

/**
* Return utils icons from manifest.json.
*
* @param string $type Type to return.
*
* @return string
*/
public static function getUtilsIcons(string $type): string
{
return self::getUtilsManifest()['icons'][Components::kebabToCamelCase($type)] ?? '';
}

/**
* Find array value by key in recursive array.
*
Expand Down Expand Up @@ -866,7 +841,7 @@ public static function getCurrentUrl(): string
*/
public static function removeUneceseryParamFields(array $params, array $additional = []): array
{
$customFields = \array_flip(Components::flattenArray(self::getStateParams()));
$customFields = \array_flip(self::flattenArray(UtilsHelper::getStateParams()));
$additional = \array_flip($additional);

return \array_filter(
Expand Down Expand Up @@ -976,76 +951,6 @@ function ($item) use ($isDeveloperModeActive) {
);
}

/**
* Return selector admin enum values by name.
*
* @param string $name Name of the enum.
*
* @return string
*/
public static function getStateSelectorAdmin(string $name): string
{
return Components::getSettings()['enums']['selectorsAdmin'][$name] ?? '';
}

/**
* Return selector enum values by name.
*
* @param string $name Name of the enum.
*
* @return string
*/
public static function getStateSelector(string $name): string
{
return Components::getSettings()['enums']['selectors'][$name] ?? '';
}

/**
* Return attribute enum values by name.
*
* @param string $name Name of the enum.
*
* @return string
*/
public static function getStateAttribute(string $name): string
{
return Components::getSettings()['enums']['attrs'][$name] ?? '';
}

/**
* Return all params enum values.
*
* @return array<string>
*/
public static function getStateParams(): array
{
return Components::getSettings()['enums']['params'] ?? [];
}

/**
* Return param enum values by name.
*
* @param string $name Name of the enum.
*
* @return string
*/
public static function getStateParam(string $name): string
{
return self::getStateParams()[$name] ?? '';
}

/**
* Return field type internal enum values by name.
*
* @param string $name Name of the enum.
*
* @return string
*/
public static function getStateFieldType(string $name): string
{
return Components::getSettings()['enums']['typeInternal'][$name] ?? '';
}

/**
* Get the settings labels and details by type and key.
* This method is used to provide the ability to translate all strings.
Expand Down Expand Up @@ -1136,6 +1041,55 @@ public static function isDeveloperForceDisabledFieldsActive(): bool
return \apply_filters(UtilsConfig::FILTER_SETTINGS_IS_DEBUG_ACTIVE, UtilsConfig::SETTINGS_DEBUG_FORCE_DISABLED_FIELDS) ?? false;
}

/**
* Convert string from kebab to camel case.
*
* @param string $stringToConvert String to convert.
* @param string $separator Separator to use for conversion.
*
* @return string
*/
public static function kebabToCamelCase(string $stringToConvert, string $separator = '-'): string
{
return \lcfirst(\str_replace($separator, '', \ucwords($stringToConvert, $separator)));
}

/**
* Check if json is valid
*
* @param string $jsonString String to check.
*
* @return bool
*/
public static function isJson(string $jsonString): bool
{
\json_decode($jsonString);
return (\json_last_error() === \JSON_ERROR_NONE);
}

/**
* Flatten multidimensional array.
*
* @param array<mixed> $arrayToFlatten Multidimensional array to flatten.
*
* @return array<mixed>
*/
public static function flattenArray(array $arrayToFlatten): array
{
$output = [];

\array_walk_recursive(
$arrayToFlatten,
function ($a) use (&$output) {
if (!empty($a)) {
$output[] = $a;
}
}
);

return $output;
}

/**
* Get list of all full filter names build from array.
*
Expand Down
5 changes: 2 additions & 3 deletions src/Helpers/I18nHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
/**
* Class that holds all i18n helpers.
*
* @package EightshiftLibs\Helpers
* @package EightshiftFormsUtils\Helpers
*/

declare(strict_types=1);

namespace EightshiftForms\Helpers;
namespace EightshiftFormsUtils\Helpers;

use EightshiftForms\Misc\SettingsWpml;
use EightshiftFormsUtils\Config\UtilsConfig;

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Helpers/IntegrationsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
/**
* Class that holds all Integration helpers.
*
* @package EightshiftLibs\Helpers
* @package EightshiftFormsUtils\Helpers
*/

declare(strict_types=1);

namespace EightshiftForms\Helpers;
namespace EightshiftFormsUtils\Helpers;

use EightshiftForms\Helpers\SettingsHelper;
use EightshiftFormsUtils\Helpers\SettingsHelper;
use EightshiftFormsUtils\Config\UtilsConfig;

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Helpers/SettingsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
/**
* Class that holds all helpers for settings.
*
* @package EightshiftLibs\Helpers
* @package EightshiftFormsUtils\Helpers
*/

declare(strict_types=1);

namespace EightshiftForms\Helpers;
namespace EightshiftFormsUtils\Helpers;

use EightshiftForms\Helpers\I18nHelper;
use EightshiftFormsUtils\Helpers\I18nHelper;
use EightshiftFormsUtils\Config\UtilsConfig;

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Helpers/SettingsOutputHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
/**
* Class that holds all Settings output helpers.
*
* @package EightshiftLibs\Helpers
* @package EightshiftFormsUtils\Helpers
*/

declare(strict_types=1);

namespace EightshiftForms\Helpers;
namespace EightshiftFormsUtils\Helpers;

use EightshiftFormsUtils\Config\UtilsConfig;

Expand Down Expand Up @@ -216,9 +216,9 @@ public static function getTestAliConnection(string $key): array
'submitValue' => \__('Test API connection', 'eightshift-forms'),
'submitVariant' => 'outline',
'submitAttrs' => [
Helper::getStateAttribute('testApiType') => $key,
UtilsHelper::getStateAttribute('testApiType') => $key,
],
'additionalClass' => Helper::getStateSelectorAdmin('testApi') . ' es-submit--api-test',
'additionalClass' => UtilsHelper::getStateSelectorAdmin('testApi') . ' es-submit--api-test',
];
}

Expand Down
Loading

0 comments on commit 3aaa9fe

Please sign in to comment.