UrlRewrite.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\UrlRewrite\Service\V1\Data;
  7. use Magento\Framework\Api\AbstractSimpleObject;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Serialize\Serializer\Json;
  10. /**
  11. * Data abstract class for url storage
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class UrlRewrite extends AbstractSimpleObject
  16. {
  17. /**#@+
  18. * Value object attribute names
  19. */
  20. const URL_REWRITE_ID = 'url_rewrite_id';
  21. const ENTITY_ID = 'entity_id';
  22. const ENTITY_TYPE = 'entity_type';
  23. const IS_AUTOGENERATED = 'is_autogenerated';
  24. const REQUEST_PATH = 'request_path';
  25. const TARGET_PATH = 'target_path';
  26. const STORE_ID = 'store_id';
  27. const REDIRECT_TYPE = 'redirect_type';
  28. const DESCRIPTION = 'description';
  29. const METADATA = 'metadata';
  30. /**#@-*/
  31. /**#@-*/
  32. protected $defaultValues = [
  33. self::REDIRECT_TYPE => 0,
  34. self::IS_AUTOGENERATED => 1,
  35. self::METADATA => null,
  36. self::DESCRIPTION => null,
  37. ];
  38. /**
  39. * @var Json
  40. */
  41. private $serializer;
  42. /**
  43. * UrlRewrite constructor.
  44. *
  45. * @param array $data
  46. * @param Json $serializer
  47. */
  48. public function __construct(
  49. $data = [],
  50. Json $serializer = null
  51. ) {
  52. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
  53. parent::__construct($data);
  54. }
  55. /**
  56. * Get data by key
  57. *
  58. * @param string $key
  59. * @return mixed|null
  60. */
  61. public function getByKey($key)
  62. {
  63. return $this->_get($key);
  64. }
  65. /**
  66. * @return int
  67. */
  68. public function getUrlRewriteId()
  69. {
  70. return $this->_get(self::URL_REWRITE_ID);
  71. }
  72. /**
  73. * @param int $urlRewriteId
  74. * @return $this
  75. */
  76. public function setUrlRewriteId($urlRewriteId)
  77. {
  78. return $this->setData(self::URL_REWRITE_ID, $urlRewriteId);
  79. }
  80. /**
  81. * @return int
  82. */
  83. public function getEntityId()
  84. {
  85. return $this->_get(self::ENTITY_ID);
  86. }
  87. /**
  88. * @param int $entityId
  89. *
  90. * @return $this
  91. */
  92. public function setEntityId($entityId)
  93. {
  94. return $this->setData(self::ENTITY_ID, $entityId);
  95. }
  96. /**
  97. * @return string
  98. */
  99. public function getEntityType()
  100. {
  101. return $this->_get(self::ENTITY_TYPE);
  102. }
  103. /**
  104. * @param string $entityType
  105. *
  106. * @return $this
  107. */
  108. public function setEntityType($entityType)
  109. {
  110. return $this->setData(self::ENTITY_TYPE, $entityType);
  111. }
  112. /**
  113. * @return int
  114. */
  115. public function getIsAutogenerated()
  116. {
  117. return $this->_get(self::IS_AUTOGENERATED) === null ?
  118. $this->defaultValues[self::IS_AUTOGENERATED] : $this->_get(self::IS_AUTOGENERATED);
  119. }
  120. /**
  121. * @param int $isAutogenerated
  122. *
  123. * @return $this
  124. */
  125. public function setIsAutogenerated($isAutogenerated)
  126. {
  127. return $this->setData(self::IS_AUTOGENERATED, $isAutogenerated);
  128. }
  129. /**
  130. * @return string
  131. */
  132. public function getRequestPath()
  133. {
  134. return $this->_get(self::REQUEST_PATH);
  135. }
  136. /**
  137. * @param string $requestPath
  138. *
  139. * @return $this
  140. */
  141. public function setRequestPath($requestPath)
  142. {
  143. return $this->setData(self::REQUEST_PATH, $requestPath);
  144. }
  145. /**
  146. * @return string
  147. */
  148. public function getTargetPath()
  149. {
  150. return $this->_get(self::TARGET_PATH);
  151. }
  152. /**
  153. * @param string $targetPath
  154. *
  155. * @return $this
  156. */
  157. public function setTargetPath($targetPath)
  158. {
  159. return $this->setData(self::TARGET_PATH, $targetPath);
  160. }
  161. /**
  162. * @return int
  163. */
  164. public function getStoreId()
  165. {
  166. return $this->_get(self::STORE_ID);
  167. }
  168. /**
  169. * @param int $storeId
  170. *
  171. * @return $this
  172. */
  173. public function setStoreId($storeId)
  174. {
  175. return $this->setData(self::STORE_ID, $storeId);
  176. }
  177. /**
  178. * @return int
  179. */
  180. public function getRedirectType()
  181. {
  182. return (int)$this->_get(self::REDIRECT_TYPE);
  183. }
  184. /**
  185. * @param int $redirectCode
  186. *
  187. * @return $this
  188. */
  189. public function setRedirectType($redirectCode)
  190. {
  191. return $this->setData(self::REDIRECT_TYPE, $redirectCode);
  192. }
  193. /**
  194. * @return string
  195. */
  196. public function getDescription()
  197. {
  198. return $this->_get(self::DESCRIPTION);
  199. }
  200. /**
  201. * @param string $description
  202. *
  203. * @return $this
  204. */
  205. public function setDescription($description)
  206. {
  207. return $this->setData(self::DESCRIPTION, $description);
  208. }
  209. /**
  210. * @return array
  211. */
  212. public function getMetadata()
  213. {
  214. $metadata = $this->_get(self::METADATA);
  215. return !empty($metadata) ? $this->serializer->unserialize($metadata) : [];
  216. }
  217. /**
  218. * @param array|string $metadata
  219. *
  220. * @return $this
  221. */
  222. public function setMetadata($metadata)
  223. {
  224. if (is_array($metadata)) {
  225. $metadata = $this->serializer->serialize($metadata);
  226. }
  227. return $this->setData(UrlRewrite::METADATA, $metadata);
  228. }
  229. /**
  230. * Convert UrlRewrite to array
  231. *
  232. * @return array
  233. */
  234. public function toArray()
  235. {
  236. return array_merge($this->defaultValues, $this->_data);
  237. }
  238. }