123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- <?php
- /**
- * Form Element Abstract Data Model
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Model\Metadata\Form;
- use Magento\Framework\Api\ArrayObjectSearch;
- use Magento\Framework\Validator\EmailAddress;
- /**
- * Form Element Abstract Data Model
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- abstract class AbstractData
- {
- /**
- * Request Scope name
- *
- * @var string
- */
- protected $_requestScope;
- /**
- * Scope visibility flag
- *
- * @var boolean
- */
- protected $_requestScopeOnly = true;
- /**
- * Is AJAX request flag
- *
- * @var boolean
- */
- protected $_isAjax = false;
- /**
- * Array of full extracted data
- * Needed for depends attributes
- *
- * @var array
- */
- protected $_extractedData = [];
- /**
- * Date filter format
- *
- * @var string
- */
- protected $_dateFilterFormat;
- /**
- * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
- */
- protected $_localeDate;
- /**
- * @var \Magento\Framework\Locale\ResolverInterface
- */
- protected $_localeResolver;
- /**
- * @var \Psr\Log\LoggerInterface
- */
- protected $_logger;
- /**
- * @var \Magento\Customer\Api\Data\AttributeMetadataInterface
- */
- protected $_attribute;
- /**
- * @var string|int|bool
- */
- protected $_value;
- /**
- * @var string
- */
- protected $_entityTypeCode;
- /**
- * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
- * @param \Psr\Log\LoggerInterface $logger
- * @param \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute
- * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
- * @param string|int|bool $value
- * @param string $entityTypeCode
- * @param bool $isAjax
- */
- public function __construct(
- \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
- \Psr\Log\LoggerInterface $logger,
- \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute,
- \Magento\Framework\Locale\ResolverInterface $localeResolver,
- $value,
- $entityTypeCode,
- $isAjax = false
- ) {
- $this->_localeDate = $localeDate;
- $this->_logger = $logger;
- $this->_attribute = $attribute;
- $this->_localeResolver = $localeResolver;
- $this->_value = $value;
- $this->_entityTypeCode = $entityTypeCode;
- $this->_isAjax = $isAjax;
- }
- /**
- * Return Attribute instance
- *
- * @return \Magento\Customer\Api\Data\AttributeMetadataInterface
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function getAttribute()
- {
- if (!$this->_attribute) {
- throw new \Magento\Framework\Exception\LocalizedException(__('Attribute object is undefined'));
- }
- return $this->_attribute;
- }
- /**
- * Set Request scope
- *
- * @param string $scope
- * @return $this
- */
- public function setRequestScope($scope)
- {
- $this->_requestScope = $scope;
- return $this;
- }
- /**
- * Set scope visibility
- *
- * Search value only in scope or search value in scope and global
- *
- * @param boolean $flag
- * @return $this
- */
- public function setRequestScopeOnly($flag)
- {
- $this->_requestScopeOnly = (bool)$flag;
- return $this;
- }
- /**
- * Set array of full extracted data
- *
- * @param array $data
- * @return $this
- */
- public function setExtractedData(array $data)
- {
- $this->_extractedData = $data;
- return $this;
- }
- /**
- * Return extracted data
- *
- * @param string $index
- * @return array|null
- */
- public function getExtractedData($index = null)
- {
- if ($index !== null) {
- if (isset($this->_extractedData[$index])) {
- return $this->_extractedData[$index];
- }
- return null;
- }
- return $this->_extractedData;
- }
- /**
- * Apply attribute input filter to value
- *
- * @param string $value
- * @return string|bool
- */
- protected function _applyInputFilter($value)
- {
- if ($value === false) {
- return false;
- }
- $filter = $this->_getFormFilter();
- if ($filter) {
- $value = $filter->inputFilter($value);
- }
- return $value;
- }
- /**
- * Return Data Form Input/Output Filter
- *
- * @return \Magento\Framework\Data\Form\Filter\FilterInterface|false
- */
- protected function _getFormFilter()
- {
- $filterCode = $this->getAttribute()->getInputFilter();
- if ($filterCode) {
- $filterClass = 'Magento\Framework\Data\Form\Filter\\' . ucfirst($filterCode);
- if ($filterCode == 'date') {
- $filter = new $filterClass($this->_dateFilterFormat(), $this->_localeResolver);
- } else {
- $filter = new $filterClass();
- }
- return $filter;
- }
- return false;
- }
- /**
- * Get/Set/Reset date filter format
- *
- * @param string|null|false $format
- * @return $this|string
- */
- protected function _dateFilterFormat($format = null)
- {
- if ($format === null) {
- // get format
- if ($this->_dateFilterFormat === null) {
- $this->_dateFilterFormat = \IntlDateFormatter::SHORT;
- }
- return $this->_localeDate->getDateFormat($this->_dateFilterFormat);
- } elseif ($format === false) {
- // reset value
- $this->_dateFilterFormat = null;
- return $this;
- }
- $this->_dateFilterFormat = $format;
- return $this;
- }
- /**
- * Apply attribute output filter to value
- *
- * @param string $value
- * @return string
- */
- protected function _applyOutputFilter($value)
- {
- $filter = $this->_getFormFilter();
- if ($filter) {
- $value = $filter->outputFilter($value);
- }
- return $value;
- }
- /**
- * Validate value by attribute input validation rule
- *
- * @param string $value
- * @return array|true
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- protected function _validateInputRule($value)
- {
- // skip validate empty value
- if (empty($value)) {
- return true;
- }
- $label = $this->getAttribute()->getStoreLabel();
- $validateRules = $this->getAttribute()->getValidationRules();
- $inputValidation = ArrayObjectSearch::getArrayElementByName(
- $validateRules,
- 'input_validation'
- );
- if ($inputValidation !== null) {
- $allowWhiteSpace = false;
- switch ($inputValidation) {
- case 'alphanum-with-spaces':
- $allowWhiteSpace = true;
- // Continue to alphanumeric validation
- case 'alphanumeric':
- $validator = new \Zend_Validate_Alnum($allowWhiteSpace);
- $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Alnum::INVALID);
- $validator->setMessage(
- __('"%1" contains non-alphabetic or non-numeric characters.', $label),
- \Zend_Validate_Alnum::NOT_ALNUM
- );
- $validator->setMessage(__('"%1" is an empty string.', $label), \Zend_Validate_Alnum::STRING_EMPTY);
- if (!$validator->isValid($value)) {
- return $validator->getMessages();
- }
- break;
- case 'numeric':
- $validator = new \Zend_Validate_Digits();
- $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Digits::INVALID);
- $validator->setMessage(
- __('"%1" contains non-numeric characters.', $label),
- \Zend_Validate_Digits::NOT_DIGITS
- );
- $validator->setMessage(
- __('"%1" is an empty string.', $label),
- \Zend_Validate_Digits::STRING_EMPTY
- );
- if (!$validator->isValid($value)) {
- return $validator->getMessages();
- }
- break;
- case 'alpha':
- $validator = new \Zend_Validate_Alpha(true);
- $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Alpha::INVALID);
- $validator->setMessage(
- __('"%1" contains non-alphabetic characters.', $label),
- \Zend_Validate_Alpha::NOT_ALPHA
- );
- $validator->setMessage(__('"%1" is an empty string.', $label), \Zend_Validate_Alpha::STRING_EMPTY);
- if (!$validator->isValid($value)) {
- return $validator->getMessages();
- }
- break;
- case 'email':
- /**
- __("'%value%' appears to be a DNS hostname but the given punycode notation cannot be decoded")
- __("Invalid type given. String expected")
- __("'%value%' appears to be a DNS hostname but contains a dash in an invalid position")
- __("'%value%' does not match the expected structure for a DNS hostname")
- __("'%value%' appears to be a DNS hostname but cannot match
- * against hostname schema for TLD '%tld%'")
- __("'%value%' does not appear to be a valid local network name")
- __("'%value%' does not appear to be a valid URI hostname")
- __("'%value%' appears to be an IP address, but IP addresses are not allowed")
- __("'%value%' appears to be a local network name but local network names are not allowed")
- __("'%value%' appears to be a DNS hostname but cannot extract TLD part")
- __("'%value%' appears to be a DNS hostname but cannot match TLD against known list")
- */
- $validator = new EmailAddress();
- $validator->setMessage(
- __('"%1" invalid type entered.', $label),
- \Zend_Validate_EmailAddress::INVALID
- );
- $validator->setMessage(
- __('"%1" is not a valid email address.', $label),
- \Zend_Validate_EmailAddress::INVALID_FORMAT
- );
- $validator->setMessage(
- __('"%1" is not a valid hostname.', $label),
- \Zend_Validate_EmailAddress::INVALID_HOSTNAME
- );
- $validator->setMessage(
- __('"%1" is not a valid hostname.', $label),
- \Zend_Validate_EmailAddress::INVALID_MX_RECORD
- );
- $validator->setMessage(
- __('"%1" is not a valid hostname.', $label),
- \Zend_Validate_EmailAddress::INVALID_MX_RECORD
- );
- $validator->setMessage(
- __('"%1" is not a valid email address.', $label),
- \Zend_Validate_EmailAddress::DOT_ATOM
- );
- $validator->setMessage(
- __('"%1" is not a valid email address.', $label),
- \Zend_Validate_EmailAddress::QUOTED_STRING
- );
- $validator->setMessage(
- __('"%1" is not a valid email address.', $label),
- \Zend_Validate_EmailAddress::INVALID_LOCAL_PART
- );
- $validator->setMessage(
- __('"%1" uses too many characters.', $label),
- \Zend_Validate_EmailAddress::LENGTH_EXCEEDED
- );
- $validator->setMessage(
- __("'%value%' looks like an IP address, which is not an acceptable format."),
- \Zend_Validate_Hostname::IP_ADDRESS_NOT_ALLOWED
- );
- $validator->setMessage(
- __("'%value%' looks like a DNS hostname but contains a dash in an invalid position."),
- \Zend_Validate_Hostname::INVALID_DASH
- );
- $validator->setMessage(
- __(
- "'%value%' looks like a DNS hostname but we cannot match it against "
- . "the hostname schema for TLD '%tld%'."
- ),
- \Zend_Validate_Hostname::INVALID_HOSTNAME_SCHEMA
- );
- $validator->setMessage(
- __("'%value%' looks like a DNS hostname but cannot extract TLD part."),
- \Zend_Validate_Hostname::UNDECIPHERABLE_TLD
- );
- $validator->setMessage(
- __("'%value%' does not look like a valid local network name."),
- \Zend_Validate_Hostname::INVALID_LOCAL_NAME
- );
- $validator->setMessage(
- __("'%value%' looks like a local network name, which is not an acceptable format."),
- \Zend_Validate_Hostname::LOCAL_NAME_NOT_ALLOWED
- );
- $validator->setMessage(
- __(
- "'%value%' appears to be a DNS hostname, but the given punycode notation cannot be decoded."
- ),
- \Zend_Validate_Hostname::CANNOT_DECODE_PUNYCODE
- );
- if (!$validator->isValid($value)) {
- return array_unique($validator->getMessages());
- }
- break;
- case 'url':
- $parsedUrl = parse_url($value);
- if ($parsedUrl === false || empty($parsedUrl['scheme']) || empty($parsedUrl['host'])) {
- return [__('"%1" is not a valid URL.', $label)];
- }
- $validator = new \Zend_Validate_Hostname();
- if (!$validator->isValid($parsedUrl['host'])) {
- return [__('"%1" is not a valid URL.', $label)];
- }
- break;
- case 'date':
- $validator = new \Zend_Validate_Date(\Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT);
- $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Date::INVALID);
- $validator->setMessage(__('"%1" is not a valid date.', $label), \Zend_Validate_Date::INVALID_DATE);
- $validator->setMessage(
- __('"%1" does not fit the entered date format.', $label),
- \Zend_Validate_Date::FALSEFORMAT
- );
- if (!$validator->isValid($value)) {
- return array_unique($validator->getMessages());
- }
- break;
- }
- }
- return true;
- }
- /**
- * Return is AJAX Request
- *
- * @return boolean
- * @SuppressWarnings(PHPMD.BooleanGetMethodName)
- */
- public function getIsAjaxRequest()
- {
- return $this->_isAjax;
- }
- /**
- * Return Original Attribute value from Request
- *
- * @param \Magento\Framework\App\RequestInterface $request
- * @return mixed
- */
- protected function _getRequestValue(\Magento\Framework\App\RequestInterface $request)
- {
- $attrCode = $this->getAttribute()->getAttributeCode();
- if ($this->_requestScope) {
- if (strpos($this->_requestScope, '/') !== false) {
- $params = $request->getParams();
- $parts = explode('/', $this->_requestScope);
- foreach ($parts as $part) {
- if (isset($params[$part])) {
- $params = $params[$part];
- } else {
- $params = [];
- }
- }
- } else {
- $params = $request->getParam($this->_requestScope);
- }
- if (isset($params[$attrCode])) {
- $value = $params[$attrCode];
- } else {
- $value = false;
- }
- if (!$this->_requestScopeOnly && $value === false) {
- $value = $request->getParam($attrCode, false);
- }
- } else {
- $value = $request->getParam($attrCode, false);
- }
- return $value;
- }
- /**
- * Extract data from request and return value
- *
- * @param \Magento\Framework\App\RequestInterface $request
- * @return array|string
- */
- abstract public function extractValue(\Magento\Framework\App\RequestInterface $request);
- /**
- * Validate data
- *
- * @param array|string|null $value
- * @return array|bool
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- abstract public function validateValue($value);
- /**
- * Export attribute value
- *
- * @param array|string $value
- * @return array|string|bool
- */
- abstract public function compactValue($value);
- /**
- * Restore attribute value from SESSION
- *
- * @param array|string $value
- * @return array|string|bool
- */
- abstract public function restoreValue($value);
- /**
- * Return formatted attribute value from entity model
- *
- * @param string $format
- * @return string|array
- */
- abstract public function outputValue(
- $format = \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_TEXT
- );
- }
|