EmicoEcommerce/Magento2Tweakwise

View on GitHub
Model/Catalog/Layer/Filter/Item.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

/**
 * Tweakwise (https://www.tweakwise.com/) - All Rights Reserved
 *
 * @copyright Copyright (c) 2017-2022 Tweakwise.com B.V. (https://www.tweakwise.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

namespace Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Filter;

use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Filter;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Url;
use Tweakwise\Magento2Tweakwise\Model\Client\Type\AttributeType;
use Tweakwise\Magento2Tweakwise\Model\Client\Type\FacetType\SettingsType;
use Magento\Catalog\Model\Layer\Filter\Item as MagentoItem;

/**
 * Class Item extends Magento\Catalog\Model\Layer\Filter\Item only for the type hint in Magento\Swatches\Block\LayeredNavigation\RenderLayered
 */
class Item extends MagentoItem
{
    /**
     * @var Filter
     */
    protected $filter;

    /**
     * @var AttributeType
     */
    protected $attributeType;

    /**
     * @var Url
     */
    protected $url;

    /**
     * @var Item[]
     */
    protected $children = [];

    /**
     * Item constructor.
     *
     * @param Filter $filter
     * @param AttributeType $attributeType
     * @param Url $url
     * @noinspection MagicMethodsValidityInspection
     * @noinspection PhpMissingParentConstructorInspection
     */
    public function __construct(Filter $filter, AttributeType $attributeType, Url $url)
    {
        $this->filter = $filter;
        $this->attributeType = $attributeType;
        $this->url = $url;
    }

    /**
     * @return void
     */
    public function __clone()
    {
        $this->attributeType = clone $this->attributeType;
    }

    /**
     * @return Filter
     */
    public function getFilter()
    {
        return $this->filter;
    }

    /**
     * @return string
     */
    public function getLabel()
    {
        return (string) $this->attributeType->getTitle();
    }

    /**
     * @return int
     */
    public function getCount()
    {
        return (int) $this->attributeType->getNumberOfResults();
    }

    /**
     * @return bool
     */
    public function isSelected()
    {
        return (bool) $this->attributeType->getIsSelected();
    }

    /**
     * @return int
     */
    public function getAlternateSortOrder()
    {
        return $this->attributeType->getAlternateSortOrder();
    }

    /**
     * Method is called for swatches and should return the original value instead of the label.
     * Used for the default swatch renderer
     *
     * @return int
     */
    public function getValue()
    {
        return $this->getFilter()->getOptionIdByLabel($this->getLabel());
    }

    /**
     * @return string
     */
    public function getUrl()
    {
        $settings = $this->getFilter()->getFacet()->getFacetSettings();
        if ($settings->getSelectionType() === SettingsType::SELECTION_TYPE_SLIDER) {
            return $this->url->getSliderUrl($this);
        }

        if ($this->isSelected() && ($settings->getSource() !== SettingsType::SOURCE_CATEGORY)) {
            return $this->url->getRemoveFilter($this);
        }

        return $this->url->getSelectFilter($this);
    }

    /**
     * Get url for remove item from filter
     *
     * @return string
     */
    public function getRemoveUrl()
    {
        return $this->url->getRemoveFilter($this);
    }

    /**
     * @return AttributeType
     */
    public function getAttribute()
    {
        return $this->attributeType;
    }

    /**
     * @param Item[] $children
     * @return $this
     */
    public function setChildren(array $children)
    {
        $this->children = $children;
        return $this;
    }

    /**
     * @return bool
     */
    public function hasChildren()
    {
        return count($this->children) > 0;
    }

    /**
     * @return Item[]
     */
    public function getChildren()
    {
        return $this->children;
    }

    /**
     * @return bool
     */
    public function isActive()
    {
        return $this->getAttribute()->getIsSelected();
    }

    /**
     * @return string
     */
    public function getCssId()
    {
        return spl_object_hash($this);
    }
}