<commit_before><?php

/*
 * This file is part of WordPlate.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace WordPlate\Acf;

use InvalidArgumentException;

/**
 * This is the field class.
 *
 * @author <NAME> <<EMAIL>>
 */
class Field
{
    /**
     * The field key.
     *
     * @var string
     */
    protected $key;

    /**
     * The field parent key.
     *
     * @var string
     */
    protected $parentKey;

    /**
     * The settings array.
     *
     * @var array
     */
    protected $settings;

    /**
     * Create a new field instance.
     *
     * @param array $settings
     * @param array $keys
     *
     * @return void
     */
    public function __construct(array $settings, array $keys = [])
    {
        $keys = array_merge(['label', 'type'], $keys);

        foreach ($keys as $key) {
            if (!array_key_exists($key, $settings)) {
                throw new InvalidArgumentException("Missing field setting key [$key].");
            }
        }

        $this->settings = $settings;
    }

    /**
     * Get the field key.
     *
     * @return string
     */
    public function getKey(): string
    {
        if ($this->key) {
            return $this->key;
        }

        $name = str_replace('-', '_', sanitize_title($this->settings['name']));

        $key = sprintf('%s_%s', $this->parentKey, $name);

        $this->key = $key;

        return $key;
    }

    /**
     * Get the field type.
     *
     * @return string
     */
    public function getType(): string
    {
        return $this->settings['type'];
    }

    /**
     * Set the field parent key.
     *
     * @param string $parentKey
     *
     * @return void
     */
    public function setParentKey(string $parentKey): void
    {
        $this->parentKey = $parentKey;
    }

    /**
     * Get the conditional logic.
     *
     * @return array
     */
    public function getConditionalLogic(): array
    {
        $conditionals = [];

        foreach ($this->settings['conditional_logic'] as $rules) {
            $conditional = new Conditional($rules, $this->parentKey);

            $conditionals[] = $conditional->toArray();
        }

        return $conditionals;
    }

    /**
     * Get the flexible content layouts.
     *
     * @return array
     */
    public function getLayouts(): array
    {
        $layouts = [];

        foreach ($this->settings['layouts'] as $layout) {
            $layout->setParentKey($this->getKey());

            $layouts[] = $layout->toArray();
        }

        return $layouts;
    }

    /**
     * Get the sub fields.
     *
     * @return array
     */<commit_msg>Fix conditional logic issue<commit_after><?php

/*
 * This file is part of WordPlate.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace WordPlate\Acf;

use InvalidArgumentException;

/**
 * This is the field class.
 *
 * @author <NAME> <<EMAIL>>
 */
class Field
{
    /**
     * The field key.
     *
     * @var string
     */
    protected $key;

    /**
     * The field parent key.
     *
     * @var string
     */
    protected $parentKey;

    /**
     * The settings array.
     *
     * @var array
     */
    protected $settings;

    /**
     * Create a new field instance.
     *
     * @param array $settings
     * @param array $keys
     *
     * @return void
     */
    public function __construct(array $settings, array $keys = [])
    {
        $keys = array_merge(['label', 'type'], $keys);

        foreach ($keys as $key) {
            if (!array_key_exists($key, $settings)) {
                throw new InvalidArgumentException("Missing field setting key [$key].");
            }
        }

        $this->settings = $settings;
    }

    /**
     * Get the field key.
     *
     * @return string
     */
    public function getKey(): string
    {
        if ($this->key) {
            return $this->key;
        }

        $name = str_replace('-', '_', sanitize_title($this->settings['name']));

        $key = sprintf('%s_%s', $this->parentKey, $name);

        $this->key = $key;

        return $key;
    }

    /**
     * Get the field type.
     *
     * @return string
     */
    public function getType(): string
    {
        return $this->settings['type'];
    }

    /**
     * Set the field parent key.
     *
     * @param string $parentKey
     *
     * @return void
     */
    public function setParentKey(string $parentKey): void
    {
        $this->parentKey = $parentKey;
    }

    /**
     * Get the conditional logic.
     *
     * @return array
     */
    public function getConditionalLogic(): array
    {
        $conditionals = [];

        foreach ($this->settings['conditional_logic'] as $rules) {
            $conditional = new Conditional($rules, $this->getKey());

            $conditionals[] = $conditional->toArray();
        }

        return $conditionals;
    }

    /**
     * Get the flexible content layouts.
     *
     * @return array
     */
    public function getLayouts(): array
    {
        $layouts = [];

        foreach ($this->settings['layouts'] as $layout) {
            $layout->setParentKey($this->getKey());

            $layouts[] = $layout->toArray();
        }

        return $layouts;
    }

    /**
     * Get the sub fields.
     *
     * @return array
     */
{'fraction_non_alphanumeric': 0.14221744581766074, 'fraction_numerical': 0.0003582303421099767, 'mean_word_length': 1.7426326129666012, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1145257', 'n_tokens_mistral': 1686, 'n_tokens_neox': 1627, 'n_words': 421}