KeyLength.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Model\Oauth\Consumer\Validator;
  7. /**
  8. * Validate OAuth keys
  9. */
  10. class KeyLength extends \Zend_Validate_StringLength
  11. {
  12. /**
  13. * Default key name
  14. *
  15. * @var string
  16. */
  17. protected $_name = 'Key';
  18. /**
  19. * @var array
  20. */
  21. protected $_messageTemplates = [
  22. self::INVALID => "Invalid type given for %name%. String expected",
  23. self::TOO_SHORT => "%name% '%value%' is less than %min% characters long",
  24. self::TOO_LONG => "%name% '%value%' is more than %max% characters long",
  25. ];
  26. /**
  27. * Additional variables available for validation failure messages
  28. *
  29. * @var array
  30. */
  31. protected $_messageVariables = ['min' => '_min', 'max' => '_max', 'name' => '_name'];
  32. /**
  33. * Sets KeyLength validator options
  34. *
  35. * Default encoding is set to utf-8 if none provided
  36. * New option name added to allow adding key name in validation error messages
  37. *
  38. * @inheritdoc
  39. */
  40. public function __construct($options = [])
  41. {
  42. if (!is_array($options)) {
  43. $options = func_get_args();
  44. if (!isset($options[1])) {
  45. $options[1] = 'utf-8';
  46. }
  47. parent::__construct($options[0], $options[0], $options[1]);
  48. return;
  49. } else {
  50. if (isset($options['length'])) {
  51. $options['max'] = $options['min'] = $options['length'];
  52. }
  53. if (isset($options['name'])) {
  54. $this->_name = $options['name'];
  55. }
  56. }
  57. parent::__construct($options);
  58. }
  59. /**
  60. * Set length
  61. *
  62. * @param int|null $length
  63. * @return $this
  64. */
  65. public function setLength($length)
  66. {
  67. parent::setMax($length);
  68. parent::setMin($length);
  69. return $this;
  70. }
  71. /**
  72. * Set length
  73. *
  74. * @return int
  75. */
  76. public function getLength()
  77. {
  78. return parent::getMin();
  79. }
  80. /**
  81. * Defined by \Zend_Validate_Interface
  82. *
  83. * Returns true if and only if the string length of $value is at least the min option and
  84. * no greater than the max option (when the max option is not null).
  85. *
  86. * @param string $value
  87. * @return boolean
  88. * @throws \Exception
  89. */
  90. public function isValid($value)
  91. {
  92. $result = parent::isValid($value);
  93. if (!$result && isset($this->_messages[self::INVALID])) {
  94. throw new \Exception($this->_messages[self::INVALID]);
  95. }
  96. return $result;
  97. }
  98. /**
  99. * Set key name
  100. *
  101. * @param string $name
  102. * @return $this
  103. */
  104. public function setName($name)
  105. {
  106. $this->_name = $name;
  107. return $this;
  108. }
  109. /**
  110. * Get key name
  111. *
  112. * @return string
  113. */
  114. public function getName()
  115. {
  116. return $this->_name;
  117. }
  118. }