<commit_before><?php

declare(strict_types=1);

namespace App\Normalizer;

use App\Service\EntityMetadata;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use ReflectionClass;
use ReflectionProperty;
use DateTime;

class JsonApiDTO implements NormalizerInterface
{

    /**
     * @var EntityMetadata
     */
    protected $entityMetadata;

    public function __construct(
        EntityMetadata $entityMetadata
    ) {
        $this->entityMetadata = $entityMetadata;
    }

    public function normalize($object, string $format = null, array $context = [])
    {
        $reflection = new ReflectionClass($object);
        $exposedProperties = $this->entityMetadata->extractExposedProperties($reflection);
        $attributes = [];
        foreach ($exposedProperties as $property) {
            $attributes[$property->name] = $this->getPropertyValue($property, $object);
        }

        $relatedProperties = $this->entityMetadata->extractRelated($reflection);

        $type = $this->entityMetadata->extractType($reflection);
        $idProperty = $this->entityMetadata->extractId($reflection);
        $id = $attributes[$idProperty];
        unset($attributes[$idProperty]);

        $related = [];
        foreach ($relatedProperties as $attributeName => $relationshipType) {
            $value = $attributes[$attributeName];
            if ($value) {
                $related[$attributeName] = [
                    'type' => $relationshipType,
                    'value' => $value,
                ];
            }

            unset($attributes[$attributeName]);
        }

        return [
            'id' => $id,
            'type' => $type,
            'attributes' => $attributes,
            'related' => $related,
        ];
    }

    protected function getPropertyValue(ReflectionProperty $property, object $object)<commit_msg>Don't unset ID until relationships have been read<commit_after><?php

declare(strict_types=1);

namespace App\Normalizer;

use App\Service\EntityMetadata;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use ReflectionClass;
use ReflectionProperty;
use DateTime;

class JsonApiDTO implements NormalizerInterface
{

    /**
     * @var EntityMetadata
     */
    protected $entityMetadata;

    public function __construct(
        EntityMetadata $entityMetadata
    ) {
        $this->entityMetadata = $entityMetadata;
    }

    public function normalize($object, string $format = null, array $context = [])
    {
        $reflection = new ReflectionClass($object);
        $exposedProperties = $this->entityMetadata->extractExposedProperties($reflection);
        $attributes = [];
        foreach ($exposedProperties as $property) {
            $attributes[$property->name] = $this->getPropertyValue($property, $object);
        }

        $relatedProperties = $this->entityMetadata->extractRelated($reflection);

        $type = $this->entityMetadata->extractType($reflection);

        $idProperty = $this->entityMetadata->extractId($reflection);
        $id = $attributes[$idProperty];

        $related = [];
        foreach ($relatedProperties as $attributeName => $relationshipType) {
            $value = $attributes[$attributeName];
            if ($value) {
                $related[$attributeName] = [
                    'type' => $relationshipType,
                    'value' => $value,
                ];
            }

            unset($attributes[$attributeName]);
        }
        unset($attributes[$idProperty]);

        return [
            'id' => $id,
            'type' => $type,
            'attributes' => $attributes,
            'related' => $related,
        ];
    }

    protected function getPropertyValue(ReflectionProperty $property, object $object)
{'fraction_non_alphanumeric': 0.1233749005041125, 'fraction_numerical': 0.0005306447333510214, 'mean_word_length': 2.54656632173095, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, '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': '5805701', 'n_tokens_mistral': 959, 'n_tokens_neox': 926, 'n_words': 186}