Validator.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order\Invoice\Comment;
  7. use Magento\Sales\Model\Order\Invoice\Comment;
  8. /**
  9. * Class Validator
  10. */
  11. class Validator
  12. {
  13. /**
  14. * Required field
  15. *
  16. * @var array
  17. */
  18. protected $required = [
  19. 'parent_id' => 'Parent Invoice Id',
  20. 'comment' => 'Comment',
  21. ];
  22. /**
  23. * Validate data
  24. *
  25. * @param \Magento\Sales\Model\Order\Invoice\Comment $comment
  26. * @return array
  27. */
  28. public function validate(Comment $comment)
  29. {
  30. $errors = [];
  31. $commentData = $comment->getData();
  32. foreach ($this->required as $code => $label) {
  33. if (!$comment->hasData($code)) {
  34. $errors[$code] = sprintf('"%s" is required. Enter and try again.', $label);
  35. } elseif (empty($commentData[$code])) {
  36. $errors[$code] = sprintf('%s can not be empty', $label);
  37. }
  38. }
  39. return $errors;
  40. }
  41. }