Phalcon\Db\Adapter\MongoDB\Functions::generateIndexName PHP Method

generateIndexName() public static method

Generate an index name from a key specification.
public static generateIndexName ( array | object $document ) : string
$document array | object Document containing fields mapped to values, which denote order or an index type
return string
    public static function generateIndexName($document)
    {
        if (is_object($document)) {
            $document = get_object_vars($document);
        }
        if (!is_array($document)) {
            throw InvalidArgumentException::invalidType('$document', $document, 'array or object');
        }
        $name = '';
        foreach ($document as $field => $type) {
            $name .= ($name != '' ? '_' : '') . $field . '_' . $type;
        }
        return $name;
    }

Usage Example

Example #1
0
 /**
  * Constructs a count command.
  *
  * Supported options:
  *
  *  * hint (string|document): The index to use. If a document, it will be
  *    interpretted as an index specification and a name will be generated.
  *
  *  * limit (integer): The maximum number of documents to count.
  *
  *  * maxTimeMS (integer): The maximum amount of time to allow the query to
  *    run.
  *
  *  * readConcern (MongoDB\Driver\ReadConcern): Read concern.
  *
  *    For servers < 3.2, this option is ignored as read concern is not
  *    available.
  *
  *  * readPreference (MongoDB\Driver\ReadPreference): Read preference.
  *
  *  * skip (integer): The number of documents to skip before returning the
  *    documents.
  *
  * @param string       $databaseName Database name
  * @param string       $collectionName Collection name
  * @param array|object $filter Query by which to filter documents
  * @param array        $options Command options
  *
  * @throws InvalidArgumentException
  */
 public function __construct($databaseName, $collectionName, $filter = [], array $options = [])
 {
     if (!is_array($filter) && !is_object($filter)) {
         throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
     }
     if (isset($options['hint'])) {
         if (is_array($options['hint']) || is_object($options['hint'])) {
             $options['hint'] = Functions::generateIndexName($options['hint']);
         }
         if (!is_string($options['hint'])) {
             throw InvalidArgumentException::invalidType('"hint" option', $options['hint'], 'string or array or object');
         }
     }
     if (isset($options['limit']) && !is_integer($options['limit'])) {
         throw InvalidArgumentException::invalidType('"limit" option', $options['limit'], 'integer');
     }
     if (isset($options['maxTimeMS']) && !is_integer($options['maxTimeMS'])) {
         throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
     }
     if (isset($options['readConcern']) && !$options['readConcern'] instanceof ReadConcern) {
         throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], 'MongoDB\\Driver\\ReadConcern');
     }
     if (isset($options['readPreference']) && !$options['readPreference'] instanceof ReadPreference) {
         throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], 'MongoDB\\Driver\\ReadPreference');
     }
     if (isset($options['skip']) && !is_integer($options['skip'])) {
         throw InvalidArgumentException::invalidType('"skip" option', $options['skip'], 'integer');
     }
     $this->databaseName = (string) $databaseName;
     $this->collectionName = (string) $collectionName;
     $this->filter = $filter;
     $this->options = $options;
 }
All Usage Examples Of Phalcon\Db\Adapter\MongoDB\Functions::generateIndexName