Sokil\Mongo\Operator::convertToArray PHP Method

convertToArray() public static method

Transform operator in different formats to canonical array form
public static convertToArray ( mixed $mixed ) : array
$mixed mixed
return array
    public static function convertToArray($mixed)
    {
        // get operator from callable
        if (is_callable($mixed)) {
            $callable = $mixed;
            $mixed = new self();
            call_user_func($callable, $mixed);
        }
        // get operator array
        if ($mixed instanceof ArrayableInterface && $mixed instanceof self) {
            $mixed = $mixed->toArray();
        } elseif (!is_array($mixed)) {
            throw new Exception('Mixed must be instance of Operator');
        }
        return $mixed;
    }

Usage Example

Example #1
0
 /**
  * Update multiple documents
  *
  * @param \Sokil\Mongo\Expression|array|callable $expression expression to define
  *  which documents will change.
  * @param \Sokil\Mongo\Operator|array|callable $updateData new data or operators to update
  * @param array $options update options, see http://php.net/manual/ru/mongocollection.update.php
  * @return \Sokil\Mongo\Collection
  * @throws \Sokil\Mongo\Exception
  */
 public function update($expression, $updateData, array $options = array())
 {
     // execute update operator
     $result = $this->getMongoCollection()->update(Expression::convertToArray($expression), Operator::convertToArray($updateData), $options);
     // if write concern acknowledged
     if (is_array($result)) {
         if ($result['ok'] != 1) {
             throw new Exception(sprintf('Update error: %s: %s', $result['err'], $result['errmsg']));
         }
         return $this;
     }
     // if write concern unacknowledged
     if (!$result) {
         throw new Exception('Update error');
     }
     return $this;
 }