<commit_before><?php

namespace Protechstudio\PrestashopWebService;

use PrestaShopWebservice as PSLibrary;
use SimpleXMLElement;

class PrestashopWebService extends PSLibrary
{

    /**
     * Retrieve the resource schema
     *
     * @param $resource
     * @return SimpleXMLElement
     * @throws \PrestaShopWebserviceException
     */
    public function getSchema($resource)
    {
        return $this->get(['resource' => $resource . '?schema=blank']);
    }

    /**
     * Fill the provided schema with an associative array data
     *
     * @param SimpleXMLElement $xmlSchema
     * @param array $data
     * @return SimpleXMLElement
     */
    public function fillSchema(SimpleXMLElement $xmlSchema, $data)
    {
        $toBeRemoved = array();
        $resource = $xmlSchema->children()->children();
        foreach ($resource as $key => $value) {
            if (array_key_exists($key, $data)) {
                if (property_exists($resource->$key, 'language')) {
                    $this->fillLanguageNode($resource->$key, $data[$key]);
                } else {
                    $resource->$key = $data[$key];
                }
            } else {
                $toBeRemoved[] = $key;
            }
        }
        foreach ($toBeRemoved as $key) {
            unset($resource->$key);
        }

        return $xmlSchema;
    }

    /**
     * @param string|array $data
     * @param $languageId
     * @return string
     */
    private function getLanguageValue($data, $languageId)
    {
        if (is_string($data)) {
            return $data;
        }

        if (array_key_exists($languageId, $data)) {
            return $data[$languageId];
        } else {
            return $data[1];
        }
    }

    /**
     * @param $node
     * @param $data
     */
    private function fillLanguageNode($node, $data)
    {
        for ($i = 0; $i < count($node->language); $i++) {
            $node->language[$i] = $this->getLanguageValue($data, (int)$node->language[$i]['id']->__toString());
        }
    }
}<commit_msg>Support for recursive nodes in fillSchema()<commit_after><?php

namespace Protechstudio\PrestashopWebService;

use PrestaShopWebservice as PSLibrary;
use SimpleXMLElement;

class PrestashopWebService extends PSLibrary
{

    /**
     * Retrieve the resource schema
     *
     * @param $resource
     * @return SimpleXMLElement
     * @throws \PrestaShopWebserviceException
     */
    public function getSchema($resource)
    {
        return $this->get(['resource' => $resource . '?schema=blank']);
    }

    /**
     * Fill the provided schema with an associative array data
     *
     * @param SimpleXMLElement $xmlSchema
     * @param array $data
     * @return SimpleXMLElement
     */
    public function fillSchema(SimpleXMLElement $xmlSchema, $data)
    {
        $resource = $xmlSchema->children()->children();
        foreach ($data as $key => $value) {
            $this->processNode($resource, $key, $value);
        }
        return $xmlSchema;
    }

    /**
     * @param string|array $data
     * @param $languageId
     * @return string
     */
    private function getLanguageValue($data, $languageId)
    {
        if (is_string($data)) {
            return $data;
        }

        if (array_key_exists($languageId, $data)) {
            return $data[$languageId];
        } else {
            return $data[1];
        }
    }

    /**
     * @param $node
     * @param $data
     */
    private function fillLanguageNode($node, $data)
    {
        for ($i = 0; $i < count($node->language); $i++) {
            $node->language[$i] = $this->getLanguageValue($data, (int)$node->language[$i]['id']->__toString());
        }
    }

    /**
     * @param SimpleXMLElement $node
     * @param $dataKey
     * @param $dataValue
     */
    private function processNode(SimpleXMLElement $node, $dataKey, $dataValue)
    {
        if (property_exists($node->$dataKey, 'language')) {
            $this->fillLanguageNode($node->$dataKey, $dataValue);
        } elseif (is_array($dataValue)) {
            foreach ($dataValue as $key => $value) {
                $this->processNode($node->$dataKey, $key, $value);
            }
        } else {
            $node->$dataKey = $dataValue;
        }
    }
}
{'fraction_non_alphanumeric': 0.13991094445746427, 'fraction_numerical': 0.0009374267635340989, 'mean_word_length': 2.0771449170872387, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '3369381', 'n_tokens_mistral': 1262, 'n_tokens_neox': 1240, 'n_words': 262}