Field.php 2.01 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Config\Model\Config\Structure\Element\Dependency;

/**
 * @api
 * @since 100.0.2
 */
class Field
{
    /**
     * Values for dependence
     *
     * @var string[]
     */
    protected $_values;

    /**
     * Id of the dependent field
     *
     * @var string
     */
    protected $_id;

    /**
     * Whether dependence is for negative comparison
     *
     * @var bool
     */
    protected $_isNegative = false;

    /**
     * @param array $fieldData
     * @param string $fieldPrefix
     */
    public function __construct(array $fieldData = [], $fieldPrefix = "")
    {
        if (isset($fieldData['separator'])) {
            $this->_values = explode($fieldData['separator'], $fieldData['value']);
        } else {
            $this->_values = [$fieldData['value']];
        }
        $fieldId = $fieldPrefix . (isset(
            $fieldData['dependPath']
        ) && is_array(
            $fieldData['dependPath']
        ) ? array_pop(
            $fieldData['dependPath']
        ) : '');
        $fieldData['dependPath'][] = $fieldId;
        $this->_id = implode('_', $fieldData['dependPath']);
        $this->_isNegative = isset($fieldData['negative']) && $fieldData['negative'];
    }

    /**
     * Check whether the value satisfy dependency
     *
     * @param string $value
     * @return bool
     */
    public function isValueSatisfy($value)
    {
        return in_array($value, $this->_values) xor $this->_isNegative;
    }

    /**
     * Get id of the dependent field
     *
     * @return string
     */
    public function getId()
    {
        return $this->_id;
    }

    /**
     * Get values for dependence
     *
     * @return string[]
     */
    public function getValues()
    {
        return $this->_values;
    }

    /**
     * Get negative indication of dependency
     *
     * @return bool
     */
    public function isNegative()
    {
        return $this->_isNegative;
    }
}