<commit_before><?php

namespace Markup\NeedleBundle\Indexer;

use Markup\NeedleBundle\Exception\IllegalSubjectException;
use Solarium\QueryType\Update\Query\Query as UpdateQuery;

/**
* An object that can generate a Solarium document for a subject.
*/
class SubjectDocumentGenerator implements SubjectDocumentGeneratorInterface
{
    /**
     * @var SubjectDataMapperInterface
     **/
    private $subjectToDataMapper;

    /**
     * @var bool
     */
    private $allowNullValues;

    /**
     * @var UpdateQuery
     **/
    private $updateQuery = null;

    /**
     * @param SubjectDataMapperInterface $subjectToDataMapper
     * @param bool                       $allowNullValues
     **/
    public function __construct(SubjectDataMapperInterface $subjectToDataMapper, $allowNullValues = true)
    {
        $this->subjectToDataMapper = $subjectToDataMapper;
        $this->allowNullValues = $allowNullValues;
    }

    public function createDocumentForSubject($subject)
    {
        try {
            $data = $this->getSubjectToDataMapper()->mapSubjectToData($subject);
        } catch (IllegalSubjectException $e) {
            return null;
        }
        if (!$this->allowNullValues) {
            //pretty much forced to do this manually. thanks for nothing, php.
            $filteredData = [];
            foreach ($data as $key => $value) {
                if ($value !== null) {
                    $filteredData[$key] = $value;
                }
            }
            $data = $filteredData;
        }

        return $this->getUpdateQuery()->createDocument($data);
    }<commit_msg>only manually filter document data for null values if it contains them<commit_after><?php

namespace Markup\NeedleBundle\Indexer;

use Markup\NeedleBundle\Exception\IllegalSubjectException;
use Solarium\QueryType\Update\Query\Query as UpdateQuery;

/**
* An object that can generate a Solarium document for a subject.
*/
class SubjectDocumentGenerator implements SubjectDocumentGeneratorInterface
{
    /**
     * @var SubjectDataMapperInterface
     **/
    private $subjectToDataMapper;

    /**
     * @var bool
     */
    private $allowNullValues;

    /**
     * @var UpdateQuery
     **/
    private $updateQuery = null;

    /**
     * @param SubjectDataMapperInterface $subjectToDataMapper
     * @param bool                       $allowNullValues
     **/
    public function __construct(SubjectDataMapperInterface $subjectToDataMapper, $allowNullValues = true)
    {
        $this->subjectToDataMapper = $subjectToDataMapper;
        $this->allowNullValues = $allowNullValues;
    }

    public function createDocumentForSubject($subject)
    {
        try {
            $data = $this->getSubjectToDataMapper()->mapSubjectToData($subject);
        } catch (IllegalSubjectException $e) {
            return null;
        }
        if (!$this->allowNullValues && false !== array_search(null, $data, true)) {
            //pretty much forced to do this manually. thanks for nothing, php.
            $filteredData = [];
            foreach ($data as $key => $value) {
                if ($value !== null) {
                    $filteredData[$key] = $value;
                }
            }
            $data = $filteredData;
        }

        return $this->getUpdateQuery()->createDocument($data);
    }
{'fraction_non_alphanumeric': 0.10542168674698796, 'fraction_numerical': 0.0, 'mean_word_length': 2.321, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '3729231', 'n_tokens_mistral': 879, 'n_tokens_neox': 853, 'n_words': 199}