<commit_before><?php

namespace Tarantool\Mapper\Plugins;

use Tarantool\Mapper\Entity;
use Tarantool\Mapper\Plugin;
use Tarantool\Mapper\Space;

class Spy extends Plugin
{
    private $create = [];
    private $update = [];
    private $remove = [];

    public function beforeCreate(Entity $instance, Space $space)
    {
        $this->create[$this->getKey($instance, $space)] = $instance;
    }

    public function beforeUpdate(Entity $instance, Space $space)
    {
        $key = $this->getKey($instance, $space);
        if(!array_key_exists($key, $this->create)) {
            $this->update[$key] = $instance;
        }
    }

    public function beforeRemove(Entity $instance, Space $space)
    {
        $key = $this->getKey($instance, $space);

        if(array_key_exists($key, $this->create)) {
            unset($this->create[$key]);
            return;
        }

        if(array_key_exists($key, $this->update)) {
            unset($this->update[$key]);
        }

        $this->remove[$key] = $instance;
    }

    public function reset()
    {
        $this->create = [];
        $this->update = [];
        $this->remove = [];
    }

    private function getKey(Entity $instance, Space $space)
    {
        $key = [$space->getName()];

        $format = $space->getFormat();
        foreach($space->getPrimaryIndex()->parts as $part) {
            $key[] = $instance->{$format[$part[0]]['name']};
        }

        return implode(':', $key);
    }

    public function getChanges()
    {
        $result = (object) [];

        foreach(['create', 'update', 'remove'] as $action) {
            $data = [];
            foreach($this->$action as $key => $row) {
                list($space) = explode(':', $key);
                if(!array_key_exists($space, $data)) {
                    $data[$space][] = $row;
                }
            }
            $result->$action = $data;
        }

        return $result;
    }

    public function hasChanges()
    {
        return count($this->create) + count($this->update) + count($this->remove) > 0;
    }
}<commit_msg>spy changes fix<commit_after><?php

namespace Tarantool\Mapper\Plugins;

use Tarantool\Mapper\Entity;
use Tarantool\Mapper\Plugin;
use Tarantool\Mapper\Space;

class Spy extends Plugin
{
    private $create = [];
    private $update = [];
    private $remove = [];

    public function beforeCreate(Entity $instance, Space $space)
    {
        $this->create[$this->getKey($instance, $space)] = $instance;
    }

    public function beforeUpdate(Entity $instance, Space $space)
    {
        $key = $this->getKey($instance, $space);
        if(!array_key_exists($key, $this->create)) {
            $this->update[$key] = $instance;
        }
    }

    public function beforeRemove(Entity $instance, Space $space)
    {
        $key = $this->getKey($instance, $space);

        if(array_key_exists($key, $this->create)) {
            unset($this->create[$key]);
            return;
        }

        if(array_key_exists($key, $this->update)) {
            unset($this->update[$key]);
        }

        $this->remove[$key] = $instance;
    }

    public function reset()
    {
        $this->create = [];
        $this->update = [];
        $this->remove = [];
    }

    private function getKey(Entity $instance, Space $space)
    {
        $key = [$space->getName()];

        $format = $space->getFormat();
        foreach($space->getPrimaryIndex()->parts as $part) {
            $key[] = $instance->{$format[$part[0]]['name']};
        }

        return implode(':', $key);
    }

    public function getChanges()
    {
        $result = (object) [];

        foreach(['create', 'update', 'remove'] as $action) {
            $data = [];
            foreach($this->$action as $key => $row) {
                list($space) = explode(':', $key);
                if(!array_key_exists($space, $data)) {
                    $data[$space] = [];
                }
                $data[$space][] = $row;
            }
            $result->$action = $data;
        }

        return $result;
    }

    public function hasChanges()
    {
        return count($this->create) + count($this->update) + count($this->remove) > 0;
    }
}
{'fraction_non_alphanumeric': 0.17275747508305647, 'fraction_numerical': 0.0009492168960607499, 'mean_word_length': 2.0411255411255413, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, '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': '2715271', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1235, 'n_words': 237}