public function testEncodeDecodeUniversalTimestamp_HistoricalDate_Same() { $model = new TestMongoDateModel(); $model->universalTimestamp = UniversalTimestamp::fromStringTimestamp('2001-01-01 12:01:01.321'); $encoded = MongoEncoder::encode($model); $this->assertInstanceOf(UTCDateTime::class, $encoded['universalTimestamp']); // var_dump($encoded); $decodedModel = new TestMongoDateModel(); MongoDecoder::decode($decodedModel, $encoded); $this->assertEquals($model->universalTimestamp, $decodedModel->universalTimestamp); // $iso8601 = $decodedModel->universalTimestamp->asFormattedString(UniversalTimestamp::ISO8601_WITH_MILLISECONDS); // var_dump($iso8601); }
/** * @param string $term * @param string | array $projectIdOrIds * @param Website $website * @param bool $include */ public function __construct($term, $projectIdOrIds = '', $website, $include = false) { $query = array('$or' => array(array('name' => array('$regex' => $term, '$options' => '-i')), array('username' => array('$regex' => $term, '$options' => '-i')), array('email' => array('$regex' => $term, '$options' => '-i')))); if (!empty($projectIdOrIds)) { // Allow $projectIdOrIds to be either an array or a single ID if (is_array($projectIdOrIds)) { $idsForQuery = $projectIdOrIds; } else { $idsForQuery = array($projectIdOrIds); } // If passed string IDs, convert to MongoID objects $idsForQuery = array_map(function ($id) { if (is_string($id)) { return MongoMapper::mongoID($id); } else { return $id; } }, $idsForQuery); $inOrNotIn = $include ? '$in' : '$nin'; $query['projects'] = array($inOrNotIn => $idsForQuery); //error_log("Query: " . print_r($query, true)); } // Filter for only users on the current site $encodedDomain = $website->domain; MongoEncoder::encodeDollarDot($encodedDomain); $query['siteRole.' . $encodedDomain] = array('$exists' => true); parent::__construct(UserModelMongoMapper::instance(), $query, array('username', 'email', 'name', 'avatarRef')); // If we were called with a project filter that excluded certain users, also // return a list of specifically which users were excluded. Which happens to // be another typeahead search with the same query term, but *including* only // the ones matching this project. if ($projectIdOrIds && !$include) { $this->excludedUsers = new UserTypeaheadModel($term, $projectIdOrIds, $website, true); $this->excludedUsers->read(); } //echo("Result: " . print_r($this, true)); }
/** * @param object $model * @param string $id * @param int $keyStyle * @param string $rootId * @param string $property * @see ID_IN_KEY * @see ID_IN_DOC * @return string * @throws \Exception */ public function write($model, $id, $keyStyle = MongoMapper::ID_IN_KEY, $rootId = '', $property = '') { CodeGuard::checkTypeAndThrow($rootId, 'string'); CodeGuard::checkTypeAndThrow($property, 'string'); CodeGuard::checkTypeAndThrow($id, 'string'); $data = MongoEncoder::encode($model); // TODO Take into account key style for stripping key out of the model if needs be if (empty($rootId)) { // We're doing a root level update, only $model, $id are relevant $id = $this->update($data, $id, self::ID_IN_KEY, '', ''); } else { if ($keyStyle == self::ID_IN_KEY) { CodeGuard::checkNullAndThrow($id, 'id'); $id = $this->update($data, $id, self::ID_IN_KEY, $rootId, $property); } else { if (empty($id)) { // TODO would be nice if the encode above gave us the id it generated so we could return it to be consistent. CP 2013-08 throw new \Exception("Method appendSubDocument() is not implemented"); //$this->appendSubDocument($data, $rootId, $property); } else { $id = $this->update($data, $id, self::ID_IN_DOC, $rootId, $property); } } } return $id; }
/** * Sets key/values in the array from the public properties of $model * @param object $model * @return array */ public static function encode($model) { $encoder = new MongoEncoder(); return $encoder->_encode($model); }