Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-10349: Restructuring code #1623

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\TypedData\ListInterface;
use Drupal\Core\TypedData\Plugin\DataType\Map;
use Drupal\grants_handler\Plugin\WebformHandler\GrantsHandler;
use Drupal\grants_metadata\AtvSchema;
use Drupal\grants_metadata\ConvertHelper;
use Drupal\grants_metadata\InputmaskHandler;

/**
Expand Down Expand Up @@ -54,7 +54,7 @@ public static function processBudgetStaticValues(Map $property): array {
$value = '';
}
else {
$value = (string) GrantsHandler::convertToFloat($value);
$value = (string) ConvertHelper::convertToFloat($value);
}

$items[] = [
Expand Down Expand Up @@ -97,7 +97,7 @@ public static function processBudgetOtherValues(ListInterface $property): array
$valueToSave = NULL;
}
else {
$valueToSave = (string) GrantsHandler::convertToFloat($value);
$valueToSave = (string) ConvertHelper::convertToFloat($value);
}

$itemValues = [
Expand Down Expand Up @@ -143,7 +143,7 @@ public static function getBudgetOtherValues(array $documentData, array $jsonPath
$groupName = $parent['costGroupName'] ?? $parent['incomeGroupName'];
if (!empty($parent) && isset($parent[$pathLast])) {
$retVal[$groupName] = array_map(function ($e) {
$value = GrantsHandler::convertToFloat($e['value']);
$value = ConvertHelper::convertToFloat($e['value']);

if ($value !== NULL) {
$value = number_format(
Expand Down Expand Up @@ -219,7 +219,7 @@ private static function getPrintValue($value) {
return $value;
}

$floatValue = (float) GrantsHandler::convertToFloat($value);
$floatValue = (float) ConvertHelper::convertToFloat($value);
return number_format(
$floatValue,
2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Drupal\grants_handler\WebformSubmissionNotesHelper;
use Drupal\grants_mandate\CompanySelectException;
use Drupal\grants_metadata\ApplicationDataService;
use Drupal\grants_metadata\ConvertHelper;
use Drupal\grants_profile\GrantsProfileService;
use Drupal\helfi_atv\AtvDocumentNotFoundException;
use Drupal\helfi_atv\AtvFailedToConnectException;
Expand Down Expand Up @@ -257,51 +258,6 @@ public static function create(
);
}

/**
* Convert EUR format value to float.
*
* @param string|null $value
* Value to be converted.
*
* @return float|null
* Floated value.
*/
public static function convertToFloat(?string $value = ''): ?float {
if (is_null($value)) {
return NULL;
}

if ($value === '') {
return NULL;
}

$value = str_replace(['€', ',', ' '], ['', '.', ''], $value);
return (float) $value;
}

/**
* Convert EUR format value to "int" .
*
* @param string|null $value
* Value to be converted.
*
* @return int|null
* Int value.
*/
public static function convertToInt(?string $value = ''): ?int {
if (is_null($value)) {
return NULL;
}

if ($value === '') {
return NULL;
}

$value = str_replace(['€', ',', ' ', '_'], ['', '.', '', ''], $value);
$value = (int) $value;
return $value;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -343,7 +299,7 @@ protected function setTotals(): void {
is_array($this->submittedFormData['myonnetty_avustus'])) {
$tempTotal = 0;
foreach ($this->submittedFormData['myonnetty_avustus'] as $item) {
$amount = self::convertToFloat($item['amount']);
$amount = ConvertHelper::convertToFloat($item['amount']);
$tempTotal += $amount;
}
$this->submittedFormData['myonnetty_avustus_total'] = $tempTotal;
Expand All @@ -353,7 +309,7 @@ protected function setTotals(): void {
is_array($this->submittedFormData['haettu_avustus_tieto'])) {
$tempTotal = 0;
foreach ($this->submittedFormData['haettu_avustus_tieto'] as $item) {
$amount = self::convertToFloat($item['amount']);
$amount = ConvertHelper::convertToFloat($item['amount']);
$tempTotal += $amount;
}
$this->submittedFormData['haettu_avustus_tieto_total'] = $tempTotal;
Expand Down Expand Up @@ -679,7 +635,7 @@ public function alterForm(array &$form, FormStateInterface $form_state, WebformS
$subventionsTotalAmount = 0;
if (isset($submissionData["subventions"]) && is_array($submissionData["subventions"])) {
foreach ($submissionData["subventions"] as $sub) {
$subventionsTotalAmount += self::convertToFloat($sub['amount']);
$subventionsTotalAmount += ConvertHelper::convertToFloat($sub['amount']);
}
}

Expand Down Expand Up @@ -711,7 +667,7 @@ public function alterForm(array &$form, FormStateInterface $form_state, WebformS
if (isset($submissionData["subventions"]) && is_array($submissionData["subventions"])) {
foreach ($submissionData["subventions"] as $sub) {
if ($sub['subventionType'] == $subventionType) {
$elementTotal = self::convertToFloat($sub['amount']);
$elementTotal = ConvertHelper::convertToFloat($sub['amount']);
break;
}
}
Expand Down Expand Up @@ -1620,30 +1576,6 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
$this->configuration['debug'] = (bool) $form_state->getValue('debug');
}

/**
* Cleans up non-array values from array structure.
*
* This is due to some configuration error with messages/statuses/events
* that I'm not able to find.
*
* @param array|null $value
* Array we need to flatten.
*
* @return array
* Fixed array
*/
public static function cleanUpArrayValues(mixed $value): array {
$retval = [];
if (is_array($value)) {
foreach ($value as $v) {
if (is_array($v)) {
$retval[] = $v;
}
}
}
return $retval;
}

/**
* Parse things from form 3rd party settings to this application.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Drupal\Core\TypedData\ListDataDefinition;
use Drupal\Core\TypedData\ListInterface;
use Drupal\grants_handler\Plugin\WebformHandler\GrantsHandler;

/**
* Service for getting & setting data values from & to JSON structure.
Expand Down Expand Up @@ -75,7 +74,7 @@ private function processToimintaAvustus($arguments) {
'ID' => 'amount',
'label' => 'Euroa',
'valueType' => 'double',
'value' => (string) GrantsHandler::convertToFloat($toimintaAvustus),
'value' => (string) ConvertHelper::convertToFloat($toimintaAvustus),
];

$this->processFieldMeta($elementArray, $elements['yhdistyksen_kuluvan_vuoden_toiminta_avustus'], $page, $section);
Expand All @@ -86,7 +85,7 @@ private function processToimintaAvustus($arguments) {
'ID' => 'usedAmount',
'label' => 'Euroa',
'valueType' => 'double',
'value' => (string) GrantsHandler::convertToFloat($usedToimintaAvustus),
'value' => (string) ConvertHelper::convertToFloat($usedToimintaAvustus),
];

$this->processFieldMeta($elementArray, $elements['yhdistyksen_kuluvan_vuoden_toiminta_avustus'], $page, $section);
Expand Down Expand Up @@ -132,7 +131,7 @@ private function processPalkkausAvustus($arguments) {
'ID' => 'amount',
'label' => 'Euroa',
'valueType' => 'double',
'value' => (string) GrantsHandler::convertToFloat($palkkausAvustus),
'value' => (string) ConvertHelper::convertToFloat($palkkausAvustus),
];

$this->processFieldMeta($elementArray, $elements['yhdistyksen_kuluvan_vuoden_palkkausavustus_'], $page, $section);
Expand All @@ -143,7 +142,7 @@ private function processPalkkausAvustus($arguments) {
'ID' => 'usedAmount',
'label' => 'Euroa',
'valueType' => 'double',
'value' => (string) GrantsHandler::convertToFloat($usedPalkkausAvustus),
'value' => (string) ConvertHelper::convertToFloat($usedPalkkausAvustus),
];

$this->processFieldMeta($elementArray, $elements['selvitys_kuluvan_vuoden_palkkausavustuksen_kaytosta'], $page, $section);
Expand Down
81 changes: 81 additions & 0 deletions public/modules/custom/grants_metadata/src/ConvertHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace Drupal\grants_metadata;

/**
* Helper class for conversion functions.
*/
final class ConvertHelper {

/**
* Convert EUR format value to "int" .
*
* @param string|null $value
* Value to be converted.
*
* @return int|null
* Int value.
*/
public static function convertToInt(?string $value = ''): ?int {
if (is_null($value)) {
return NULL;
}

if ($value === '') {
return NULL;
}

$value = str_replace(['€', ',', ' ', '_'], ['', '.', '', ''], $value);
$value = (int) $value;
return $value;
}

/**
* Convert EUR format value to float.
*
* @param string|null $value
* Value to be converted.
*
* @return float|null
* Floated value.
*/
public static function convertToFloat(?string $value = ''): ?float {
if (is_null($value)) {
return NULL;
}

if ($value === '') {
return NULL;
}

$value = str_replace(['€', ',', ' '], ['', '.', ''], $value);
return (float) $value;
}

/**
* Cleans up non-array values from array structure.
*
* This is due to some configuration error with messages/statuses/events
* that I'm not able to find.
*
* @param array|null $value
* Array we need to flatten.
*
* @return array
* Fixed array
*/
public static function cleanUpArrayValues(mixed $value): array {
$retval = [];
if (is_array($value)) {
foreach ($value as $v) {
if (is_array($v)) {
$retval[] = $v;
}
}
}
return $retval;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Drupal\grants_metadata\Plugin\DataType;

use Drupal\Core\TypedData\Plugin\DataType\Map;
use Drupal\grants_handler\Plugin\WebformHandler\GrantsHandler;
use Drupal\grants_metadata\ConvertHelper;

/**
* Address DataType.
Expand Down Expand Up @@ -35,11 +35,11 @@ public function getValue() {
}
// We need to make sure amount is valid float to get validation working.
if (isset($this->values['amount'])) {
$this->values['amount'] = GrantsHandler::convertToFloat($this->values['amount']);
$this->values['amount'] = ConvertHelper::convertToFloat($this->values['amount']);
}
// We need to make sure amount is valid float to get validation working.
if (isset($this->values['usedAmount'])) {
$this->values['usedAmount'] = GrantsHandler::convertToFloat($this->values['usedAmount']);
$this->values['usedAmount'] = ConvertHelper::convertToFloat($this->values['usedAmount']);
}

return $this->values;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Drupal\grants_metadata\Plugin\DataType;

use Drupal\Core\TypedData\Plugin\DataType\Map;
use Drupal\grants_handler\Plugin\WebformHandler\GrantsHandler;
use Drupal\grants_metadata\ConvertHelper;

/**
* Address DataType.
Expand Down Expand Up @@ -35,7 +35,7 @@ public function getValue() {
}
// We need to make sure amount is valid float to get validation working.
if (isset($this->values['amount'])) {
$this->values['amount'] = GrantsHandler::convertToFloat($this->values['amount']) ?? 0;
$this->values['amount'] = ConvertHelper::convertToFloat($this->values['amount']) ?? 0;
}

return $this->values;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public function getBaseProperties(): array {
'jsonType' => 'double',
])
->setSetting('valueCallback', [
'\Drupal\grants_handler\Plugin\WebformHandler\GrantsHandler',
'\Drupal\grants_metadata\ConvertHelper',
'convertToFloat',
])
->setSetting('jsonPath', [
Expand All @@ -304,7 +304,7 @@ public function getBaseProperties(): array {
'otherAppliedCompensationsTotal',
])
->setSetting('valueCallback', [
'\Drupal\grants_handler\Plugin\WebformHandler\GrantsHandler',
'\Drupal\grants_metadata\ConvertHelper',
'convertToFloat',
])
->addConstraint('NotBlank');
Expand Down Expand Up @@ -406,7 +406,7 @@ public function getBaseProperties(): array {

$info['status_updates'] = MapDataDefinition::create()
->setSetting('valueCallback', [
'\Drupal\grants_handler\Plugin\WebformHandler\GrantsHandler',
'\Drupal\grants_metadata\ConvertHelper',
'cleanUpArrayValues',
])
->setPropertyDefinition(
Expand Down Expand Up @@ -450,7 +450,7 @@ public function getBaseProperties(): array {

$info['events'] = MapDataDefinition::create()
->setSetting('valueCallback', [
'\Drupal\grants_handler\Plugin\WebformHandler\GrantsHandler',
'\Drupal\grants_metadata\ConvertHelper',
'cleanUpArrayValues',
])
->setPropertyDefinition(
Expand Down Expand Up @@ -488,7 +488,7 @@ public function getBaseProperties(): array {

$info['messages'] = MapDataDefinition::create()
->setSetting('valueCallback', [
'\Drupal\grants_handler\Plugin\WebformHandler\GrantsHandler',
'\Drupal\grants_metadata\ConvertHelper',
'cleanUpArrayValues',
])
->setPropertyDefinition(
Expand Down
Loading
Loading