Sokil\Mongo\Structure::prepareToStore PHP Method

prepareToStore() public static method

public static prepareToStore ( $value )
    public static function prepareToStore($value)
    {
        // if array - try to prepare every value
        if (is_array($value)) {
            foreach ($value as $k => $v) {
                $value[$k] = self::prepareToStore($v);
            }
            return $value;
        }
        // if scalar - return it
        if (!is_object($value)) {
            return $value;
        }
        // if internal mongo types - pass it as is
        if (in_array(get_class($value), array('MongoId', 'MongoCode', 'MongoDate', 'MongoRegex', 'MongoBinData', 'MongoInt32', 'MongoInt64', 'MongoDBRef', 'MongoMinKey', 'MongoMaxKey', 'MongoTimestamp'))) {
            return $value;
        }
        // do not convert geo-json to array
        if ($value instanceof \GeoJson\Geometry\Geometry) {
            return $value->jsonSerialize();
        }
        // structure
        if ($value instanceof Structure) {
            // validate structure
            if (!$value->isValid()) {
                $exception = new InvalidDocumentException('Embedded document not valid');
                $exception->setDocument($value);
                throw $exception;
            }
            // get array from structure
            return $value->toArray();
        }
        // other objects convert to array
        return (array) $value;
    }

Usage Example

 public function pushEach($fieldName, array $values)
 {
     // value must be list, not dictionary
     $values = array_values($values);
     // prepasre to store
     $values = Structure::prepareToStore($values);
     // no $push operator found
     if (!isset($this->_operators['$push'])) {
         $this->_operators['$push'] = array();
     }
     // no field name found
     if (!isset($this->_operators['$push'][$fieldName])) {
         $this->_operators['$push'][$fieldName] = array('$each' => $values);
     } else {
         if (!is_array($this->_operators['$push'][$fieldName]) || !isset($this->_operators['$push'][$fieldName]['$each'])) {
             $oldValue = $this->_operators['$push'][$fieldName];
             $this->_operators['$push'][$fieldName] = array('$each' => array_merge(array($oldValue), $values));
         } else {
             $this->_operators['$push'][$fieldName]['$each'] = array_merge($this->_operators['$push'][$fieldName]['$each'], $values);
         }
     }
     return $this;
 }
All Usage Examples Of Sokil\Mongo\Structure::prepareToStore