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