class ValidationResult

A value object to contain the results of a validation.

@property \Drupal\Core\StringTranslation\TranslatableMarkup[] $messages

Hierarchy

Expanded class hierarchy of ValidationResult

29 files declare their use of ValidationResult
AllowedScaffoldPackagesValidatorTest.php in core/modules/package_manager/tests/src/Kernel/AllowedScaffoldPackagesValidatorTest.php
BaseRequirementsFulfilledValidatorTest.php in core/modules/package_manager/tests/src/Kernel/BaseRequirementsFulfilledValidatorTest.php
ComposerMinimumStabilityValidatorTest.php in core/modules/package_manager/tests/src/Kernel/ComposerMinimumStabilityValidatorTest.php
ComposerPatchesValidatorTest.php in core/modules/package_manager/tests/src/Kernel/ComposerPatchesValidatorTest.php
ComposerPluginsValidatorTest.php in core/modules/package_manager/tests/src/Kernel/ComposerPluginsValidatorTest.php

... See full list

File

core/modules/package_manager/src/ValidationResult.php, line 17

Namespace

Drupal\package_manager
View source
final class ValidationResult {
    
    /**
     * Creates a ValidationResult object.
     *
     * @param int $severity
     *   The severity of the result. Should be one of the
     *   SystemManager::REQUIREMENT_* constants.
     * @param \Drupal\Core\StringTranslation\TranslatableMarkup[]|string[] $messages
     *   The result messages.
     * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $summary
     *   A succinct summary of the result.
     * @param bool $assert_translatable
     *   Whether to assert the messages are translatable. Internal use only.
     *
     * @throws \InvalidArgumentException
     *   Thrown if $messages is empty, or if it has 2 or more items but $summary
     *   is NULL.
     */
    private function __construct(int $severity, array $messages, ?TranslatableMarkup $summary, bool $assert_translatable) {
        if ($assert_translatable) {
            assert(Inspector::assertAll(fn($message) => $message instanceof TranslatableMarkup, $messages));
        }
        if (empty($messages)) {
            throw new \InvalidArgumentException('At least one message is required.');
        }
        if (count($messages) > 1 && !$summary) {
            throw new \InvalidArgumentException('If more than one message is provided, a summary is required.');
        }
    }
    
    /**
     * Implements magic ::__get() method.
     */
    public function __get(string $name) : mixed {
        return match ($name) {    'messages' => $this->messages,
        
        };
    }
    
    /**
     * Creates an error ValidationResult object from a throwable.
     *
     * @param \Throwable $throwable
     *   The throwable.
     * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $summary
     *   The errors summary.
     *
     * @return static
     */
    public static function createErrorFromThrowable(\Throwable $throwable, ?TranslatableMarkup $summary = NULL) : static {
        // All Composer Stager exceptions are translatable.
        $is_translatable = $throwable instanceof ExceptionInterface;
        $message = $is_translatable ? $throwable->getTranslatableMessage() : $throwable->getMessage();
        return new static(SystemManager::REQUIREMENT_ERROR, [
            $message,
        ], $summary, $is_translatable);
    }
    
    /**
     * Creates an error ValidationResult object.
     *
     * @param \Drupal\Core\StringTranslation\TranslatableMarkup[] $messages
     *   The error messages.
     * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $summary
     *   The errors summary.
     *
     * @return static
     */
    public static function createError(array $messages, ?TranslatableMarkup $summary = NULL) : static {
        return new static(SystemManager::REQUIREMENT_ERROR, $messages, $summary, TRUE);
    }
    
    /**
     * Creates a warning ValidationResult object.
     *
     * @param \Drupal\Core\StringTranslation\TranslatableMarkup[] $messages
     *   The error messages.
     * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $summary
     *   The errors summary.
     *
     * @return static
     */
    public static function createWarning(array $messages, ?TranslatableMarkup $summary = NULL) : static {
        return new static(SystemManager::REQUIREMENT_WARNING, $messages, $summary, TRUE);
    }
    
    /**
     * Returns the overall severity for a set of validation results.
     *
     * @param \Drupal\package_manager\ValidationResult[] $results
     *   The validation results.
     *
     * @return int
     *   The overall severity of the results. Will be one of the
     *   SystemManager::REQUIREMENT_* constants.
     */
    public static function getOverallSeverity(array $results) : int {
        foreach ($results as $result) {
            if ($result->severity === SystemManager::REQUIREMENT_ERROR) {
                return SystemManager::REQUIREMENT_ERROR;
            }
        }
        // If there were no errors, then any remaining results must be warnings.
        return $results ? SystemManager::REQUIREMENT_WARNING : SystemManager::REQUIREMENT_OK;
    }
    
    /**
     * Determines if two validation results are equivalent.
     *
     * @param self $a
     *   A validation result.
     * @param self $b
     *   Another validation result.
     *
     * @return bool
     *   TRUE if the given validation results have the same severity, summary,
     *   and messages (in the same order); otherwise FALSE.
     */
    public static function isEqual(self $a, self $b) : bool {
        return $a->severity === $b->severity && strval($a->summary) === strval($b->summary) && array_map('strval', $a->messages) === array_map('strval', $b->messages);
    }

}

Members

Title Sort descending Modifiers Object type Summary
ValidationResult::createError public static function Creates an error ValidationResult object.
ValidationResult::createErrorFromThrowable public static function Creates an error ValidationResult object from a throwable.
ValidationResult::createWarning public static function Creates a warning ValidationResult object.
ValidationResult::getOverallSeverity public static function Returns the overall severity for a set of validation results.
ValidationResult::isEqual public static function Determines if two validation results are equivalent.
ValidationResult::__construct private function Creates a ValidationResult object.
ValidationResult::__get public function Implements magic ::__get() method.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.