avalanche-development/swagger-router-middleware

View on GitHub
src/Parser/ExplodeTrait.php

Summary

Maintainability
A
35 mins
Test Coverage
<?php

namespace AvalancheDevelopment\SwaggerRouterMiddleware\Parser;

use Exception;

trait ExplodeTrait
{

    /**
     * @param mixed $value
     * @param array $parameter
     * @return array
     */
    protected function explodeValue($value, array $parameter)
    {
        $delimiter = $this->getDelimiter($parameter);
        return preg_split("@{$delimiter}@", $value);
    }

    /**
     * @param array $parameter
     * @return string
     */
    protected function getDelimiter(array $parameter)
    {
        $collectionFormat = 'csv';
        if (isset($parameter['collectionFormat'])) {
            $collectionFormat = $parameter['collectionFormat'];
        }

        switch ($collectionFormat) {
            case 'csv':
                $delimiter = ',';
                break;
            case 'ssv':
                $delimiter = '\s';
                break;
            case 'tsv':
                $delimiter = '\t';
                break;
            case 'pipes':
                $delimiter = '|';
                break;
            default:
                throw new Exception('Invalid collection format value defined in swagger');
                break;
        }

        return $delimiter;
    }
}