<commit_before><?php

namespace Tequila\MongoDB\ODM\Metadata\Field;

use Tequila\MongoDB\ODM\Code\PropertyGenerator;
use Tequila\MongoDB\ODM\Code\DocumentGenerator;
use Tequila\MongoDB\ODM\DocumentInterface;
use Tequila\MongoDB\ODM\Exception\InvalidArgumentException;
use Tequila\MongoDB\ODM\Proxy\AbstractCollection;
use Tequila\MongoDB\ODM\Proxy\Generator\AbstractGenerator;
use Tequila\MongoDB\ODM\Util\StringUtil;
use Zend\Code\Generator\MethodGenerator;
use Zend\Code\Generator\ParameterGenerator;
use Zend\Code\Generator\PropertyValueGenerator;

class CollectionField extends AbstractFieldMetadata
{
    /**
     * @var FieldMetadataInterface
     */
    private $itemMetadata;

    /**
     * @param FieldMetadataInterface $itemMetadata
     * @param string                 $propertyName
     * @param string|null            $dbFieldName
     */
    public function __construct(FieldMetadataInterface $itemMetadata, string $propertyName, string $dbFieldName = null)
    {
        $this->itemMetadata = $itemMetadata;

        parent::__construct($propertyName, $dbFieldName);
    }

    public function generateDocument(DocumentGenerator $documentGenerator)
    {
        if ($this->itemMetadata instanceof DocumentField) {
            $documentGenerator->addUse($this->itemMetadata->getDocumentClass());
        }

        $camelizedItemPropertyName = StringUtil::camelize($this->itemMetadata->getPropertyName());
        $adderParam = new ParameterGenerator(lcfirst($camelizedItemPropertyName));
        $adderParam->setType($this->itemMetadata->getType());

        $adder = new MethodGenerator('add'.$camelizedItemPropertyName);
        $adder->setParameter($adderParam);
        $adder->setBody(
            sprintf('$this->%s[] = $%s;', $this->getPropertyName(), $adderParam->getName())
        );

        $remover = new MethodGenerator('remove'.$camelizedItemPropertyName);
        $removerParam = new ParameterGenerator(lcfirst($camelizedItemPropertyName).'ToRemove');
        $remover->setParameter($removerParam);
        if ($this->itemMetadata instanceof DocumentField) {
            $removerBody = <<<'EOT'
foreach ($this->{{property}} as $key => ${{item}}) {
    $hasSameId = null !== ${{param}} && ${{param}} === ${{item}}->getMongoId();
    if (${{param}} === ${{item}} || $hasSameId) {
        $this->{{property}}[$key] = null;
    }
}
EOT;
        } else {
            $removerBody = <<<'EOT'
foreach ($this->{{property}} as $key => ${{item}}) {
    if (${{param}} === ${{item}}) {
        $this->{{property}}[$key] = null;
    }<commit_msg>TODO for CollectionField<commit_after><?php

namespace Tequila\MongoDB\ODM\Metadata\Field;

use Tequila\MongoDB\ODM\Code\PropertyGenerator;
use Tequila\MongoDB\ODM\Code\DocumentGenerator;
use Tequila\MongoDB\ODM\DocumentInterface;
use Tequila\MongoDB\ODM\Exception\InvalidArgumentException;
use Tequila\MongoDB\ODM\Proxy\AbstractCollection;
use Tequila\MongoDB\ODM\Proxy\Generator\AbstractGenerator;
use Tequila\MongoDB\ODM\Util\StringUtil;
use Zend\Code\Generator\MethodGenerator;
use Zend\Code\Generator\ParameterGenerator;
use Zend\Code\Generator\PropertyValueGenerator;

class CollectionField extends AbstractFieldMetadata
{
    /**
     * @var FieldMetadataInterface
     */
    private $itemMetadata;

    /**
     * @param FieldMetadataInterface $itemMetadata
     * @param string                 $propertyName
     * @param string|null            $dbFieldName
     */
    public function __construct(FieldMetadataInterface $itemMetadata, string $propertyName, string $dbFieldName = null)
    {
        $this->itemMetadata = $itemMetadata;

        parent::__construct($propertyName, $dbFieldName);
    }

    public function generateDocument(DocumentGenerator $documentGenerator)
    {
        if ($this->itemMetadata instanceof DocumentField) {
            $documentGenerator->addUse($this->itemMetadata->getDocumentClass());
        }

        $camelizedItemPropertyName = StringUtil::camelize($this->itemMetadata->getPropertyName());
        $adderParam = new ParameterGenerator(lcfirst($camelizedItemPropertyName));
        $adderParam->setType($this->itemMetadata->getType());

        // TODO refactor adders and removers functionality:
        // We should use "collections" for consistent updates in documents and proxies
        $adder = new MethodGenerator('add'.$camelizedItemPropertyName);
        $adder->setParameter($adderParam);
        $adder->setBody(
            sprintf('$this->%s[] = $%s;', $this->getPropertyName(), $adderParam->getName())
        );

        $remover = new MethodGenerator('remove'.$camelizedItemPropertyName);
        $removerParam = new ParameterGenerator(lcfirst($camelizedItemPropertyName).'ToRemove');
        $remover->setParameter($removerParam);
        if ($this->itemMetadata instanceof DocumentField) {
            $removerBody = <<<'EOT'
foreach ($this->{{property}} as $key => ${{item}}) {
    $hasSameId = null !== ${{param}} && ${{param}} === ${{item}}->getMongoId();
    if (${{param}} === ${{item}} || $hasSameId) {
        $this->{{property}}[$key] = null;
    }
}
EOT;
        } else {
            $removerBody = <<<'EOT'
foreach ($this->{{property}} as $key => ${{item}}) {
    if (${{param}} === ${{item}}) {
        $this->{{property}}[$key] = null;
    }
{'fraction_non_alphanumeric': 0.14909297052154194, 'fraction_numerical': 0.0, 'mean_word_length': 3.6757950530035335, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 51, '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': '2873992', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1502, 'n_words': 259}