<commit_before><?php

/**
 * PHP Version 5.3
 *
 * @copyright   (c) 2006-2014 <NAME>
 * @author      <NAME> <<EMAIL>>
 * @license     http://opensource.org/licenses/MIT MIT
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ptlis\ConNeg\TypeBuilder;

use ptlis\ConNeg\Exception\InvalidTypeException;
use ptlis\ConNeg\Type\MimeAbsentType;
use ptlis\ConNeg\Type\MimeType;
use ptlis\ConNeg\Type\MimeWildcardSubType;
use ptlis\ConNeg\Type\MimeWildcardType;
use ptlis\ConNeg\Type\TypeInterface;

/**
 * Concrete builder for Mime type.
 */
class MimeTypeBuilder extends TypeBuilder
{
    /**
     * Get the built type from the specification previously provided.
     *
     * @throws InvalidTypeException
     *
     * @return TypeInterface
     */
    public function get()
    {
        switch (true) {
            // Absent Type
            case 0 === strlen($this->type):
                $type = new MimeAbsentType($this->qFactorFactory->get(0, $this->appType));
                break;

            // Type & subtype present
            case 2 === count($explodedType = explode('/', $this->type)):
                list($mimeType, $subType) = $explodedType;

                $this->validateType($mimeType, $subType);

                $type = $this->getType();
                break;

            default:
                throw new InvalidTypeException(
                    '"' . $this->type . '" is not a valid mime type'
                );
                break;
        }

        return $type;
    }


    /**
     * Validate the provided type data.
     *
     * @throws InvalidTypeException
     *
     * @param string $mimeType
     * @param string $subType
     */
    private function validateType($mimeType, $subType)
    {
        if (($mimeType === '*' || $subType === '*') && $this->appType) {
            throw new InvalidTypeException(
                'Wildcards are not valid in application-provided types.'
            );
        }

        if ($mimeType === '*' && $subType !== '*') {
            throw new InvalidTypeException(
                '"' . $this->type . '" is not a valid mime type'
            );
        }
    }


    /**
     * Get the type object from the provided specification.
     *
     * @throws InvalidTypeException
     *
     * @return TypeInterface
     */
    protected function getType()
    {
        $explodedType = explode('/', $this->type);
        list($mimeType, $subType) = $explodedType;

        switch (true) {
            // Full wildcard type<commit_msg>Fix: Cleanup awkward type validation logic.<commit_after><?php

/**
 * PHP Version 5.3
 *
 * @copyright   (c) 2006-2014 <NAME>
 * @author      <NAME> <<EMAIL>>
 * @license     http://opensource.org/licenses/MIT MIT
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ptlis\ConNeg\TypeBuilder;

use ptlis\ConNeg\Exception\InvalidTypeException;
use ptlis\ConNeg\Type\MimeAbsentType;
use ptlis\ConNeg\Type\MimeType;
use ptlis\ConNeg\Type\MimeWildcardSubType;
use ptlis\ConNeg\Type\MimeWildcardType;
use ptlis\ConNeg\Type\TypeInterface;

/**
 * Concrete builder for Mime type.
 */
class MimeTypeBuilder extends TypeBuilder
{
    /**
     * Get the built type from the specification previously provided.
     *
     * @throws InvalidTypeException
     *
     * @return TypeInterface
     */
    public function get()
    {
        switch (true) {
            // Absent Type
            case 0 === strlen($this->type):
                $type = new MimeAbsentType($this->qFactorFactory->get(0, $this->appType));
                break;

            // Type & subtype present
            case 2 === count($explodedType = explode('/', $this->type)):
                list($mimeType, $subType) = $explodedType;

                $this->validateType($mimeType, $subType);

                $type = $this->getType();
                break;

            default:
                throw new InvalidTypeException(
                    '"' . $this->type . '" is not a valid mime type'
                );
        }

        return $type;
    }


    /**
     * Validate the provided type data.
     *
     * @throws InvalidTypeException
     *
     * @param string $mimeType
     * @param string $subType
     */
    private function validateType($mimeType, $subType)
    {
        $this->noAppWildcards($mimeType, $subType);
        $this->validWildcard($mimeType, $subType);
    }

    /**
     * Ensure that application-provided types do not include wildcards.
     *
     * @throws InvalidTypeException
     *
     * @param string $mimeType
     * @param string $subType
     */
    private function noAppWildcards($mimeType, $subType)
    {
        if ($this->appType && ($mimeType === '*' || $subType === '*')) {
            throw new InvalidTypeException(
                'Wildcards are not valid in application-provided types.'
            );
        }
    }

    /**
     * Ensure that wildcards are valid (you may not have a wildcard type but a concrete subtype).
     *
     * @throws InvalidTypeException
     *
     * @param string $mimeType
     * @param string $subType
     */
    private function validWildcard($mimeType, $subType)
    {
        if ($mimeType === '*' && $subType !== '*') {
            throw new InvalidTypeException(
                '"' . $this->type . '" is not a valid mime type.'
            );
        }
    }


    /**
     * Get the type object from the provided specification.
     *
     * @throws InvalidTypeException
     *
     * @return TypeInterface
     */
    protected function getType()
    {
        $explodedType = explode('/', $this->type);
        list($mimeType, $subType) = $explodedType;

        switch (true) {
            // Full wildcard type
{'fraction_non_alphanumeric': 0.12170212765957447, 'fraction_numerical': 0.004425531914893617, 'mean_word_length': 1.9394697348674337, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 35, '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': '3934780', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1639, 'n_words': 423}