<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of the Valkyrja framework.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Valkyrja\Path;

use InvalidArgumentException;
use Valkyrja\Application\Application;
use Valkyrja\Support\Providers\Provides;

use function is_array;

/**
 * Class PathGenerator.
 *
 * @author <NAME>
 */
class NativePathGenerator implements PathGenerator
{
    use Provides;

    /**
     * The items provided by this provider.
     *
     * @return array
     */
    public static function provides(): array
    {
        return [
            PathGenerator::class,
        ];
    }

    /**
     * Publish the provider.
     *
     * @param Application $app The application
     *
     * @return void
     */
    public static function publish(Application $app): void
    {
        $app->container()->singleton(PathGenerator::class, new static());
    }

    /**
     * Parse segments, data, and params into a path.
     *
     * @param array $segments The segments
     * @param array $data     [optional] The data
     * @param array $params   [optional] The params
     *
     * @throws InvalidArgumentException
     *
     * @return string
     */
    public function parse(array $segments, array $data = null, array $params = null): string
    {
        // If data was passed but no params
        if (null === $params && null !== $data) {
            throw new InvalidArgumentException('Route params are required when supplying data');
        }

        $path         = '';
        $replace      = [];
        $replacements = [];

        // If there is data, parse the replacements
        if (null !== $data) {
            $this->parseData($segments, $data, $params, $replace, $replacements);
        }

        // Iterate through the segments
        foreach ($segments as $index => $segment) {
            // No need to do replacements if there was no data
            if (null !== $data) {
                // Replace any parameters
                $segment = str_replace($replace, $replacements, $segment);
            }

            // If parameters were replaced or none to begin with
            if (strpos($segment, '{') === false) {
                // Append this segment
                $path .= $segment;
            }<commit_msg>Set params to empty array if null.<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of the Valkyrja framework.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Valkyrja\Path;

use InvalidArgumentException;
use Valkyrja\Application\Application;
use Valkyrja\Support\Providers\Provides;

use function is_array;

/**
 * Class PathGenerator.
 *
 * @author <NAME>
 */
class NativePathGenerator implements PathGenerator
{
    use Provides;

    /**
     * The items provided by this provider.
     *
     * @return array
     */
    public static function provides(): array
    {
        return [
            PathGenerator::class,
        ];
    }

    /**
     * Publish the provider.
     *
     * @param Application $app The application
     *
     * @return void
     */
    public static function publish(Application $app): void
    {
        $app->container()->singleton(PathGenerator::class, new static());
    }

    /**
     * Parse segments, data, and params into a path.
     *
     * @param array $segments The segments
     * @param array $data     [optional] The data
     * @param array $params   [optional] The params
     *
     * @throws InvalidArgumentException
     *
     * @return string
     */
    public function parse(array $segments, array $data = null, array $params = null): string
    {
        // If data was passed but no params
        if (null === $params && null !== $data) {
            throw new InvalidArgumentException('Route params are required when supplying data');
        }

        $path         = '';
        $params       = $params ?? [];
        $replace      = [];
        $replacements = [];

        // If there is data, parse the replacements
        if (null !== $data) {
            $this->parseData($segments, $data, $params, $replace, $replacements);
        }

        // Iterate through the segments
        foreach ($segments as $index => $segment) {
            // No need to do replacements if there was no data
            if (null !== $data) {
                // Replace any parameters
                $segment = str_replace($replace, $replacements, $segment);
            }

            // If parameters were replaced or none to begin with
            if (strpos($segment, '{') === false) {
                // Append this segment
                $path .= $segment;
            }
{'fraction_non_alphanumeric': 0.11004098360655738, 'fraction_numerical': 0.0004098360655737705, 'mean_word_length': 1.988977342314758, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '3213118', 'n_tokens_mistral': 1307, 'n_tokens_neox': 1269, 'n_words': 430}