UrlRewrite.php 5.35 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\UrlRewrite\Service\V1\Data;

use Magento\Framework\Api\AbstractSimpleObject;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\Serializer\Json;

/**
 * Data abstract class for url storage
 * @api
 * @since 100.0.2
 */
class UrlRewrite extends AbstractSimpleObject
{
    /**#@+
     * Value object attribute names
     */
    const URL_REWRITE_ID = 'url_rewrite_id';
    const ENTITY_ID = 'entity_id';
    const ENTITY_TYPE = 'entity_type';
    const IS_AUTOGENERATED = 'is_autogenerated';
    const REQUEST_PATH = 'request_path';
    const TARGET_PATH = 'target_path';
    const STORE_ID = 'store_id';
    const REDIRECT_TYPE = 'redirect_type';
    const DESCRIPTION = 'description';
    const METADATA = 'metadata';
    /**#@-*/

    /**#@-*/
    protected $defaultValues = [
        self::REDIRECT_TYPE => 0,
        self::IS_AUTOGENERATED => 1,
        self::METADATA => null,
        self::DESCRIPTION => null,
    ];

    /**
     * @var Json
     */
    private $serializer;

    /**
     * UrlRewrite constructor.
     *
     * @param array $data
     * @param Json $serializer
     */
    public function __construct(
        $data = [],
        Json $serializer = null
    ) {
        $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
        parent::__construct($data);
    }

    /**
     * Get data by key
     *
     * @param string $key
     * @return mixed|null
     */
    public function getByKey($key)
    {
        return $this->_get($key);
    }

    /**
     * @return int
     */
    public function getUrlRewriteId()
    {
        return $this->_get(self::URL_REWRITE_ID);
    }

    /**
     * @param int $urlRewriteId
     * @return $this
     */
    public function setUrlRewriteId($urlRewriteId)
    {
        return $this->setData(self::URL_REWRITE_ID, $urlRewriteId);
    }

    /**
     * @return int
     */
    public function getEntityId()
    {
        return $this->_get(self::ENTITY_ID);
    }

    /**
     * @param int $entityId
     *
     * @return $this
     */
    public function setEntityId($entityId)
    {
        return $this->setData(self::ENTITY_ID, $entityId);
    }

    /**
     * @return string
     */
    public function getEntityType()
    {
        return $this->_get(self::ENTITY_TYPE);
    }

    /**
     * @param string $entityType
     *
     * @return $this
     */
    public function setEntityType($entityType)
    {
        return $this->setData(self::ENTITY_TYPE, $entityType);
    }

    /**
     * @return int
     */
    public function getIsAutogenerated()
    {
        return $this->_get(self::IS_AUTOGENERATED) === null ?
            $this->defaultValues[self::IS_AUTOGENERATED] : $this->_get(self::IS_AUTOGENERATED);
    }

    /**
     * @param int $isAutogenerated
     *
     * @return $this
     */
    public function setIsAutogenerated($isAutogenerated)
    {
        return $this->setData(self::IS_AUTOGENERATED, $isAutogenerated);
    }

    /**
     * @return string
     */
    public function getRequestPath()
    {
        return $this->_get(self::REQUEST_PATH);
    }

    /**
     * @param string $requestPath
     *
     * @return $this
     */
    public function setRequestPath($requestPath)
    {
        return $this->setData(self::REQUEST_PATH, $requestPath);
    }

    /**
     * @return string
     */
    public function getTargetPath()
    {
        return $this->_get(self::TARGET_PATH);
    }

    /**
     * @param string $targetPath
     *
     * @return $this
     */
    public function setTargetPath($targetPath)
    {
        return $this->setData(self::TARGET_PATH, $targetPath);
    }

    /**
     * @return int
     */
    public function getStoreId()
    {
        return $this->_get(self::STORE_ID);
    }

    /**
     * @param int $storeId
     *
     * @return $this
     */
    public function setStoreId($storeId)
    {
        return $this->setData(self::STORE_ID, $storeId);
    }

    /**
     * @return int
     */
    public function getRedirectType()
    {
        return (int)$this->_get(self::REDIRECT_TYPE);
    }

    /**
     * @param int $redirectCode
     *
     * @return $this
     */
    public function setRedirectType($redirectCode)
    {
        return $this->setData(self::REDIRECT_TYPE, $redirectCode);
    }

    /**
     * @return string
     */
    public function getDescription()
    {
        return $this->_get(self::DESCRIPTION);
    }

    /**
     * @param string $description
     *
     * @return $this
     */
    public function setDescription($description)
    {
        return $this->setData(self::DESCRIPTION, $description);
    }

    /**
     * @return array
     */
    public function getMetadata()
    {
        $metadata = $this->_get(self::METADATA);
        return !empty($metadata) ? $this->serializer->unserialize($metadata) : [];
    }

    /**
     * @param array|string $metadata
     *
     * @return $this
     */
    public function setMetadata($metadata)
    {
        if (is_array($metadata)) {
            $metadata = $this->serializer->serialize($metadata);
        }
        return $this->setData(UrlRewrite::METADATA, $metadata);
    }

    /**
     * Convert UrlRewrite to array
     *
     * @return array
     */
    public function toArray()
    {
        return array_merge($this->defaultValues, $this->_data);
    }
}