Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Collection.php

class Collection

Same name in this branch
  1. 11.1.x vendor/ramsey/collection/src/Collection.php \Ramsey\Collection\Collection
  2. 11.1.x vendor/phpdocumentor/type-resolver/src/Types/Collection.php \phpDocumentor\Reflection\Types\Collection

Validates a collection with constraints defined for specific keys.

@author Bernhard Schussek <bschussek@gmail.com>

Hierarchy

  • class \Symfony\Component\Validator\Constraint
    • class \Symfony\Component\Validator\Constraints\Composite extends \Symfony\Component\Validator\Constraint
      • class \Symfony\Component\Validator\Constraints\Collection extends \Symfony\Component\Validator\Constraints\Composite

Expanded class hierarchy of Collection

3 files declare their use of Collection
ProjectRelease.php in core/modules/update/src/ProjectRelease.php
Recipe.php in core/lib/Drupal/Core/Recipe/Recipe.php
SecurityAdvisory.php in core/modules/system/src/SecurityAdvisories/SecurityAdvisory.php
46 string references to 'Collection'
BlockContentForm::save in core/modules/block_content/src/BlockContentForm.php
Form submission handler for the 'save' action.
BlockContentTypeForm::save in core/modules/block_content/src/BlockContentTypeForm.php
Form submission handler for the 'save' action.
CommentTypeForm::save in core/modules/comment/src/CommentTypeForm.php
Form submission handler for the 'save' action.
Config::query in core/modules/migrate_drupal/src/Plugin/migrate/source/d8/Config.php
Constraint::checkArray in vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php
Validates an array

... See full list

File

vendor/symfony/validator/Constraints/Collection.php, line 21

Namespace

Symfony\Component\Validator\Constraints
View source
class Collection extends Composite {
    public const MISSING_FIELD_ERROR = '2fa2158c-2a7f-484b-98aa-975522539ff8';
    public const NO_SUCH_FIELD_ERROR = '7703c766-b5d5-4cef-ace7-ae0dd82304e9';
    protected const ERROR_NAMES = [
        self::MISSING_FIELD_ERROR => 'MISSING_FIELD_ERROR',
        self::NO_SUCH_FIELD_ERROR => 'NO_SUCH_FIELD_ERROR',
    ];
    public array $fields = [];
    public bool $allowExtraFields = false;
    public bool $allowMissingFields = false;
    public string $extraFieldsMessage = 'This field was not expected.';
    public string $missingFieldsMessage = 'This field is missing.';
    
    /**
     * @param array<string,Constraint>|array<string,mixed>|null $fields             An associative array defining keys in the collection and their constraints
     * @param string[]|null                                     $groups
     * @param bool|null                                         $allowExtraFields   Whether to allow additional keys not declared in the configured fields (defaults to false)
     * @param bool|null                                         $allowMissingFields Whether to allow the collection to lack some fields declared in the configured fields (defaults to false)
     */
    public function __construct(mixed $fields = null, ?array $groups = null, mixed $payload = null, ?bool $allowExtraFields = null, ?bool $allowMissingFields = null, ?string $extraFieldsMessage = null, ?string $missingFieldsMessage = null) {
        if (self::isFieldsOption($fields)) {
            $fields = [
                'fields' => $fields,
            ];
        }
        parent::__construct($fields, $groups, $payload);
        $this->allowExtraFields = $allowExtraFields ?? $this->allowExtraFields;
        $this->allowMissingFields = $allowMissingFields ?? $this->allowMissingFields;
        $this->extraFieldsMessage = $extraFieldsMessage ?? $this->extraFieldsMessage;
        $this->missingFieldsMessage = $missingFieldsMessage ?? $this->missingFieldsMessage;
    }
    protected function initializeNestedConstraints() : void {
        parent::initializeNestedConstraints();
        foreach ($this->fields as $fieldName => $field) {
            // the XmlFileLoader and YamlFileLoader pass the field Optional
            // and Required constraint as an array with exactly one element
            if (\is_array($field) && 1 == \count($field)) {
                $this->fields[$fieldName] = $field = $field[0];
            }
            if (!$field instanceof Optional && !$field instanceof Required) {
                $this->fields[$fieldName] = new Required($field);
            }
        }
    }
    public function getRequiredOptions() : array {
        return [
            'fields',
        ];
    }
    protected function getCompositeOption() : string {
        return 'fields';
    }
    private static function isFieldsOption($options) : bool {
        if (!\is_array($options)) {
            return false;
        }
        foreach ($options as $optionOrField) {
            if ($optionOrField instanceof Constraint) {
                return true;
            }
            if (null === $optionOrField) {
                continue;
            }
            if (!\is_array($optionOrField)) {
                return false;
            }
            if ($optionOrField && !($optionOrField[0] ?? null) instanceof Constraint) {
                return false;
            }
        }
        return true;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Collection::$allowExtraFields public property
Collection::$allowMissingFields public property
Collection::$extraFieldsMessage public property
Collection::$fields public property
Collection::$missingFieldsMessage public property
Collection::ERROR_NAMES protected constant Maps error codes to the names of their constants. Overrides Constraint::ERROR_NAMES
Collection::getCompositeOption protected function Returns the name of the property that contains the nested constraints. Overrides Composite::getCompositeOption
Collection::getRequiredOptions public function Returns the name of the required options. Overrides Constraint::getRequiredOptions
Collection::initializeNestedConstraints protected function Initializes the nested constraints. Overrides Composite::initializeNestedConstraints
Collection::isFieldsOption private static function
Collection::MISSING_FIELD_ERROR public constant
Collection::NO_SUCH_FIELD_ERROR public constant
Collection::__construct public function Overrides Composite::__construct
Composite::addImplicitGroupName public function Implicit group names are forwarded to nested constraints. Overrides Constraint::addImplicitGroupName
Composite::getNestedConstraints public function @internal Used by metadata
Constraint::$groups public property The groups that the constraint belongs to.
Constraint::$payload public property Domain-specific data attached to a constraint.
Constraint::CLASS_CONSTRAINT public constant Marks a constraint that can be put onto classes.
Constraint::DEFAULT_GROUP public constant The name of the group given to all constraints with no explicit group.
Constraint::getDefaultOption public function Returns the name of the default option. 28
Constraint::getErrorName public static function Returns the name of the given error code.
Constraint::getTargets public function Returns whether the constraint can be put onto classes, properties or
both.
8
Constraint::normalizeOptions protected function
Constraint::PROPERTY_CONSTRAINT public constant Marks a constraint that can be put onto properties.
Constraint::validatedBy public function Returns the name of the class that validates this constraint. 9
Constraint::__get public function Returns the value of a lazily initialized option. 2
Constraint::__isset public function 1
Constraint::__set public function Sets the value of a lazily initialized option. 1
Constraint::__sleep public function Optimizes the serialized value to minimize storage space.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal