| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Form\LegacyConsumer; |
| 4 |
|
| 5 |
/** |
| 6 |
* @since 2.14.0 |
| 7 |
*/ |
| 8 |
class UniqueIdAttributeGenerator |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @var array |
| 12 |
*/ |
| 13 |
private $formCounter = []; |
| 14 |
|
| 15 |
/** |
| 16 |
* @since 2.14.0 |
| 17 |
* |
| 18 |
* @param int $formId |
| 19 |
*/ |
| 20 |
private function increaseCounter($formId) |
| 21 |
{ |
| 22 |
if ( ! isset($this->formCounter[$formId])) { |
| 23 |
$this->formCounter[$formId] = 1; |
| 24 |
|
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
$this->formCounter[$formId]++; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @since 2.14.0 |
| 33 |
* |
| 34 |
* @param int $formId |
| 35 |
* @param string $fieldName |
| 36 |
* |
| 37 |
* @return string |
| 38 |
*/ |
| 39 |
public function getId($formId, $fieldName) |
| 40 |
{ |
| 41 |
$id = "give-$fieldName-$formId-{$this->getCounterValue( $formId )}"; |
| 42 |
$this->increaseCounter($formId); |
| 43 |
|
| 44 |
return $id; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @since 2.14.0 |
| 49 |
* |
| 50 |
* @param int $formId |
| 51 |
*/ |
| 52 |
private function getCounterValue($formId) |
| 53 |
{ |
| 54 |
return ! empty($this->formCounter[$formId]) ? $this->formCounter[$formId] : 1; |
| 55 |
} |
| 56 |
} |
| 57 |
|