Skip to content

Commit

Permalink
[#977] Make sure at least one country is selected
Browse files Browse the repository at this point in the history
  • Loading branch information
pH-7 committed Feb 13, 2022
1 parent d6a7e23 commit 4b846a2
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* @package PH7 / App / System / Core / Form / Processing
*/

declare(strict_types=1);

namespace PH7;

use PH7\Framework\Cache\Cache;
Expand Down Expand Up @@ -35,7 +37,13 @@ public function __construct($sTable)
$oUserModel->clearCountries($this->sTable);

// Then, reindex the table
foreach ($this->httpRequest->post('countries') as $sCountry) {
$aCountries = (array)$this->httpRequest->post('countries');
if ($this->areCountriesNotSet($aCountries)) {
\PFBC\Form::setError('form_country_restriction', t('You need to select at least one country.'));
return;
}

foreach ($aCountries as $sCountry) {
if ($this->isEligibleToAdd($sCountry)) {
$oUserModel->addCountry($sCountry, $this->sTable);
}
Expand All @@ -59,17 +67,17 @@ private function isEligibleToAdd($sCountryCode)
$this->isCountryCodeUppercase($sCountryCode);
}

/**
* @param string $sCountryCode
*
* @return bool
*/
private function isCountryCodeUppercase($sCountryCode)
private function areCountriesNotSet(array $aCountries)
{
return empty($aCountries) || count($aCountries) === 1 && empty($aCountries[0]);
}

private function isCountryCodeUppercase(string $sCountryCode): bool
{
return strtoupper($sCountryCode) === $sCountryCode;
}

private function clearCache()
private function clearCache(): void
{
(new Cache)->start(
UserCoreModel::CACHE_GROUP,
Expand Down

0 comments on commit 4b846a2

Please sign in to comment.