IdGeneratorTrait.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\View\Helper;
  17. use Cake\Utility\Text;
  18. /**
  19. * A trait that provides id generating methods to be
  20. * used in various widget classes.
  21. */
  22. trait IdGeneratorTrait
  23. {
  24. /**
  25. * Prefix for id attribute.
  26. *
  27. * @var string|null
  28. */
  29. protected ?string $_idPrefix = null;
  30. /**
  31. * A list of id suffixes used in the current rendering.
  32. *
  33. * @var array<string>
  34. */
  35. protected array $_idSuffixes = [];
  36. /**
  37. * Clear the stored ID suffixes.
  38. *
  39. * @return void
  40. */
  41. protected function _clearIds(): void
  42. {
  43. $this->_idSuffixes = [];
  44. }
  45. /**
  46. * Generate an ID attribute for an element.
  47. *
  48. * Ensures that id's for a given set of fields are unique.
  49. *
  50. * @param string $name The ID attribute name.
  51. * @param string $val The ID attribute value.
  52. * @return string Generated id.
  53. */
  54. protected function _id(string $name, string $val): string
  55. {
  56. $name = $this->_domId($name);
  57. $suffix = $this->_idSuffix($val);
  58. return trim($name . '-' . $suffix, '-');
  59. }
  60. /**
  61. * Generate an ID suffix.
  62. *
  63. * Ensures that id's for a given set of fields are unique.
  64. *
  65. * @param string $val The ID attribute value.
  66. * @return string Generated id suffix.
  67. */
  68. protected function _idSuffix(string $val): string
  69. {
  70. $idSuffix = mb_strtolower(str_replace(['/', '@', '<', '>', ' ', '"', '\''], '-', $val));
  71. $count = 1;
  72. $check = $idSuffix;
  73. while (in_array($check, $this->_idSuffixes, true)) {
  74. $check = $idSuffix . $count++;
  75. }
  76. $this->_idSuffixes[] = $check;
  77. return $check;
  78. }
  79. /**
  80. * Generate an ID suitable for use in an ID attribute.
  81. *
  82. * @param string $value The value to convert into an ID.
  83. * @return string The generated id.
  84. */
  85. protected function _domId(string $value): string
  86. {
  87. $domId = mb_strtolower(Text::slug($value, '-'));
  88. if ($this->_idPrefix) {
  89. $domId = $this->_idPrefix . '-' . $domId;
  90. }
  91. return $domId;
  92. }
  93. }