PHP Classes

File: src/StringParameter.php

Recommend this page to a friend!
  Classes of Rodolfo Berrios Arce   Parameter   src/StringParameter.php   Download  
File: src/StringParameter.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Parameter
Validate function parameters with PHP attributes
Author: By
Last change:
Date: 15 days ago
Size: 2,734 bytes
 

Contents

Class file image Download
<?php

/*
 * This file is part of Chevere.
 *
 * (c) Rodolfo Berrios <[email protected]>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace
Chevere\Parameter;

use
Chevere\Parameter\Interfaces\StringParameterInterface;
use
Chevere\Parameter\Interfaces\TypeInterface;
use
Chevere\Parameter\Traits\ParameterTrait;
use
Chevere\Regex\Interfaces\RegexInterface;
use
Chevere\Regex\Regex;
use
InvalidArgumentException;
use
Stringable;
use function
Chevere\Message\message;

final class
StringParameter implements StringParameterInterface
{
    use
ParameterTrait;

    private
RegexInterface $regex;

    private ?
string $default = null;

    public function
__invoke(string|Stringable $value): string
   
{
       
$value = strval($value);
        if (
$this->regex->match($value) !== []) {
            return
$value;
        }

        throw new
InvalidArgumentException(
            (string)
message(
               
"Argument value provided%provided% doesn't match the regex `%regex%`",
               
provided: valMd($value, $this->isSensitive),
               
regex: strval($this->regex),
            )
        );
    }

    public function
setUp(): void
   
{
       
$this->regex = new Regex(self::PATTERN_DEFAULT);
    }

    public function
withRegex(RegexInterface $regex): StringParameterInterface
   
{
       
$new = clone $this;
       
$new->regex = $regex;

        return
$new;
    }

    public function
withDefault(string $value): StringParameterInterface
   
{
       
$this($value);
       
$new = clone $this;
       
$new->default = $value;

        return
$new;
    }

    public function
regex(): RegexInterface
   
{
        return
$this->regex;
    }

    public function default(): ?
string
   
{
        return
$this->default;
    }

    public function
schema(): array
    {
        return [
           
'type' => $this->type()->primitive(),
           
'description' => $this->description(),
           
'default' => $this->default(),
           
'regex' => $this->regex()->noDelimiters(),
        ];
    }

    public function
assertCompatible(StringParameterInterface $parameter): void
   
{
        if (
$this->regex->__toString() === $parameter->regex()->__toString()) {
            return;
        }

        throw new
InvalidArgumentException(
            (string)
message(
               
'Expected regex `%expected%`, provided `%provided%`',
               
expected: $this->regex->__toString(),
               
provided: $parameter->regex()->__toString()
            )
        );
    }

    private function
typeName(): string
   
{
        return
TypeInterface::STRING;
    }
}