AbstractData.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. <?php
  2. /**
  3. * Form Element Abstract Data Model
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Customer\Model\Metadata\Form;
  9. use Magento\Framework\Api\ArrayObjectSearch;
  10. use Magento\Framework\Validator\EmailAddress;
  11. /**
  12. * Form Element Abstract Data Model
  13. *
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. abstract class AbstractData
  17. {
  18. /**
  19. * Request Scope name
  20. *
  21. * @var string
  22. */
  23. protected $_requestScope;
  24. /**
  25. * Scope visibility flag
  26. *
  27. * @var boolean
  28. */
  29. protected $_requestScopeOnly = true;
  30. /**
  31. * Is AJAX request flag
  32. *
  33. * @var boolean
  34. */
  35. protected $_isAjax = false;
  36. /**
  37. * Array of full extracted data
  38. * Needed for depends attributes
  39. *
  40. * @var array
  41. */
  42. protected $_extractedData = [];
  43. /**
  44. * Date filter format
  45. *
  46. * @var string
  47. */
  48. protected $_dateFilterFormat;
  49. /**
  50. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  51. */
  52. protected $_localeDate;
  53. /**
  54. * @var \Magento\Framework\Locale\ResolverInterface
  55. */
  56. protected $_localeResolver;
  57. /**
  58. * @var \Psr\Log\LoggerInterface
  59. */
  60. protected $_logger;
  61. /**
  62. * @var \Magento\Customer\Api\Data\AttributeMetadataInterface
  63. */
  64. protected $_attribute;
  65. /**
  66. * @var string|int|bool
  67. */
  68. protected $_value;
  69. /**
  70. * @var string
  71. */
  72. protected $_entityTypeCode;
  73. /**
  74. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  75. * @param \Psr\Log\LoggerInterface $logger
  76. * @param \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute
  77. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  78. * @param string|int|bool $value
  79. * @param string $entityTypeCode
  80. * @param bool $isAjax
  81. */
  82. public function __construct(
  83. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  84. \Psr\Log\LoggerInterface $logger,
  85. \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute,
  86. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  87. $value,
  88. $entityTypeCode,
  89. $isAjax = false
  90. ) {
  91. $this->_localeDate = $localeDate;
  92. $this->_logger = $logger;
  93. $this->_attribute = $attribute;
  94. $this->_localeResolver = $localeResolver;
  95. $this->_value = $value;
  96. $this->_entityTypeCode = $entityTypeCode;
  97. $this->_isAjax = $isAjax;
  98. }
  99. /**
  100. * Return Attribute instance
  101. *
  102. * @return \Magento\Customer\Api\Data\AttributeMetadataInterface
  103. * @throws \Magento\Framework\Exception\LocalizedException
  104. */
  105. public function getAttribute()
  106. {
  107. if (!$this->_attribute) {
  108. throw new \Magento\Framework\Exception\LocalizedException(__('Attribute object is undefined'));
  109. }
  110. return $this->_attribute;
  111. }
  112. /**
  113. * Set Request scope
  114. *
  115. * @param string $scope
  116. * @return $this
  117. */
  118. public function setRequestScope($scope)
  119. {
  120. $this->_requestScope = $scope;
  121. return $this;
  122. }
  123. /**
  124. * Set scope visibility
  125. *
  126. * Search value only in scope or search value in scope and global
  127. *
  128. * @param boolean $flag
  129. * @return $this
  130. */
  131. public function setRequestScopeOnly($flag)
  132. {
  133. $this->_requestScopeOnly = (bool)$flag;
  134. return $this;
  135. }
  136. /**
  137. * Set array of full extracted data
  138. *
  139. * @param array $data
  140. * @return $this
  141. */
  142. public function setExtractedData(array $data)
  143. {
  144. $this->_extractedData = $data;
  145. return $this;
  146. }
  147. /**
  148. * Return extracted data
  149. *
  150. * @param string $index
  151. * @return array|null
  152. */
  153. public function getExtractedData($index = null)
  154. {
  155. if ($index !== null) {
  156. if (isset($this->_extractedData[$index])) {
  157. return $this->_extractedData[$index];
  158. }
  159. return null;
  160. }
  161. return $this->_extractedData;
  162. }
  163. /**
  164. * Apply attribute input filter to value
  165. *
  166. * @param string $value
  167. * @return string|bool
  168. */
  169. protected function _applyInputFilter($value)
  170. {
  171. if ($value === false) {
  172. return false;
  173. }
  174. $filter = $this->_getFormFilter();
  175. if ($filter) {
  176. $value = $filter->inputFilter($value);
  177. }
  178. return $value;
  179. }
  180. /**
  181. * Return Data Form Input/Output Filter
  182. *
  183. * @return \Magento\Framework\Data\Form\Filter\FilterInterface|false
  184. */
  185. protected function _getFormFilter()
  186. {
  187. $filterCode = $this->getAttribute()->getInputFilter();
  188. if ($filterCode) {
  189. $filterClass = 'Magento\Framework\Data\Form\Filter\\' . ucfirst($filterCode);
  190. if ($filterCode == 'date') {
  191. $filter = new $filterClass($this->_dateFilterFormat(), $this->_localeResolver);
  192. } else {
  193. $filter = new $filterClass();
  194. }
  195. return $filter;
  196. }
  197. return false;
  198. }
  199. /**
  200. * Get/Set/Reset date filter format
  201. *
  202. * @param string|null|false $format
  203. * @return $this|string
  204. */
  205. protected function _dateFilterFormat($format = null)
  206. {
  207. if ($format === null) {
  208. // get format
  209. if ($this->_dateFilterFormat === null) {
  210. $this->_dateFilterFormat = \IntlDateFormatter::SHORT;
  211. }
  212. return $this->_localeDate->getDateFormat($this->_dateFilterFormat);
  213. } elseif ($format === false) {
  214. // reset value
  215. $this->_dateFilterFormat = null;
  216. return $this;
  217. }
  218. $this->_dateFilterFormat = $format;
  219. return $this;
  220. }
  221. /**
  222. * Apply attribute output filter to value
  223. *
  224. * @param string $value
  225. * @return string
  226. */
  227. protected function _applyOutputFilter($value)
  228. {
  229. $filter = $this->_getFormFilter();
  230. if ($filter) {
  231. $value = $filter->outputFilter($value);
  232. }
  233. return $value;
  234. }
  235. /**
  236. * Validate value by attribute input validation rule
  237. *
  238. * @param string $value
  239. * @return array|true
  240. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  241. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  242. */
  243. protected function _validateInputRule($value)
  244. {
  245. // skip validate empty value
  246. if (empty($value)) {
  247. return true;
  248. }
  249. $label = $this->getAttribute()->getStoreLabel();
  250. $validateRules = $this->getAttribute()->getValidationRules();
  251. $inputValidation = ArrayObjectSearch::getArrayElementByName(
  252. $validateRules,
  253. 'input_validation'
  254. );
  255. if ($inputValidation !== null) {
  256. $allowWhiteSpace = false;
  257. switch ($inputValidation) {
  258. case 'alphanum-with-spaces':
  259. $allowWhiteSpace = true;
  260. // Continue to alphanumeric validation
  261. case 'alphanumeric':
  262. $validator = new \Zend_Validate_Alnum($allowWhiteSpace);
  263. $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Alnum::INVALID);
  264. $validator->setMessage(
  265. __('"%1" contains non-alphabetic or non-numeric characters.', $label),
  266. \Zend_Validate_Alnum::NOT_ALNUM
  267. );
  268. $validator->setMessage(__('"%1" is an empty string.', $label), \Zend_Validate_Alnum::STRING_EMPTY);
  269. if (!$validator->isValid($value)) {
  270. return $validator->getMessages();
  271. }
  272. break;
  273. case 'numeric':
  274. $validator = new \Zend_Validate_Digits();
  275. $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Digits::INVALID);
  276. $validator->setMessage(
  277. __('"%1" contains non-numeric characters.', $label),
  278. \Zend_Validate_Digits::NOT_DIGITS
  279. );
  280. $validator->setMessage(
  281. __('"%1" is an empty string.', $label),
  282. \Zend_Validate_Digits::STRING_EMPTY
  283. );
  284. if (!$validator->isValid($value)) {
  285. return $validator->getMessages();
  286. }
  287. break;
  288. case 'alpha':
  289. $validator = new \Zend_Validate_Alpha(true);
  290. $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Alpha::INVALID);
  291. $validator->setMessage(
  292. __('"%1" contains non-alphabetic characters.', $label),
  293. \Zend_Validate_Alpha::NOT_ALPHA
  294. );
  295. $validator->setMessage(__('"%1" is an empty string.', $label), \Zend_Validate_Alpha::STRING_EMPTY);
  296. if (!$validator->isValid($value)) {
  297. return $validator->getMessages();
  298. }
  299. break;
  300. case 'email':
  301. /**
  302. __("'%value%' appears to be a DNS hostname but the given punycode notation cannot be decoded")
  303. __("Invalid type given. String expected")
  304. __("'%value%' appears to be a DNS hostname but contains a dash in an invalid position")
  305. __("'%value%' does not match the expected structure for a DNS hostname")
  306. __("'%value%' appears to be a DNS hostname but cannot match
  307. * against hostname schema for TLD '%tld%'")
  308. __("'%value%' does not appear to be a valid local network name")
  309. __("'%value%' does not appear to be a valid URI hostname")
  310. __("'%value%' appears to be an IP address, but IP addresses are not allowed")
  311. __("'%value%' appears to be a local network name but local network names are not allowed")
  312. __("'%value%' appears to be a DNS hostname but cannot extract TLD part")
  313. __("'%value%' appears to be a DNS hostname but cannot match TLD against known list")
  314. */
  315. $validator = new EmailAddress();
  316. $validator->setMessage(
  317. __('"%1" invalid type entered.', $label),
  318. \Zend_Validate_EmailAddress::INVALID
  319. );
  320. $validator->setMessage(
  321. __('"%1" is not a valid email address.', $label),
  322. \Zend_Validate_EmailAddress::INVALID_FORMAT
  323. );
  324. $validator->setMessage(
  325. __('"%1" is not a valid hostname.', $label),
  326. \Zend_Validate_EmailAddress::INVALID_HOSTNAME
  327. );
  328. $validator->setMessage(
  329. __('"%1" is not a valid hostname.', $label),
  330. \Zend_Validate_EmailAddress::INVALID_MX_RECORD
  331. );
  332. $validator->setMessage(
  333. __('"%1" is not a valid hostname.', $label),
  334. \Zend_Validate_EmailAddress::INVALID_MX_RECORD
  335. );
  336. $validator->setMessage(
  337. __('"%1" is not a valid email address.', $label),
  338. \Zend_Validate_EmailAddress::DOT_ATOM
  339. );
  340. $validator->setMessage(
  341. __('"%1" is not a valid email address.', $label),
  342. \Zend_Validate_EmailAddress::QUOTED_STRING
  343. );
  344. $validator->setMessage(
  345. __('"%1" is not a valid email address.', $label),
  346. \Zend_Validate_EmailAddress::INVALID_LOCAL_PART
  347. );
  348. $validator->setMessage(
  349. __('"%1" uses too many characters.', $label),
  350. \Zend_Validate_EmailAddress::LENGTH_EXCEEDED
  351. );
  352. $validator->setMessage(
  353. __("'%value%' looks like an IP address, which is not an acceptable format."),
  354. \Zend_Validate_Hostname::IP_ADDRESS_NOT_ALLOWED
  355. );
  356. $validator->setMessage(
  357. __("'%value%' looks like a DNS hostname but contains a dash in an invalid position."),
  358. \Zend_Validate_Hostname::INVALID_DASH
  359. );
  360. $validator->setMessage(
  361. __(
  362. "'%value%' looks like a DNS hostname but we cannot match it against "
  363. . "the hostname schema for TLD '%tld%'."
  364. ),
  365. \Zend_Validate_Hostname::INVALID_HOSTNAME_SCHEMA
  366. );
  367. $validator->setMessage(
  368. __("'%value%' looks like a DNS hostname but cannot extract TLD part."),
  369. \Zend_Validate_Hostname::UNDECIPHERABLE_TLD
  370. );
  371. $validator->setMessage(
  372. __("'%value%' does not look like a valid local network name."),
  373. \Zend_Validate_Hostname::INVALID_LOCAL_NAME
  374. );
  375. $validator->setMessage(
  376. __("'%value%' looks like a local network name, which is not an acceptable format."),
  377. \Zend_Validate_Hostname::LOCAL_NAME_NOT_ALLOWED
  378. );
  379. $validator->setMessage(
  380. __(
  381. "'%value%' appears to be a DNS hostname, but the given punycode notation cannot be decoded."
  382. ),
  383. \Zend_Validate_Hostname::CANNOT_DECODE_PUNYCODE
  384. );
  385. if (!$validator->isValid($value)) {
  386. return array_unique($validator->getMessages());
  387. }
  388. break;
  389. case 'url':
  390. $parsedUrl = parse_url($value);
  391. if ($parsedUrl === false || empty($parsedUrl['scheme']) || empty($parsedUrl['host'])) {
  392. return [__('"%1" is not a valid URL.', $label)];
  393. }
  394. $validator = new \Zend_Validate_Hostname();
  395. if (!$validator->isValid($parsedUrl['host'])) {
  396. return [__('"%1" is not a valid URL.', $label)];
  397. }
  398. break;
  399. case 'date':
  400. $validator = new \Zend_Validate_Date(\Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT);
  401. $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Date::INVALID);
  402. $validator->setMessage(__('"%1" is not a valid date.', $label), \Zend_Validate_Date::INVALID_DATE);
  403. $validator->setMessage(
  404. __('"%1" does not fit the entered date format.', $label),
  405. \Zend_Validate_Date::FALSEFORMAT
  406. );
  407. if (!$validator->isValid($value)) {
  408. return array_unique($validator->getMessages());
  409. }
  410. break;
  411. }
  412. }
  413. return true;
  414. }
  415. /**
  416. * Return is AJAX Request
  417. *
  418. * @return boolean
  419. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  420. */
  421. public function getIsAjaxRequest()
  422. {
  423. return $this->_isAjax;
  424. }
  425. /**
  426. * Return Original Attribute value from Request
  427. *
  428. * @param \Magento\Framework\App\RequestInterface $request
  429. * @return mixed
  430. */
  431. protected function _getRequestValue(\Magento\Framework\App\RequestInterface $request)
  432. {
  433. $attrCode = $this->getAttribute()->getAttributeCode();
  434. if ($this->_requestScope) {
  435. if (strpos($this->_requestScope, '/') !== false) {
  436. $params = $request->getParams();
  437. $parts = explode('/', $this->_requestScope);
  438. foreach ($parts as $part) {
  439. if (isset($params[$part])) {
  440. $params = $params[$part];
  441. } else {
  442. $params = [];
  443. }
  444. }
  445. } else {
  446. $params = $request->getParam($this->_requestScope);
  447. }
  448. if (isset($params[$attrCode])) {
  449. $value = $params[$attrCode];
  450. } else {
  451. $value = false;
  452. }
  453. if (!$this->_requestScopeOnly && $value === false) {
  454. $value = $request->getParam($attrCode, false);
  455. }
  456. } else {
  457. $value = $request->getParam($attrCode, false);
  458. }
  459. return $value;
  460. }
  461. /**
  462. * Extract data from request and return value
  463. *
  464. * @param \Magento\Framework\App\RequestInterface $request
  465. * @return array|string
  466. */
  467. abstract public function extractValue(\Magento\Framework\App\RequestInterface $request);
  468. /**
  469. * Validate data
  470. *
  471. * @param array|string|null $value
  472. * @return array|bool
  473. * @throws \Magento\Framework\Exception\LocalizedException
  474. */
  475. abstract public function validateValue($value);
  476. /**
  477. * Export attribute value
  478. *
  479. * @param array|string $value
  480. * @return array|string|bool
  481. */
  482. abstract public function compactValue($value);
  483. /**
  484. * Restore attribute value from SESSION
  485. *
  486. * @param array|string $value
  487. * @return array|string|bool
  488. */
  489. abstract public function restoreValue($value);
  490. /**
  491. * Return formatted attribute value from entity model
  492. *
  493. * @param string $format
  494. * @return string|array
  495. */
  496. abstract public function outputValue(
  497. $format = \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_TEXT
  498. );
  499. }