Factory.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Google AdWords Validator Factory
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. * @SuppressWarnings(PHPMD.LongVariable)
  8. */
  9. namespace Magento\GoogleAdwords\Model\Validator;
  10. use Magento\Framework\Validator\IntUtils;
  11. use Magento\Framework\Validator\Regex;
  12. use Magento\Framework\Validator\UniversalFactory;
  13. /**
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class Factory
  18. {
  19. /**
  20. * @var UniversalFactory
  21. */
  22. protected $_validatorBuilderFactory;
  23. /**
  24. * @param UniversalFactory $validatorBuilderFactory
  25. */
  26. public function __construct(UniversalFactory $validatorBuilderFactory)
  27. {
  28. $this->_validatorBuilderFactory = $validatorBuilderFactory;
  29. }
  30. /**
  31. * Create color validator
  32. *
  33. * @param string $currentColor
  34. * @return \Magento\Framework\Validator
  35. */
  36. public function createColorValidator($currentColor)
  37. {
  38. $message = __(
  39. 'Conversion Color value is not valid "%1". Please set hexadecimal 6-digit value.',
  40. $currentColor
  41. );
  42. /** @var \Magento\Framework\Validator\Builder $builder */
  43. $builder = $this->_validatorBuilderFactory->create(
  44. \Magento\Framework\Validator\Builder::class,
  45. [
  46. 'constraints' => [
  47. [
  48. 'alias' => 'Regex',
  49. 'type' => '',
  50. 'class' => \Magento\Framework\Validator\Regex::class,
  51. 'options' => [
  52. 'arguments' => ['pattern' => '/^[0-9a-f]{6}$/i'],
  53. 'methods' => [
  54. [
  55. 'method' => 'setMessages',
  56. 'arguments' => [
  57. [Regex::NOT_MATCH => $message, Regex::INVALID => $message],
  58. ],
  59. ],
  60. ],
  61. ],
  62. ],
  63. ]
  64. ]
  65. );
  66. return $builder->createValidator();
  67. }
  68. /**
  69. * Create Conversion id validator
  70. *
  71. * @param int|string $currentId
  72. * @return \Magento\Framework\Validator
  73. */
  74. public function createConversionIdValidator($currentId)
  75. {
  76. $message = __('Conversion Id value is not valid "%1". Conversion Id should be an integer.', $currentId);
  77. /** @var \Magento\Framework\Validator\Builder $builder */
  78. $builder = $this->_validatorBuilderFactory->create(
  79. \Magento\Framework\Validator\Builder::class,
  80. [
  81. 'constraints' => [
  82. [
  83. 'alias' => 'Int',
  84. 'type' => '',
  85. 'class' => \Magento\Framework\Validator\IntUtils::class,
  86. 'options' => [
  87. 'methods' => [
  88. [
  89. 'method' => 'setMessages',
  90. 'arguments' => [[IntUtils::NOT_INT => $message, IntUtils::INVALID => $message]],
  91. ],
  92. ],
  93. ],
  94. ],
  95. ]
  96. ]
  97. );
  98. return $builder->createValidator();
  99. }
  100. }