Property.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Validator constraint delegates validation of value's property to wrapped validator.
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Validator\Constraint;
  9. class Property extends \Magento\Framework\Validator\Constraint
  10. {
  11. /**
  12. * Property name
  13. *
  14. * @var string
  15. */
  16. protected $_property;
  17. /**
  18. * Constructor
  19. *
  20. * @param \Magento\Framework\Validator\ValidatorInterface $validator
  21. * @param string $property
  22. * @param string $alias
  23. */
  24. public function __construct(\Magento\Framework\Validator\ValidatorInterface $validator, $property, $alias = null)
  25. {
  26. parent::__construct($validator, $alias);
  27. $this->_property = $property;
  28. }
  29. /**
  30. * Get value that should be validated. Tries to extract value's property if \Magento\Framework\DataObject or
  31. * \ArrayAccess or array is passed
  32. *
  33. * @param mixed $value
  34. * @return mixed
  35. */
  36. protected function _getValidatorValue($value)
  37. {
  38. $result = null;
  39. if ($value instanceof \Magento\Framework\DataObject) {
  40. $result = $value->getDataUsingMethod($this->_property);
  41. } elseif ((is_array($value) || $value instanceof \ArrayAccess) && isset($value[$this->_property])) {
  42. $result = $value[$this->_property];
  43. }
  44. return $result;
  45. }
  46. /**
  47. * Add messages with code of property name
  48. *
  49. * @param array $messages
  50. * @return void
  51. */
  52. protected function _addMessages(array $messages)
  53. {
  54. $this->_messages[$this->_property] = $messages;
  55. }
  56. }