<commit_before><?php

/*
 * Copyright 2011 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace JMS\SerializerBundle\Serializer;

use JMS\SerializerBundle\Serializer\Normalizer\NormalizableInterface;

use JMS\SerializerBundle\Exception\RuntimeException;
use JMS\SerializerBundle\Serializer\SerializerAwareInterface;
use JMS\SerializerBundle\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;

/**
 * Serializer implementation.
 *
 * This serializer distinuishes three different types of normalizers, one
 * normalizer for native php types, one default normalizer for objects, and an
 * arbitrary amount of specialized normalizers for specific object classes.
 *
 * @author <NAME> <<EMAIL>>
 */
class Serializer implements SerializerInterface
{
    private $nativePhpTypeNormalizer;
    private $customObjectNormalizers;
    private $defaultObjectNormalizer;
    private $encoderMap;

    public function __construct(NormalizerInterface $nativePhpNormalizer, NormalizerInterface $defaultObjectNormalizer, array $customObjectNormalizers = array(), array $encoderMap = array())
    {
        if ($nativePhpNormalizer instanceof SerializerAwareInterface) {
            $nativePhpNormalizer->setSerializer($this);
        }
        $this->nativePhpTypeNormalizer = $nativePhpNormalizer;

        if ($defaultObjectNormalizer instanceof SerializerAwareInterface) {
            $defaultObjectNormalizer->setSerializer($this);
        }
        $this->defaultObjectNormalizer = $defaultObjectNormalizer;

        foreach ($customObjectNormalizers as $normalizer) {
            if ($normalizer instanceof SerializerAwareInterface) {
                $normalizer->setSerializer($this);
            }
        }
        $this->customObjectNormalizers = $customObjectNormalizers;

        foreach ($encoderMap as $encoder) {
            if ($encoder instanceof SerializerAwareInterface) {
                $encoder->setSerializer($this);
            }
        }
        $this->encoderMap = $encoderMap;
    }

    /**
     * {@inheritDoc}
     */
    public final function normalize($data, $format = null)
    {
        if (is_object($data) && $this->customObjectNormalizers) {
            foreach ($this->customObjectNormalizers as $normalizer) {
                if ($normalizer->supportsNormalization($data, $format)) {
                    return $normalizer->normalize($data, $format);
                }
            }
        }

        if ($this->nativePhpTypeNormalizer->supportsNormalization($data, $format)) {
            return $this->nativePhpTypeNormalizer->normalize($data, $format);
        }

        return $this->defaultObjectNormalizer->normalize($data, $format);
    }

    /**
     * {@inheritDoc}
     */
    public final function denormalize($data, $type, $format = null)
    {
        if ($this->nativePhpTypeNormalizer->supportsDenormalization($data, $type, $format)) {
            return $this->nativePhpTypeNormalizer->denormalize($data, $type, $format);
        }

        if ($this->customObjectNormalizers) {
            foreach ($this->customObjectNormalizers as $normalizer) {
                if ($normalizer->supportsDenormalization($data, $type, $format)) {
                    return $normalizer->denormalize($data, $type, $format);
                }
            }
        }

        return $this->defaultObjectNormalizer->denormalize($data, $type, $format);
    }

    /**
     * {@inheritDoc}
     */
    public final function serialize($data, $format)
    {
        $data = $this->normalize($data, $format);

        return $this->encode($data, $format);
    }

    /**
     * {@inheritDoc}
     */
    public final function deserialize($data, $type, $format)
    {
        $data = $this->decode($data, $format);

        return $this->denormalize($data, $type, $format);
    }

    /**
     * {@inheritDoc}
     */
    public final function encode($data, $format)
    {
        return $this->getEncoder($format)->encode($data, $format);
    }

    /**
     * {@inheritDoc}
     */
    public final function decode($data, $format)
    {
        return $this->getEncoder($format)->decode($data, $format);
    }

    protected function getEncoder($format)
    {
        if (!isset($this->encoderMap[$format])) {
            throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
        }

        return $this->encoderMap[$format];
    }
}
<commit_msg>switch condition order, added small comment<commit_after><?php

/*
 * Copyright 2011 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace JMS\SerializerBundle\Serializer;

use JMS\SerializerBundle\Serializer\Normalizer\NormalizableInterface;

use JMS\SerializerBundle\Exception\RuntimeException;
use JMS\SerializerBundle\Serializer\SerializerAwareInterface;
use JMS\SerializerBundle\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;

/**
 * Serializer implementation.
 *
 * This serializer distinuishes three different types of normalizers, one
 * normalizer for native php types, one default normalizer for objects, and an
 * arbitrary amount of specialized normalizers for specific object classes.
 *
 * @author <NAME> <<EMAIL>>
 */
class Serializer implements SerializerInterface
{
    private $nativePhpTypeNormalizer;
    private $customObjectNormalizers;
    private $defaultObjectNormalizer;
    private $encoderMap;

    public function __construct(NormalizerInterface $nativePhpNormalizer, NormalizerInterface $defaultObjectNormalizer, array $customObjectNormalizers = array(), array $encoderMap = array())
    {
        if ($nativePhpNormalizer instanceof SerializerAwareInterface) {
            $nativePhpNormalizer->setSerializer($this);
        }
        $this->nativePhpTypeNormalizer = $nativePhpNormalizer;

        if ($defaultObjectNormalizer instanceof SerializerAwareInterface) {
            $defaultObjectNormalizer->setSerializer($this);
        }
        $this->defaultObjectNormalizer = $defaultObjectNormalizer;

        foreach ($customObjectNormalizers as $normalizer) {
            if ($normalizer instanceof SerializerAwareInterface) {
                $normalizer->setSerializer($this);
            }
        }
        $this->customObjectNormalizers = $customObjectNormalizers;

        foreach ($encoderMap as $encoder) {
            if ($encoder instanceof SerializerAwareInterface) {
                $encoder->setSerializer($this);
            }
        }
        $this->encoderMap = $encoderMap;
    }

    /**
     * {@inheritDoc}
     */
    public final function normalize($data, $format = null)
    {
        // needs to run first so that users can override the behavior for built-in
        // interface like \Traversable, see #10
        if ($this->customObjectNormalizers && is_object($data)) {
            foreach ($this->customObjectNormalizers as $normalizer) {
                if ($normalizer->supportsNormalization($data, $format)) {
                    return $normalizer->normalize($data, $format);
                }
            }
        }

        if ($this->nativePhpTypeNormalizer->supportsNormalization($data, $format)) {
            return $this->nativePhpTypeNormalizer->normalize($data, $format);
        }

        return $this->defaultObjectNormalizer->normalize($data, $format);
    }

    /**
     * {@inheritDoc}
     */
    public final function denormalize($data, $type, $format = null)
    {
        if ($this->nativePhpTypeNormalizer->supportsDenormalization($data, $type, $format)) {
            return $this->nativePhpTypeNormalizer->denormalize($data, $type, $format);
        }

        if ($this->customObjectNormalizers) {
            foreach ($this->customObjectNormalizers as $normalizer) {
                if ($normalizer->supportsDenormalization($data, $type, $format)) {
                    return $normalizer->denormalize($data, $type, $format);
                }
            }
        }

        return $this->defaultObjectNormalizer->denormalize($data, $type, $format);
    }

    /**
     * {@inheritDoc}
     */
    public final function serialize($data, $format)
    {
        $data = $this->normalize($data, $format);

        return $this->encode($data, $format);
    }

    /**
     * {@inheritDoc}
     */
    public final function deserialize($data, $type, $format)
    {
        $data = $this->decode($data, $format);

        return $this->denormalize($data, $type, $format);
    }

    /**
     * {@inheritDoc}
     */
    public final function encode($data, $format)
    {
        return $this->getEncoder($format)->encode($data, $format);
    }

    /**
     * {@inheritDoc}
     */
    public final function decode($data, $format)
    {
        return $this->getEncoder($format)->decode($data, $format);
    }

    protected function getEncoder($format)
    {
        if (!isset($this->encoderMap[$format])) {
            throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
        }

        return $this->encoderMap[$format];
    }
}

{'fraction_non_alphanumeric': 0.11574577443906296, 'fraction_numerical': 0.001779183552436493, 'mean_word_length': 2.8706962509563887, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5737502', 'n_tokens_mistral': 2687, 'n_tokens_neox': 2621, 'n_words': 649}