-
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.
Merge pull request #1508 from younginnovations/1492-remove-requiremen…
…t-for-actual-and-target-value-against-a-result-period Review: 1492-remove-requirement-for-actual-and-target-value-against-a-result-period
- Loading branch information
Showing
3 changed files
with
150 additions
and
27 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
114 changes: 114 additions & 0 deletions
114
app/Rules/RequiredEitherNumericTargetValueOrActualValue.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,114 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Rules; | ||
|
||
use Illuminate\Contracts\Validation\Rule; | ||
use Illuminate\Support\Arr; | ||
|
||
/** | ||
* @class RequiredEitherNumericTargetValueOrActualValue | ||
* | ||
* Change source: https://github.com/younginnovations/iatipublisher/issues/1492 | ||
*/ | ||
class RequiredEitherNumericTargetValueOrActualValue implements Rule | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public string $errorType = ''; | ||
|
||
/** | ||
* Create a new rule instance. | ||
* | ||
* @param string $field | ||
* @param string $otherField | ||
* @param array $formFields | ||
*/ | ||
public function __construct(protected string $field, protected string $otherField, protected array $formFields) | ||
{ | ||
} | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param string $attribute | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function passes($attribute, $value): bool | ||
{ | ||
if ($this->bothTargetAnDActualHaveEmptyValues()) { | ||
return false; | ||
} | ||
|
||
/** | ||
* I'm doing this because $value can be null or N empty spaces. | ||
* Null during creation/untouched value field. | ||
* Empty spaces when value filed data is edited/cleared. | ||
* strlen > 0 because I'm allowing ''. | ||
*/ | ||
$value = trim($value ?? ''); | ||
|
||
if ($this->otherFieldHasAtleastOneNonEmptyValue() && strlen($value) > 0 && !is_numeric($value)) { | ||
$this->errorType = 'numeric'; | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @return string | ||
*/ | ||
public function message(): string | ||
{ | ||
if ($this->errorType === 'numeric') { | ||
return 'The @value field must be numeric.'; | ||
} | ||
|
||
return sprintf('%s value is required if %s value is not provided.', ucfirst($this->field), $this->otherField); | ||
} | ||
|
||
/** | ||
* Check if both target and actual fields have empty value field. | ||
* | ||
* @return bool | ||
*/ | ||
private function bothTargetAndActualHaveEmptyValues(): bool | ||
{ | ||
$targetValues = Arr::get($this->formFields, 'target', []); | ||
$actualValues = Arr::get($this->formFields, 'actual', []); | ||
|
||
$targetAllNonEmpty = !collect($targetValues)->every(function ($item) { | ||
return !empty(trim($item['value'] ?? '')); | ||
}); | ||
|
||
$actualAllNonEmpty = !collect($actualValues)->every(function ($item) { | ||
return !empty(trim($item['value'] ?? '')); | ||
}); | ||
|
||
return $targetAllNonEmpty && $actualAllNonEmpty; | ||
} | ||
|
||
/** | ||
* Check if the other field (target or actual) has at least one non-empty value field. | ||
* | ||
* @return bool | ||
*/ | ||
private function otherFieldHasAtleastOneNonEmptyValue(): bool | ||
{ | ||
foreach ($this->formFields[$this->otherField] as $item) { | ||
$item = trim($item['value'] ?? ''); | ||
if ($item !== '') { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
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