Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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['ns'])) {
  66              throw new InvalidArgumentException('Required "ns" option is missing from index specification');
  67          }
  68  
  69          if (! is_string($index['ns'])) {
  70              throw InvalidArgumentException::invalidType('"ns" option', $index['ns'], 'string');
  71          }
  72  
  73          if (! isset($index['name'])) {
  74              $index['name'] = generate_index_name($index['key']);
  75          }
  76  
  77          if (! is_string($index['name'])) {
  78              throw InvalidArgumentException::invalidType('"name" option', $index['name'], 'string');
  79          }
  80  
  81          $this->index = $index;
  82      }
  83  
  84      /**
  85       * Return the index name.
  86       *
  87       * @return string
  88       */
  89      public function __toString()
  90      {
  91          return $this->index['name'];
  92      }
  93  
  94      /**
  95       * Serialize the index information to BSON for index creation.
  96       *
  97       * @see \MongoDB\Collection::createIndexes()
  98       * @see http://php.net/mongodb-bson-serializable.bsonserialize
  99       * @return array
 100       */
 101      public function bsonSerialize()
 102      {
 103          return $this->index;
 104      }
 105  }