<commit_before><?php

/**
 * League.Url (http://url.thephpleague.com)
 *
 * @package   League.uri
 * @author    <NAME> <<EMAIL>>
 * @copyright 2013-2015 <NAME>
 * @license   https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License)
 * @version   4.0.0
 * @link      https://github.com/thephpleague/uri/
 */
namespace League\Uri\Components;

use InvalidArgumentException;
use League\Uri\Interfaces\Components\DataPath as DataPathInterface;
use RuntimeException;
use SplFileObject;

/**
 * Value object representing a URI path component.
 *
 * @package League.uri
 * @author  <NAME> <<EMAIL>>
 * @since   4.0.0
 */
class DataPath extends Path implements DataPathInterface
{
    const DEFAULT_MIMETYPE = 'text/plain';

    const DEFAULT_PARAMETER = 'charset=us-ascii';

    const BINARY_PARAMETER = 'base64';

    /**
     * The File Data
     *
     * @var string
     */
    protected $data;

    /**
     * The File MimeType
     *
     * @var string
     */
    protected $mimeType = self::DEFAULT_MIMETYPE;

    /**
     * The File optional parameters
     *
     * @var string[]
     */
    protected $parameters = [self::DEFAULT_PARAMETER];

    /**
     * is the file save as a binary data
     *
     * @var bool
     */
    protected $isBinaryData = false;

    /**
     * a new Media Instance
     *
     * @param string $str
     */
    public function __construct($str = null)
    {
        if (!empty($str)) {
            $this->assertValidComponent($str);
            $parts = $this->extractPathParts($this->validateString($str));
            $this->validate($parts);
        }
    }

    /**
     * Extract Path part according to RFC2937
     *
     * @param string $str
     *
     * @throws InvalidArgumentException if the string is not valid according to RFC2937
     *
     * @return string[]
     */
    protected function extractPathParts($str)
    {
        $this->assertValidComponent($str);
        if (!mb_detect_encoding($str, 'US-ASCII', true)
            || false === strpos($str, ',')
            || false !== strpos($str, '\n')
        ) {
            throw new InvalidArgumentException(
                sprintf('The submitted path `%s` is invalid according to RFC2937', $str)
            );
        }

        $commaPos = strpos($str, ',');

        $matches['mediatype'] = substr($str, 0, $commaPos);
        $matches['data'] = ',' !== substr($str, -1) ? substr($str, $commaPos + 1) : '';

        return $matches;
    }

    /**
     * Validate the string
     *<commit_msg>Improve PR #11<commit_after><?php

/**
 * League.Url (http://url.thephpleague.com)
 *
 * @package   League.uri
 * @author    <NAME> <<EMAIL>>
 * @copyright 2013-2015 <NAME>
 * @license   https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License)
 * @version   4.0.0
 * @link      https://github.com/thephpleague/uri/
 */
namespace League\Uri\Components;

use InvalidArgumentException;
use League\Uri\Interfaces\Components\DataPath as DataPathInterface;
use RuntimeException;
use SplFileObject;

/**
 * Value object representing a URI path component.
 *
 * @package League.uri
 * @author  <NAME> <<EMAIL>>
 * @since   4.0.0
 */
class DataPath extends Path implements DataPathInterface
{
    const DEFAULT_MIMETYPE = 'text/plain';

    const DEFAULT_PARAMETER = 'charset=us-ascii';

    const BINARY_PARAMETER = 'base64';

    /**
     * The File Data
     *
     * @var string
     */
    protected $data;

    /**
     * The File MimeType
     *
     * @var string
     */
    protected $mimeType = self::DEFAULT_MIMETYPE;

    /**
     * The File optional parameters
     *
     * @var string[]
     */
    protected $parameters = [self::DEFAULT_PARAMETER];

    /**
     * is the file save as a binary data
     *
     * @var bool
     */
    protected $isBinaryData = false;

    /**
     * a new Media Instance
     *
     * @param string $str
     */
    public function __construct($str = null)
    {
        if (!empty($str)) {
            $this->assertValidComponent($str);
            $parts = $this->extractPathParts($this->validateString($str));
            $this->validate($parts);
        }
    }

    /**
     * Extract Path part according to RFC2937
     *
     * @param string $str
     *
     * @throws InvalidArgumentException if the string is not valid according to RFC2937
     *
     * @return string[]
     */
    protected function extractPathParts($str)
    {
        $this->assertValidComponent($str);
        if (!mb_detect_encoding($str, 'US-ASCII', true)
            || false === strpos($str, ',')
            || false !== strpos($str, '\n')
        ) {
            throw new InvalidArgumentException(
                sprintf('The submitted path `%s` is invalid according to RFC2937', $str)
            );
        }

        $res = explode(',', $str, 2);

        return [
            'mediatype' => array_shift($res),
            'data' => array_shift($res),
        ];
    }

    /**
     * Validate the string
     *
{'fraction_non_alphanumeric': 0.13869346733668342, 'fraction_numerical': 0.012462311557788945, 'mean_word_length': 2.1633820724729818, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 29, 'https://': 4, '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': '3800432', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1490, 'n_words': 355}