Exemplo n.º 1
0
 /**
  * Do some conversions for some types
  */
 public function __set($key, $val)
 {
     // If it is a Mongo entity, convert it to its reference
     if ($val instanceof ZFE_Model_Mongo) {
         $val = $val->getReference();
     }
     // If it is a DateTime, convert it to MongoDate
     if ($val instanceof DateTime) {
         $val = new MongoDate($val->getTimestamp());
     }
     // If it is an array of Mongo entities or DateTimes
     if (is_array($val)) {
         $_val = array();
         foreach ($val as $i => $v) {
             if ($v instanceof ZFE_Model_Mongo) {
                 $v = $v->getReference();
             }
             if ($v instanceof DateTime) {
                 $v = new MongoDate($v->getTimestamp());
             }
             $_val[$i] = $v;
         }
         $val = $_val;
     }
     parent::__set($key, $val);
 }
Exemplo n.º 2
0
 public function insert($collectionName, $userid, $record)
 {
     if (!$this->verifyCollection($collectionName)) {
         return false;
     }
     $collection = $this->db->selectCollection($collectionName);
     //Set core fields
     $newRec = array();
     $newRec[FIELD_USERID] = $userid;
     $newRec[FIELD_CREATED] = new MongoTimestamp();
     $newRec[FIELD_UPDATED] = new MongoTimestamp();
     //Check type of fields to convert to mongo types
     foreach ($record as $key => $value) {
         $type = gettype($value);
         if ($type == "object") {
             $type = get_class($value);
         }
         if ($type == "DateTime") {
             $value = new MongoDate($value->getTimestamp());
         }
         $newRec[$key] = $value;
     }
     //Insert to dataase
     $result = $collection->insert($newRec);
     if ($result['ok'] == 1) {
         return (string) $newRec['_id'];
     }
     return false;
 }
Exemplo n.º 3
0
 public function set($key, $value)
 {
     switch ($key) {
         case 'date':
             if (!$value instanceof \MongoDate) {
                 if ($value instanceof \DateTime) {
                     $value = new \MongoDate($value->getTimestamp());
                 } elseif (is_string($value)) {
                     $value = new \MongoDate(strtotime($value));
                 } elseif (is_int($value)) {
                     $value = new \MongoDate($value);
                 }
             }
             break;
     }
     parent::set($key, $value);
 }
Exemplo n.º 4
0
/**
 *  @DataType date
 *  @Validate(Date)
 *  @Embed
 */
function is_date(&$date)
{
    /* is_date */
    if ($date instanceof \MongoDate) {
        return true;
    }
    if ($date instanceof \Datetime) {
        $date = new \MongoDate($date->getTimestamp());
    }
    if (is_string($date)) {
        $date = strtotime($date);
    }
    if (is_integer($date) && $date > 0) {
        $date = new \MongoDate($date);
        return true;
    }
    return false;
}
Exemplo n.º 5
0
 /**
  * Parse value with specific definition in $attrs
  *
  * @param string $key key
  * @param mixed $value value
  *
  * @throws Exception\InvalidDataTypeException
  * @return mixed
  */
 public function parseValue($key, $value)
 {
     $attrs = $this->getAttrs();
     if (!isset($attrs[$key]) && is_object($value)) {
         // Handle dates
         if (!$value instanceof \MongoDate || !$value instanceof \MongoId || !$value instanceof \MongoDBRef) {
             if ($value instanceof \DateTime) {
                 $value = new \MongoDate($value->getTimestamp());
             } else {
                 if (method_exists($value, 'toArray')) {
                     $value = (array) $value->toArray();
                 } elseif (method_exists($value, 'to_array')) {
                     $value = (array) $value->to_array();
                 } else {
                     //ingore this object when saving
                     $this->ignoreData[$key] = $value;
                 }
             }
         }
     } elseif (isset($attrs[$key]) && isset($attrs[$key]['type'])) {
         switch ($attrs[$key]['type']) {
             case self::DATA_TYPE_INT:
             case self::DATA_TYPE_INTEGER:
                 $value = (int) $value;
                 break;
             case self::DATA_TYPE_STR:
             case self::DATA_TYPE_STRING:
                 $value = (string) $value;
                 break;
             case self::DATA_TYPE_FLT:
             case self::DATA_TYPE_FLOAT:
             case self::DATA_TYPE_DBL:
             case self::DATA_TYPE_DOUBLE:
                 $value = (double) $value;
                 break;
             case self::DATA_TYPE_TIMESTAMP:
                 if (!$value instanceof \MongoTimestamp) {
                     try {
                         $value = new \MongoTimestamp($value);
                     } catch (\Exception $e) {
                         throw new InvalidDataTypeException('$key cannot be parsed by \\MongoTimestamp', $e->getCode(), $e);
                     }
                 }
                 break;
             case self::DATA_TYPE_DATE:
                 if (!$value instanceof \MongoDate) {
                     try {
                         if (!$value instanceof \MongoDate) {
                             if (is_numeric($value)) {
                                 $value = '@' . $value;
                             }
                             if (!$value instanceof \DateTime) {
                                 $value = new \DateTime($value);
                             }
                             $value = new \MongoDate($value->getTimestamp());
                         }
                     } catch (\Exception $e) {
                         throw new InvalidDataTypeException('$key cannot be parsed by \\DateTime', $e->getCode(), $e);
                     }
                 }
                 break;
             case self::DATA_TYPE_BOOL:
             case self::DATA_TYPE_BOOLEAN:
                 $value = (bool) $value;
                 break;
             case self::DATA_TYPE_OBJ:
             case self::DATA_TYPE_OBJECT:
                 if (!empty($value) && !is_array($value) && !is_object($value)) {
                     throw new InvalidDataTypeException("[{$key}] is not an object");
                 }
                 $value = (object) $value;
                 break;
             case self::DATA_TYPE_ARRAY:
                 if (!empty($value) && !is_array($value)) {
                     throw new InvalidDataTypeException("[{$key}] is not an array");
                 }
                 $value = (array) $value;
                 break;
             case self::DATA_TYPE_EMBED:
             case self::DATA_TYPE_EMBEDS:
             case self::DATA_TYPE_MIXED:
             case self::DATA_TYPE_REFERENCE:
             case self::DATA_TYPE_REFERENCES:
                 break;
             default:
                 throw new InvalidDataTypeException("{$attrs[$key]['type']} is not a valid type");
                 break;
         }
     }
     return $value;
 }
Exemplo n.º 6
0
 public function setTranslation($key, $val, $lang)
 {
     // If it is a Mongo entity, convert it to its reference
     if ($val instanceof ZFE_Model_Mongo) {
         $val = $val->getReference();
     }
     // If it is a DateTime, convert it to MongoDate
     if ($val instanceof DateTime) {
         $val = new MongoDate($val->getTimestamp());
     }
     parent::setTranslation($key, $val, $lang);
 }
Exemplo n.º 7
0
 /**
  *
  */
 public function __set($parameter, $value)
 {
     // Transform 'id' to a MongoId
     if ('id' == $parameter || '_id' == $parameter) {
         $parameter = '_id';
         $value = $value instanceof \MongoId ? $value : new \MongoId($value);
     }
     // Transform DateTime objects to MongoDate
     if ($value instanceof \DateTime) {
         $value = new \MongoDate($value->getTimestamp());
     }
     // Set the saved state to false and save the parameter
     $this->__saved = false;
     $this->__data[$parameter] =& $value;
     // Return the value
     return $value;
 }
Exemplo n.º 8
0
 /**
  * Set additional query, fields will be merged to currently existed request using array_merge.
  *
  * @link http://docs.mongodb.org/manual/tutorial/query-documents/
  * @param array $query Fields and conditions to query by.
  * @return $this
  */
 public function query(array $query = [])
 {
     array_walk_recursive($query, function (&$value) {
         if ($value instanceof \DateTime) {
             //MongoDate is always UTC, which is good :)
             $value = new \MongoDate($value->getTimestamp());
         }
     });
     $this->query = array_merge($this->query, $query);
     return $this;
 }