<commit_before><?php

namespace YesWiki\Bazar\Field;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use YesWiki\Bazar\Service\EntryManager;
use YesWiki\Bazar\Service\ExternalBazarService;
use YesWiki\Bazar\Service\ListManager;
use YesWiki\Wiki;

abstract class EnumField extends BazarField
{
    protected $options;
    protected $optionsUrls; // only for loadOptionsFromJson

    protected $listLabel; // Allows to differentiate two enums using the same list
    protected $keywords;
    protected $queries;

    protected const FIELD_LIST_LABEL = 6;
    protected const FIELD_KEYWORDS = 13;
    protected const FIELD_QUERIES = 15;

    public function __construct(array $values, ContainerInterface $services)
    {
        parent::__construct($values, $services);

        $this->listLabel = $values[self::FIELD_LIST_LABEL];
        $this->keywords = $values[self::FIELD_KEYWORDS];
        $this->queries = $values[self::FIELD_QUERIES];

        $this->options = [];
        $this->optionsUrls = [];

        $this->propertyName = $this->type . $this->name . $this->listLabel;
    }

    public function loadOptionsFromList()
    {
        if (!empty($this->getLinkedObjectName())) {
            $listValues = $this->getService(ListManager::class)->getOne($this->getLinkedObjectName());
            if (is_array($listValues)) {
                $this->options = $listValues['label'];
            }
        }
    }

    public function loadOptionsFromJson()
    {
        $params = $this->getService(ParameterBagInterface::class);
        $refreshCacheDuration = ($params->has('baz_enum_field_time_cache_for_json'))
            ? $params->get('baz_enum_field_time_cache_for_json')
            : 7200 ; // 2 hours by default
        if ((($_GET['refresh'] ?? false) === 'true') && $this->getService(Wiki::class)->UserIsAdmin()) {
            $refreshCacheDuration = 0;
        }
        $json = $this->getService(ExternalBazarService::class)->getJSONCachedUrlContent($this->getLinkedObjectName(), $refreshCacheDuration);
        $entries = json_decode($json, true);
        $options = [];
        $this->optionsUrls = [];
        foreach ($entries as $id => $entry) {
            if (!empty($entry['bf_titre'])) {
                $options[$id] = $entry['bf_titre'];
            }
            if (!empty($entry['url'])) {
                $this->optionsUrls[$id] = $entry['url'];
            }<commit_msg>fix(EnumField): remove warnings when retrieving data from external site<commit_after><?php

namespace YesWiki\Bazar\Field;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use YesWiki\Bazar\Service\EntryManager;
use YesWiki\Bazar\Service\ExternalBazarService;
use YesWiki\Bazar\Service\ListManager;
use YesWiki\Wiki;

abstract class EnumField extends BazarField
{
    protected $options;
    protected $optionsUrls; // only for loadOptionsFromJson

    protected $listLabel; // Allows to differentiate two enums using the same list
    protected $keywords;
    protected $queries;

    protected const FIELD_LIST_LABEL = 6;
    protected const FIELD_KEYWORDS = 13;
    protected const FIELD_QUERIES = 15;

    public function __construct(array $values, ContainerInterface $services)
    {
        parent::__construct($values, $services);

        $this->listLabel = $values[self::FIELD_LIST_LABEL];
        $this->keywords = $values[self::FIELD_KEYWORDS];
        $this->queries = $values[self::FIELD_QUERIES];

        $this->options = [];
        $this->optionsUrls = [];

        $this->propertyName = $this->type . $this->name . $this->listLabel;
    }

    public function loadOptionsFromList()
    {
        if (!empty($this->getLinkedObjectName())) {
            $listValues = $this->getService(ListManager::class)->getOne($this->getLinkedObjectName());
            if (is_array($listValues)) {
                $this->options = $listValues['label'];
            }
        }
    }

    public function loadOptionsFromJson()
    {
        $params = $this->getService(ParameterBagInterface::class);
        $refreshCacheDuration = ($params->has('baz_enum_field_time_cache_for_json'))
            ? $params->get('baz_enum_field_time_cache_for_json')
            : 7200 ; // 2 hours by default
        if ((($_GET['refresh'] ?? false) === 'true') && $this->getService(Wiki::class)->UserIsAdmin()) {
            $refreshCacheDuration = 0;
        }
        $json = $this->getService(ExternalBazarService::class)->getJSONCachedUrlContent($this->getLinkedObjectName(), $refreshCacheDuration);
        $entries = json_decode($json, true);
        $options = [];
        $this->optionsUrls = [];
        if (is_array($entries)) {
            foreach ($entries as $id => $entry) {
                if (!empty($entry['bf_titre'])) {
                    $options[$id] = $entry['bf_titre'];
                }
                if (!empty($entry['url'])) {
                    $this->optionsUrls[$id] = $entry['url'];
                }
            }
{'fraction_non_alphanumeric': 0.13824340291453327, 'fraction_numerical': 0.004332414336352895, 'mean_word_length': 2.93415956622773, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, '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': '2115216', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1435, 'n_words': 262}