acelot/search-schema

View on GitHub
src/ParamGenerator/DefaultGenerator.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php declare(strict_types=1);

namespace Acelot\SearchSchema\ParamGenerator;

use Acelot\SearchSchema\ParamGeneratorInterface;

class DefaultGenerator implements ParamGeneratorInterface
{
    private $fieldsCounter = [];

    public function generate(string $field): string
    {
        if (!isset($this->fieldsCounter[$field])) {
            $this->fieldsCounter[$field] = 0;
        } else {
            $this->fieldsCounter[$field]++;
        }

        return sprintf(
            ':%s_%s',
            preg_replace('/[^\w]/', '_', $field),
            $this->fieldsCounter[$field]
        );
    }
}