Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   1  <?php
   2  /*
   3   * Copyright 2015-2017 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   *   http://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  use function is_array;
  23  use function is_float;
  24  use function is_int;
  25  use function is_object;
  26  use function is_string;
  27  use function MongoDB\generate_index_name;
  28  use function sprintf;
  29  
  30  /**
  31   * Index input model class.
  32   *
  33   * This class is used to validate user input for index creation.
  34   *
  35   * @internal
  36   * @see \MongoDB\Collection::createIndexes()
  37   * @see https://github.com/mongodb/specifications/blob/master/source/enumerate-indexes.rst
  38   * @see http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/
  39   */
  40  class IndexInput implements Serializable
  41  {
  42      /** @var array */
  43      private $index;
  44  
  45      /**
  46       * @param array $index Index specification
  47       * @throws InvalidArgumentException
  48       */
  49      public function __construct(array $index)
  50      {
  51          if (! isset($index['key'])) {
  52              throw new InvalidArgumentException('Required "key" document is missing from index specification');
  53          }
  54  
  55          if (! is_array($index['key']) && ! is_object($index['key'])) {
  56              throw InvalidArgumentException::invalidType('"key" option', $index['key'], 'array or object');
  57          }
  58  
  59          foreach ($index['key'] as $fieldName => $order) {
  60              if (! is_int($order) && ! is_float($order) && ! is_string($order)) {
  61                  throw InvalidArgumentException::invalidType(sprintf('order value for "%s" field within "key" option', $fieldName), $order, 'numeric or string');
  62              }
  63          }
  64  
  65          if (! isset($index['name'])) {
  66              $index['name'] = generate_index_name($index['key']);
  67          }
  68  
  69          if (! is_string($index['name'])) {
  70              throw InvalidArgumentException::invalidType('"name" option', $index['name'], 'string');
  71          }
  72  
  73          $this->index = $index;
  74      }
  75  
  76      /**
  77       * Return the index name.
  78       *
  79       * @return string
  80       */
  81      public function __toString()
  82      {
  83          return $this->index['name'];
  84      }
  85  
  86      /**
  87       * Serialize the index information to BSON for index creation.
  88       *
  89       * @see \MongoDB\Collection::createIndexes()
  90       * @see http://php.net/mongodb-bson-serializable.bsonserialize
  91       * @return array
  92       */
  93      public function bsonSerialize()
  94      {
  95          return $this->index;
  96      }
  97  }