<commit_before><?php

namespace Tequila\MongoDB\ODM\Proxy\Generator;

use MongoDB\BSON\Unserializable;
use Tequila\MongoDB\ODM\Proxy\NestedProxyInterface;
use Tequila\MongoDB\ODM\Proxy\Traits\NestedProxyTrait;
use Zend\Code\Generator\MethodGenerator;

class NestedProxyGenerator extends AbstractGenerator
{
    protected function createBsonUnserializeMethod(): MethodGenerator
    {
        $method = new MethodGenerator('doBsonUnserialize');
        $methodBody = $this->buildUnserializationDefaultsCode();

        foreach ($this->metadata->getFieldsMetadata() as $field) {
            $code = strtr($field->getUnserializationCode($this), [
                '$objectData' => '$objectData[\''.$field->getPropertyName().'\']',
                '$dbData' => sprintf('$this->_mongoDbData[\'%s\']', $field->getDbFieldName()),
                '$pathInDocument' => sprintf('$this->getPathInDocument(\'%s\')', $field->getDbFieldName()),
                '$rootProxy' => '$this->getRootProxy()',
            ]);

            $methodBody .= PHP_EOL.$code;
        }

        $methodBody .= PHP_EOL.'parent::bsonUnserialize($objectData);';
        $methodBody .= PHP_EOL.'$this->_mongoDbData = null;';

        $method->setBody($methodBody);

        return $method;
    }

    protected function getInterfaces(): array
    {
        return [
            NestedProxyInterface::class,
            Unserializable::class,
        ];
    }

    protected function getTraits(): array
    {
        return [NestedProxyTrait::class];
    }

    private function buildUnserializationDefaultsCode(): string
    {
        $lines = [];
        foreach ($this->metadata->getFieldsMetadata() as $fieldMetadata) {
            $lines[] = "'{$fieldMetadata->getDbFieldName()}' => null,";
        }

        $code = <<<'EOT'
$defaults = [
%s
]; 
$this->_mongoDbData += $defaults;
$objectData = [];
EOT;

        return sprintf($code, implode("    \n", $lines));<commit_msg>Fix of nested proxy class being extend Unserializable twice<commit_after><?php

namespace Tequila\MongoDB\ODM\Proxy\Generator;

use Tequila\MongoDB\ODM\Proxy\NestedProxyInterface;
use Tequila\MongoDB\ODM\Proxy\Traits\NestedProxyTrait;
use Zend\Code\Generator\MethodGenerator;

class NestedProxyGenerator extends AbstractGenerator
{
    protected function createBsonUnserializeMethod(): MethodGenerator
    {
        $method = new MethodGenerator('doBsonUnserialize');
        $methodBody = $this->buildUnserializationDefaultsCode();

        foreach ($this->metadata->getFieldsMetadata() as $field) {
            $code = strtr($field->getUnserializationCode($this), [
                '$objectData' => '$objectData[\''.$field->getPropertyName().'\']',
                '$dbData' => sprintf('$this->_mongoDbData[\'%s\']', $field->getDbFieldName()),
                '$pathInDocument' => sprintf('$this->getPathInDocument(\'%s\')', $field->getDbFieldName()),
                '$rootProxy' => '$this->getRootProxy()',
            ]);

            $methodBody .= PHP_EOL.$code;
        }

        $methodBody .= PHP_EOL.'parent::bsonUnserialize($objectData);';
        $methodBody .= PHP_EOL.'$this->_mongoDbData = null;';

        $method->setBody($methodBody);

        return $method;
    }

    protected function getInterfaces(): array
    {
        return [
            NestedProxyInterface::class,
        ];
    }

    protected function getTraits(): array
    {
        return [NestedProxyTrait::class];
    }

    private function buildUnserializationDefaultsCode(): string
    {
        $lines = [];
        foreach ($this->metadata->getFieldsMetadata() as $fieldMetadata) {
            $lines[] = "'{$fieldMetadata->getDbFieldName()}' => null,";
        }

        $code = <<<'EOT'
$defaults = [
%s
]; 
$this->_mongoDbData += $defaults;
$objectData = [];
EOT;

        return sprintf($code, implode("    \n", $lines));
{'fraction_non_alphanumeric': 0.15675116399379202, 'fraction_numerical': 0.0, 'mean_word_length': 2.9060606060606062, 'pattern_counts': {'":': 0, '<': 11, '<?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': '7579755', 'n_tokens_mistral': 1148, 'n_tokens_neox': 1128, 'n_words': 180}