ComposerNameValidator.php 1.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php
namespace T3o\TerFe2\Validation\Validator;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;

/**
20
 * Validator for composer name
21 22 23 24 25 26 27 28
 */
class ComposerNameValidator extends AbstractValidator
{
    protected $acceptsEmptyValues = false;

    public static $composerNameIsConfirmed = false;

    /**
29
     * Returns false, if given composer name is not set, invalid or too long
30 31
     *
     * @param mixed $value The value that should be validated
32
     * @return bool TRUE if the value is valid, FALSE if an error occured
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
     */
    public function isValid($value)
    {
        $value = (string)$value;
        if ($value === '') {
            if (!self::$composerNameIsConfirmed) {
                return true;
            }
            $this->addError('This composer name must not be empty.', 1527507206);
            return false;
        }
        if (!$this->isValidComposerName($value)) {
            $this->addError('This composer name is not valid.', 1527501477);
            return false;
        }
        return true;
    }

    private function isValidComposerName(string $composerName): bool
    {
53
        if (strlen($composerName) > 60) {
54 55 56 57 58 59 60 61 62 63
            return false;
        }
        if (substr_count($composerName, '/') !== 1) {
            return false;
        }
        $cleanedComposerName = preg_replace('/[^a-z0-9\/_-]/', '', $composerName);

        return $cleanedComposerName === $composerName;
    }
}