Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  /*
   3   * Copyright 2015-present MongoDB, Inc.
   4   *
   5   * Licensed under the Apache License, Version 2.0 (the "License");
   6   * you may not use this file except in compliance with the License.
   7   * You may obtain a copy of the License at
   8   *
   9   *   https://www.apache.org/licenses/LICENSE-2.0
  10   *
  11   * Unless required by applicable law or agreed to in writing, software
  12   * distributed under the License is distributed on an "AS IS" BASIS,
  13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14   * See the License for the specific language governing permissions and
  15   * limitations under the License.
  16   */
  17  
  18  namespace MongoDB\Model;
  19  
  20  use MongoDB\BSON\Serializable;
  21  use MongoDB\Exception\InvalidArgumentException;
  22  
  23  use function is_array;
  24  use function is_float;
  25  use function is_int;
  26  use function is_object;
  27  use function is_string;
  28  use function MongoDB\generate_index_name;
  29  use function sprintf;
  30  
  31  /**
  32   * Index input model class.
  33   *
  34   * This class is used to validate user input for index creation.
  35   *
  36   * @internal
  37   * @see \MongoDB\Collection::createIndexes()
  38   * @see https://github.com/mongodb/specifications/blob/master/source/enumerate-indexes.rst
  39   * @see https://mongodb.com/docs/manual/reference/method/db.collection.createIndex/
  40   */
  41  class IndexInput implements Serializable
  42  {
  43      /** @var array */
  44      private $index;
  45  
  46      /**
  47       * @param array $index Index specification
  48       * @throws InvalidArgumentException
  49       */
  50      public function __construct(array $index)
  51      {
  52          if (! isset($index['key'])) {
  53              throw new InvalidArgumentException('Required "key" document is missing from index specification');
  54          }
  55  
  56          if (! is_array($index['key']) && ! is_object($index['key'])) {
  57              throw InvalidArgumentException::invalidType('"key" option', $index['key'], 'array or object');
  58          }
  59  
  60          foreach ($index['key'] as $fieldName => $order) {
  61              if (! is_int($order) && ! is_float($order) && ! is_string($order)) {
  62                  throw InvalidArgumentException::invalidType(sprintf('order value for "%s" field within "key" option', $fieldName), $order, 'numeric or string');
  63              }
  64          }
  65  
  66          if (! isset($index['name'])) {
  67              $index['name'] = generate_index_name($index['key']);
  68          }
  69  
  70          if (! is_string($index['name'])) {
  71              throw InvalidArgumentException::invalidType('"name" option', $index['name'], 'string');
  72          }
  73  
  74          $this->index = $index;
  75      }
  76  
  77      /**
  78       * Return the index name.
  79       */
  80      public function __toString(): string
  81      {
  82          return $this->index['name'];
  83      }
  84  
  85      /**
  86       * Serialize the index information to BSON for index creation.
  87       *
  88       * @see \MongoDB\Collection::createIndexes()
  89       * @see https://php.net/mongodb-bson-serializable.bsonserialize
  90       */
  91      public function bsonSerialize(): array
  92      {
  93          return $this->index;
  94      }
  95  }