PHP Classes

File: tests/src/NoUsesAttr.php

Recommend this page to a friend!
  Packages of Rodolfo Berrios Arce   Parameter   tests/src/NoUsesAttr.php   Download  
File: tests/src/NoUsesAttr.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Parameter
Validate function parameters with PHP attributes
Author: By
Last change:
Date: 1 year ago
Size: 2,736 bytes
 

Contents

Class file image Download
<?php

/*
 * This file is part of Chevere.
 *
 * (c) Rodolfo Berrios <rodolfo@chevere.org>
 *
 * 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\Tests\src;

use Chevere\Parameter\Exceptions\ParameterException;
use Chevere\Parameter\Interfaces\ParameterAttributeInterface;
use Throwable;
use function Chevere\Parameter\Attributes\arrayArguments;
use function Chevere\Parameter\Attributes\arrayAttr;
use function Chevere\Parameter\Attributes\boolAttr;
use function Chevere\Parameter\Attributes\enumAttr;
use function Chevere\Parameter\Attributes\floatAttr;
use function Chevere\Parameter\Attributes\intAttr;
use function Chevere\Parameter\Attributes\iteratorAttr;
use function Chevere\Parameter\Attributes\nullAttr;
use function Chevere\Parameter\Attributes\returnAttr;
use function Chevere\Parameter\Attributes\stringAttr;
use function Chevere\Parameter\Attributes\valid;
use function PHPUnit\Framework\assertSame;

final class NoUsesAttr
{
    public function __construct(
        string $name = 'Test',
        int $age = 12,
        array $cols = [
            'id' => 1,
        ],
        iterable $tags = ['Chevere', 'Chevere', 'Chevere', 'Uh'],
        bool $flag = false,
        float $amount = 0,
        mixed $null = null,
        string $enum = 'value',
    ) {
        // Validate all
        valid();
        // Pick validation
        valid('name');
        valid('age');
        valid('cols');
        valid('tags');
        valid('flag');
        valid('amount');

        try {
            valid('404');
        } catch (ParameterException $e) {
            assertSame(
                'Parameter `404` not found',
                $e->getMessage()
            );
        }
        // Get attribute, validate and return
        try {
            $name = stringAttr('name')($name);
        } catch (Throwable $e) {
            assertSame(
                'No `' . ParameterAttributeInterface::class . '` attribute for parameter `name`',
                $e->getMessage()
            );
        }
        // $age = intAttr('age')($age);
        // $cols = arrayAttr('cols')($cols);
        // $id = arrayArguments('cols')->required('id')->int();
        // $tags = iteratorAttr('tags')($tags);
        // $flag = boolAttr('flag')($flag);
        // $amount = floatAttr('amount')($amount);
        // $null = nullAttr('null')($null);
        // $enum = enumAttr('enum')($enum);
        // Validate return attr
        // returnAttr()($id);
    }

    public function run(): int
    {
        return returnAttr()(1);
    }

    public static function return(): int
    {
        return 120;
    }
}