This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Adds a kernel test 2. Remove outdated verification steps from tide_webform.module
- Loading branch information
1 parent
1331175
commit c20eb1b
Showing
5 changed files
with
277 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
modules/tide_webform_jsonapi/src/TideWebformJsonapiHelper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace Drupal\tide_webform_jsonapi; | ||
|
||
/** | ||
* Provides helper functions for tide_webform_jsonapi. | ||
*/ | ||
class TideWebformJsonapiHelper { | ||
|
||
/** | ||
* Extracts validate settings from the webform settings. | ||
*/ | ||
public function webformValidateSettingsExtractor(array $supported_validation, array $original_elements): array { | ||
$res = []; | ||
// Iterate through webform values. | ||
foreach ($original_elements as $w_key => $items) { | ||
// Iterate through supported validation keys and their values. | ||
foreach ($supported_validation as $key => $value) { | ||
// If the current value is an array, check for a match with the key. | ||
if (is_array($value)) { | ||
if (array_key_exists($key, $items)) { | ||
$res[$w_key][$key] = $items[$key]; | ||
} | ||
} | ||
else { | ||
// If the value exists in the items and is not an array, | ||
// store it in the result. | ||
if (array_key_exists($value, $items)) { | ||
$res[$w_key][$value] = $items[$value]; | ||
} | ||
} | ||
} | ||
} | ||
return $res; | ||
} | ||
|
||
/** | ||
* Supported validators. | ||
*/ | ||
public function getSupportedValidateElements() { | ||
return [ | ||
'#required', | ||
'#required_error', | ||
'#pattern', | ||
'#pattern_error', | ||
]; | ||
} | ||
|
||
/** | ||
* Attached supported validator to Payload. | ||
*/ | ||
public function attachValidateSettingsToPayload(array $payload) { | ||
$output = []; | ||
$mapping = [ | ||
'#required' => 'required', | ||
'#required_error' => 'required', | ||
'#pattern' => 'pattern', | ||
'#pattern_error' => 'pattern', | ||
]; | ||
foreach ($payload as $key => $value) { | ||
if (isset($mapping[$key])) { | ||
$output[$mapping[$key]][] = $value; | ||
} | ||
} | ||
return $output; | ||
} | ||
|
||
/** | ||
* Verifies the payload. | ||
*/ | ||
public function validatePayload(array $payload, array $massaged_validates_array) { | ||
$results = []; | ||
foreach ($payload as $id => $value) { | ||
if (array_key_exists($id, $massaged_validates_array)) { | ||
if (!empty($this->generateErrorString($value, $massaged_validates_array[$id]))) { | ||
$results[$id] = $this->generateErrorString($value, $massaged_validates_array[$id]); | ||
} | ||
} | ||
} | ||
return $results; | ||
} | ||
|
||
/** | ||
* Generates error messages. | ||
*/ | ||
public function generateErrorString($value, array $arr) { | ||
$res = []; | ||
foreach ($arr as $k => $v) { | ||
if (call_user_func('tide_webform_' . $k . '_validate', $value, $v) !== TRUE) { | ||
$res[] = call_user_func('tide_webform_' . $k . '_validate', $value, $v); | ||
} | ||
} | ||
return $res; | ||
} | ||
|
||
} |
Oops, something went wrong.