Author: Dmytro Sokil (dmytro.sokil@gmail.com)
Inheritance: extends Sokil\Mongo\Exception
Example #1
0
 /**
  *
  * @throws \Sokil\Mongo\Document\InvalidDocumentException
  * @return \Sokil\Mongo\Document
  */
 public function validate()
 {
     if ($this->triggerEvent('beforeValidate')->isCancelled()) {
         return $this;
     }
     if (!$this->isValid()) {
         $exception = new InvalidDocumentException('Document not valid');
         $exception->setDocument($this);
         $this->triggerEvent('validateError');
         throw $exception;
     }
     $this->triggerEvent('afterValidate');
     return $this;
 }
Example #2
0
 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;
 }