Inheritance: implements Serializable, implements Alcaeus\MongoDbAdapter\TypeInterface, implements JsonSerializable
 function it_hydrates_a_result_record(Builder $builder, Query $query, ProductInterface $product, Association $association, AssociationType $associationType, ProductInterface $associatedProduct1, ProductInterface $associatedProduct2, DocumentManager $documentManager, \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $metadata, Collection $collection, ArrayIterator $arrayIterator)
 {
     $options = ['locale_code' => 'en_US', 'scope_code' => 'print', 'current_group_id' => null, 'attributes_configuration' => [], 'association_type_id' => 1, 'current_product' => $product];
     $builder->getQuery()->willReturn($query);
     $builder->hydrate(false)->willReturn($builder);
     $associatedProduct1->getId()->willReturn('550ae6b98ead0ed7778b46bb');
     $associatedProduct2->getId()->willReturn('550ae6b98abd0ec8778b46bb');
     $product->getAssociations()->willReturn([$association]);
     $association->getAssociationType()->willReturn($associationType);
     $associationType->getId()->willReturn(1);
     $association->getProducts()->willReturn([$associatedProduct1, $associatedProduct2]);
     $queryDefinition = ['type' => 1, 'sort' => ['normalizedData.is_associated' => -1, '_id' => 1], 'limit' => 10, 'skip' => 0, 'query' => ['_id' => ['$ne' => \MongoId::__set_state(['$id' => '550ae6b98ead0ee8778b46bb'])]], 'newObj' => []];
     $query->getQuery()->willReturn($queryDefinition);
     $query->getDocumentManager()->willReturn($documentManager);
     $documentManager->getDocumentCollection(Argument::any())->willReturn($collection);
     $documentManager->getClassMetadata(Argument::any())->willReturn($metadata);
     $metadata->getFieldNames()->willReturn(['id', 'created', 'updated', 'locale', 'scope', 'values', 'indexedValues', 'indexedValuesOutdated', 'family', 'familyId', 'categories', 'categoryIds', 'enabled', 'groups', 'groupIds', 'associations', 'completenesses', 'normalizedData']);
     $pipeline = [['$match' => ['_id' => ['$ne' => \MongoId::__set_state(['$id' => '550ae6b98ead0ee8778b46bb'])]]], ['$project' => ['id' => 1, 'created' => 1, 'updated' => 1, 'locale' => 1, 'scope' => 1, 'values' => 1, 'indexedValues' => 1, 'indexedValuesOutdated' => 1, 'family' => 1, 'familyId' => 1, 'categories' => 1, 'categoryIds' => 1, 'enabled' => 1, 'groups' => 1, 'groupIds' => 1, 'associations' => 1, 'completenesses' => 1, 'normalizedData' => 1, 'is_associated' => ['$cond' => [['$or' => [['$eq' => ['$_id', \MongoId::__set_state(['$id' => '550ae6b98ead0ed7778b46bb'])]], ['$eq' => ['$_id', \MongoId::__set_state(['$id' => '550ae6b98abd0ec8778b46bb'])]]]], 1, 0]]]], ['$sort' => ['is_associated' => -1]], ['$skip' => 0], ['$limit' => 10]];
     $collection->aggregate($pipeline)->willReturn($arrayIterator);
     $fixture = ['_id' => \MongoId::__set_state(['$id' => '550ae6b98ead0ee8778b46bb']), 'normalizedData' => [], 'sku' => ['attribute' => ['code' => 'sku', 'attributeType' => 'text', 'backendType' => 'text'], 'locale' => null, 'scope' => null, 'value' => 'mysku'], 'name' => ['attribute' => ['code' => 'name', 'attributeType' => 'text', 'backendType' => 'text'], 'locale' => 'fr_FR', 'scope' => null], 'desc' => ['attribute' => ['code' => 'desc', 'attributeType' => 'text', 'backendType' => 'text'], 'locale' => 'fr_FR', 'scope' => 'print'], 'is_associated' => 1];
     $arrayIterator->toArray()->willReturn([$fixture]);
     $rows = $this->hydrate($builder, $options);
     $rows->shouldHaveCount(1);
     $firstResult = $rows[0];
     $firstResult->shouldBeAnInstanceOf('\\Oro\\Bundle\\DataGridBundle\\Datasource\\ResultRecord');
 }
Ejemplo n.º 2
0
 public static function setMongoIdForClasses($objectRef)
 {
     $mongoId = new MongoId();
     $objectRef->_id = $mongoId;
     $objectRef->id = $mongoId->__toString();
     return $objectRef;
 }
Ejemplo n.º 3
0
Archivo: Id.php Proyecto: webcodr/mango
 public function __construct($value = null)
 {
     if (!$value instanceof \MongoId) {
         $value = new \MongoId($value);
     }
     $this->value = $value->__toString();
 }
 function it_transforms_product_fields_result(\MongoId $id)
 {
     $locale = 'fr_FR';
     $id->__toString()->willReturn(42);
     $result = ['_id' => $id, 'created' => null, 'updated' => null, 'enabled' => null];
     $expected = ['created' => null, 'updated' => null, 'enabled' => false, 'id' => 42, 'dataLocale' => 'fr_FR'];
     $this->transform($result, $locale)->shouldReturn($expected);
 }
Ejemplo n.º 5
0
 /**
  * @param $mongoId
  */
 public function assertMongoId($mongoId)
 {
     if (is_string($mongoId)) {
         $mongoId = new \MongoId($mongoId);
     }
     $this->assertTrue($mongoId instanceof \MongoId);
     $this->assertNotNull($mongoId->getPID());
     $this->assertNotNull($mongoId->getTimestamp());
     $this->assertNotNull($mongoId->getInc());
     $this->assertNotNull($mongoId->getHostname());
     $this->assertEquals(24, strlen($mongoId->{'$id'}));
 }
Ejemplo n.º 6
0
 /**
  * Check if given string is a potentially valid MongoId string<br>
  *
  * NOTE: This check is not bullet proof but is useful in most situations.
  *
  * @param $id
  *
  * @return bool
  */
 public static function isMongoId($id)
 {
     if ($id instanceof \MongoId) {
         return true;
     }
     if (!self::isString($id)) {
         return false;
     }
     $match = self::str($id)->match('[0-9a-f]{24}', false);
     if (!$match) {
         return false;
     }
     $mongoId = new \MongoId($match[0]);
     return $mongoId->getTimestamp() > 0;
 }
Ejemplo n.º 7
0
 public function setPageId($data)
 {
     if (!MongoId::isValid($data)) {
         throw new Exception('INVALID MENUITEMINTERNAL PAGEID', 500);
     }
     $this->pageId = (string) $data;
 }
 public function gen_value($input)
 {
     //如果为空,返回空
     $input = trim($input);
     if ($input == "" || $input == "-") {
         return "";
     }
     //先检查是不是已经是 mongoid
     if (strlen($input) == 24 && MongoId::isValid($input)) {
         return $input;
     }
     $this->db->select(array($this->valueField, $this->showField))->where(array($this->showField => $input));
     if (!isset($this->whereOrgId)) {
         $this->whereOrgId = $this->CI->myOrgId;
     }
     $this->db->where(array('orgId' => $this->whereOrgId));
     $query = $this->db->get($this->tableName);
     if ($query->num_rows() > 0) {
         $result = $query->row_array();
         $real_id = $result['_id']->{'$id'};
     } else {
         $new_id = $this->plusCreate($input);
         $real_id = $new_id->{'$id'};
     }
     return $real_id;
 }
Ejemplo n.º 9
0
 public static function getMapped($value)
 {
     if (!$value or !\MongoId::isValid($value)) {
         return false;
     }
     return self::$list[(string) $value];
 }
Ejemplo n.º 10
0
 /**
  * Get a file in MongoGridFS
  * The result is cached to save resource.
  *
  * @return \MongoGridFS
  */
 public function getDocument()
 {
     if (!isset($this->cache['document'])) {
         $this->cache['document'] = $this->fs->findOne(array('filename' => $this->getResolvedPath()));
     }
     return $this->cache['document'];
 }
Ejemplo n.º 11
0
 /**
  * @param $jobId
  * @return JobBase
  */
 public function getJob($jobId)
 {
     if (!\MongoId::isValid($jobId) || !($data = $this->collection()->findOne(['_id' => new \MongoId($jobId)])) || !($class = $this->getClass($data['type']))) {
         return null;
     }
     return new $class($data);
 }
Ejemplo n.º 12
0
 /**
  * Match the eagerly loaded results to their parents.
  *
  * @param  array   $models
  * @param  \Illuminate\Database\Eloquent\Collection  $results
  * @param  string  $relation
  * @return array
  */
 public function match(array $models, Collection $results, $relation)
 {
     $foreign = $this->foreignKey;
     $other = $this->otherKey;
     // First we will get to build a dictionary of the child models by their primary
     // key of the relationship, then we can easily match the children back onto
     // the parents using that dictionary and the primary key of the children.
     $dictionary = [];
     foreach ($results as $result) {
         $dictionary[(string) $result->getAttribute($other)] = $result;
     }
     // Once we have the dictionary constructed, we can loop through all the parents
     // and match back onto their children using these keys of the dictionary and
     // the primary key of the children to map them onto the correct instances.
     foreach ($models as $model) {
         if (\MongoId::isValid((string) $model->{$foreign})) {
             $modelForeign = (string) $model->{$foreign};
         } else {
             $modelForeign = $model->{$foreign};
         }
         if (isset($dictionary[$modelForeign])) {
             $model->setRelation($relation, $dictionary[$modelForeign]);
         }
     }
     return $models;
 }
Ejemplo n.º 13
0
 /**
  * Sets the id
  * @param int|Object $arg0
  */
 function setId($arg0)
 {
     if ($this->getIdType() == self::ID_TYPE_MONGO) {
         if (is_string($arg0) && \MongoId::isValid($arg0)) {
             parent::setId(new \MongoId($arg0));
         } else {
             if ($arg0 instanceof \MongoId) {
                 parent::setId($arg0);
             } else {
                 if (is_null($arg0)) {
                     parent::setId($arg0);
                 } else {
                     if (is_string($arg0) && trim($arg0) == '') {
                         parent::setId(null);
                     } else {
                         if (is_int($arg0) && $arg0 == 0) {
                             parent::setId(null);
                         } else {
                             try {
                                 throw new \Exception('Invalid ID set: ' . var_export($arg0, true));
                             } catch (\Exception $e) {
                                 \Mojavi\Logging\LoggerManager::error(__METHOD__ . " :: " . $e->getTraceAsString());
                             }
                             parent::setId(null);
                         }
                     }
                 }
             }
         }
     } else {
         if ($this->getIdType() == self::ID_TYPE_STRING) {
             if (is_string($arg0)) {
                 parent::setId($arg0);
             } else {
                 if ($arg0 instanceof \MongoId) {
                     parent::setId((string) $arg0);
                 } else {
                     if (is_null($arg0)) {
                         parent::setId($arg0);
                     } else {
                         if (is_string($arg0) && trim($arg0) == '') {
                             parent::setId(null);
                         } else {
                             if (is_int($arg0) && $arg0 == 0) {
                                 parent::setId(null);
                             } else {
                                 try {
                                     throw new \Exception('Invalid ID set: ' . var_export($arg0, true));
                                 } catch (\Exception $e) {
                                     \Mojavi\Logging\LoggerManager::error(__METHOD__ . " :: " . $e->getTraceAsString());
                                 }
                                 parent::setId(null);
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 14
0
 /**
  * Get the key value of the parent's local key.
  *
  * @param $value
  * @return mixed
  */
 public function evaluateObjectID($value)
 {
     $isObject = config('database.connections.mongodb.mongoid', false);
     if ($isObject && \MongoId::isValid($value)) {
         return new \MongoId($value);
     }
     return $value;
 }
Ejemplo n.º 15
0
 /**
  * 
  * @param type $var
  */
 public function loadTodo($var)
 {
     if (MongoId::isValid($var)) {
         $this->loadDocument($var);
     } else {
         $this->loadDocument($var, "some field");
     }
 }
Ejemplo n.º 16
0
 /**
  * Retrieves the _id portion of the route.
  * If it's not there, errors and returns null.
  */
 protected static function getIdFromRoute(array $restOfRoute)
 {
     if (!isset($restOfRoute[0]) || !MongoId::isValid($restOfRoute[0])) {
         self::error("invalid access");
         self::render('notice');
         return null;
     }
     return new MongoId($restOfRoute[0]);
 }
Ejemplo n.º 17
0
 public static final function toMongoId($id)
 {
     if ($id and is_string($id) and MongoId::isValid($id)) {
         $id = new MongoId($id);
     } elseif ($id and is_array($id)) {
         $id = array_map([__CLASS__, 'toMongoId'], $id);
     }
     return $id;
 }
 function it_hydrates_a_result_record(Builder $builder, Query $query, ProductInterface $product, Association $association, AssociationType $associationType, ProductInterface $associatedProduct1, ProductInterface $associatedProduct2, ArrayCollection $productsCollection, ArrayCollection $productIdsCollection, ArrayIterator $productsIterator, ArrayCollection $associationsCollection, ArrayIterator $associationsIterator, ArrayIterator $arrayIterator)
 {
     $product->getId()->willReturn('110ae6b98ead0ee8778b46bb');
     $options = ['locale_code' => 'en_US', 'scope_code' => 'print', 'current_group_id' => null, 'attributes_configuration' => [], 'association_type_id' => 1, 'current_product' => $product];
     $builder->find()->willReturn($builder);
     $builder->count()->willReturn($builder);
     $builder->getQuery()->willReturn($query);
     $builder->hydrate(false)->willReturn($builder);
     $builder->setQueryArray(Argument::any())->willReturn($builder);
     $builder->limit(Argument::any())->willReturn($builder);
     $builder->skip(Argument::any())->willReturn($builder);
     $product->getAssociations()->willReturn($associationsCollection);
     $associationsCollection->getIterator()->willReturn($associationsIterator);
     $associationsIterator->rewind()->shouldBeCalled();
     $associationsCount = 1;
     $associationsIterator->valid()->will(function () use(&$associationsCount) {
         return $associationsCount-- > 0;
     });
     $associationsIterator->next()->shouldBeCalled();
     $associationsIterator->current()->will(new ReturnPromise([$association]));
     $associationsCollection->filter(Argument::any())->willReturn($associationsIterator);
     $associationsIterator->first()->willReturn($association);
     $association->getAssociationType()->willReturn($associationType);
     $associationType->getId()->willReturn(1);
     $associatedProduct1->getId()->willReturn('220ae6b98ead0ed7778b46bb');
     $associatedProduct2->getId()->willReturn('330ae6b98abd0ec8778b46bb');
     $association->getProducts()->willReturn($productsCollection);
     $productsCollection->getIterator()->willReturn($productsIterator);
     $productsIterator->rewind()->shouldBeCalled();
     $productsCount = 2;
     $productsIterator->valid()->will(function () use(&$productsCount) {
         return $productsCount-- > 0;
     });
     $associatedProduct1->getId()->willReturn('220ae6b98ead0ed7778b46bb');
     $associatedProduct2->getId()->willReturn('330ae6b98abd0ec8778b46bb');
     $association->getProducts()->willReturn($productsCollection);
     $productsCollection->getIterator()->willReturn($productsIterator);
     $productsIterator->rewind()->shouldBeCalled();
     $productsCount = 2;
     $productsIterator->valid()->will(function () use(&$productsCount) {
         return $productsCount-- > 0;
     });
     $productsIterator->next()->shouldBeCalled();
     $productsIterator->current()->will(new ReturnPromise([$associatedProduct1, $associatedProduct2]));
     $productsCollection->map(Argument::any())->willReturn($productIdsCollection);
     $productIdsCollection->toArray()->willReturn(['220ae6b98ead0ed7778b46bb', '330ae6b98abd0ec8778b46bb']);
     $queryDefinition = ['type' => 1, 'sort' => ['normalizedData.is_associated' => -1, '_id' => 1], 'limit' => 10, 'skip' => 0, 'query' => ['_id' => ['$ne' => \MongoId::__set_state(['$id' => '110ae6b98ead0ee8778b46bb'])]], 'newObj' => []];
     $query->getQuery()->willReturn($queryDefinition);
     $fixture = ['_id' => \MongoId::__set_state(['$id' => '550ae6b98ead0ee8778b46bb']), 'normalizedData' => [], 'sku' => ['attribute' => ['code' => 'sku', 'attributeType' => 'text', 'backendType' => 'text'], 'locale' => null, 'scope' => null, 'value' => 'mysku'], 'name' => ['attribute' => ['code' => 'name', 'attributeType' => 'text', 'backendType' => 'text'], 'locale' => 'fr_FR', 'scope' => null], 'desc' => ['attribute' => ['code' => 'desc', 'attributeType' => 'text', 'backendType' => 'text'], 'locale' => 'fr_FR', 'scope' => 'print'], 'is_associated' => 1];
     $query->execute()->willReturn($arrayIterator);
     $arrayIterator->toArray()->willReturn([$fixture]);
     $rows = $this->hydrate($builder, $options);
     $rows->shouldHaveCount(1);
     $firstResult = $rows[0];
     $firstResult->shouldBeAnInstanceOf('\\Oro\\Bundle\\DataGridBundle\\Datasource\\ResultRecord');
 }
Ejemplo n.º 19
0
 function queryFix($query)
 {
     if (!is_array($query)) {
         if (MongoId::isValid($query)) {
             $query = array("_id" => new MongoId($query));
         } else {
             $query = array("_id" => $query);
         }
     }
     return $query;
 }
Ejemplo n.º 20
0
 public function getById($id)
 {
     if (!MongoId::isValid($id)) {
         return null;
     }
     $datas = KLib\MongoDB::findOne(array('_id' => $id), 'kn_block');
     if (is_array($datas) && !empty($datas) && array_key_exists('type', $datas) && is_string($datas['type']) && class_exists($datas['type'])) {
         return KLib\instance::addOne($datas['type'], $datas);
     }
     return null;
 }
Ejemplo n.º 21
0
 public function __construct($id = null)
 {
     // Convert all passed ids to lowercase string
     if (is_string($id)) {
         $id = strtolower($id);
     }
     // Already a MongoId
     if ($id instanceof \MongoId) {
         $id = $id->{"\$id"};
     }
     return parent::__construct($id);
 }
Ejemplo n.º 22
0
 private function generateId()
 {
     if (null === self::$refInc) {
         self::$refInc = (int) mt_rand(0, pow(2, 24));
     }
     $this->timestamp = time();
     $this->inc = self::$refInc++;
     $this->pid = getmypid();
     if ($this->pid > 32768) {
         $this->pid = $this->pid - 32768;
     }
     return $this->assembleId();
 }
Ejemplo n.º 23
0
 public function init($value)
 {
     if (gettype($value) != "array") {
         $value = array();
     }
     $this->value = $value;
     foreach ($value as $key => $this_item) {
         if (MongoId::isValid($this_item)) {
             $this->real_data[$key] = new User_model();
             $this->real_data[$key]->init_with_id($this_item);
         }
     }
 }
Ejemplo n.º 24
0
 /**
  * Fetches a document from the collection
  * 
  * This function is a way of getting a document from the collection into the object
  * Since there will always be an _id field its a safe bet to load from there
  * however there will generally always be another unique field to search by
  * so this function allows classes that extend this class to specify which field
  * 
  * @param String|MongoId $var The value of the field to check
  * @param String $field
  * @return array
  */
 public function loadDocument($var, $field = "_id")
 {
     $query = null;
     if ($field === "_id" && MongoId::isValid($var)) {
         $query = array($field => new MongoId($var));
     } else {
         $query = array($field => $var);
     }
     $this->document = $this->collection->findOne($query);
     if ($this->document !== null) {
         $this->qry = $query;
     }
     return $this->document;
 }
Ejemplo n.º 25
0
 /**
  * Transform the value in a MongoDBRef if needed
  * @param $attr
  * @param $value
  * @return mixed
  */
 protected function _val($attr, $value)
 {
     $reference = \Rocketr\Schema\Reference::get($this->get_collection_name(), $attr);
     $collection = \Rocketr\Schema\HasCollection::get($this->get_collection_name(), $attr);
     if ($reference xor $collection) {
         $ref_attr = \Rocketr\Schema\Map::on($this->get_collection_name(), $attr);
         $_id = is_array($value) && isset($value[$ref_attr]) || is_object($value) && $value->{$ref_attr} ? is_array($value) ? $value[$ref_attr] : $value->{$ref_attr} : $value;
         $target = $reference ? $reference : $collection;
         $_id = $ref_attr == '_id' && \MongoId::isValid($_id) ? new \MongoId($_id) : $_id;
         return \MongoDBRef::create($target, $_id);
     } else {
         return $value;
     }
 }
Ejemplo n.º 26
0
 public function load($type, array $id)
 {
     $type = (int) $type;
     $id = (string) $id['id'];
     $collectionName = $this->_getCollectionName($type);
     if (!MongoId::isValid($id)) {
         return false;
     }
     $mongoId = new MongoId($id);
     $data = $this->_getMongoDb()->findOne($collectionName, ['_id' => $mongoId]);
     if (null === $data) {
         return false;
     }
     return $data;
 }
Ejemplo n.º 27
0
 /**
  * Validate the given request with the given rules.
  *
  * @param  array  $data
  * @param  array  $rules
  * @param  array  $messages
  * @return void
  */
 public function validate(array $data, array $rules, array $messages = array(), $connection = '')
 {
     Validator::extend('mongo_id', function ($attribute, $value, $parameters) {
         return true === \MongoId::isValid($value);
     });
     $validator = Validator::make($data, $rules, $messages);
     if (!empty($connection)) {
         $verifier = App::make('validation.presence');
         $verifier->setConnection($connection);
         $validator->setPresenceVerifier($verifier);
     }
     if ($validator->fails()) {
         $this->throwValidationException($validator);
     }
 }
Ejemplo n.º 28
0
 private function convertRowValues($row)
 {
     $id = new MongoId($row["_id"]);
     $time_stamp = $row["lastModified"]->sec;
     //$id->getTimestamp();
     $dt = new DateTime("@{$time_stamp}");
     $row["created"] = $dt->format('F d, Y g:i');
     $row["id"] = $id->__toString();
     //parse date modified to use in direct URL to post
     $date_of_post = date_parse($row["created"]);
     $row["month"] = $date_of_post["month"];
     $row["day"] = $date_of_post["day"];
     $row["year"] = $date_of_post["year"];
     $row["safe_title"] = $this->convertPostTitleSpacesToHyphens($row["title"]);
     return $row;
 }
Ejemplo n.º 29
0
 /**
  * Atomically dumps content into a file.
  *
  * @param  string  $filename The file to be written to.
  * @param  string  $content  The data to write into the file.
  * @param  integer $mode     The file mode (octal).
  * @throws IOException       If the file cannot be written to.
  */
 public function dumpFile($filename, $content, $mode = 0666)
 {
     $dir = dirname($filename);
     $dirFileInfo = new SplFileInfo($dir, $this->fs);
     if (!$dirFileInfo->isDir()) {
         $this->mkdir($dir);
     } elseif (!$dirFileInfo->isWritable()) {
         throw new IOException(sprintf('Unable to write in the %s directory\\n', $dir));
     }
     $this->remove($filename);
     try {
         $this->fs->storeBytes($content, array('filename' => $filename, 'type' => 'file'));
     } catch (\MongoCursorException $e) {
         throw new IOException(sprintf('Failed to write file "%s".', $filename));
     }
 }
Ejemplo n.º 30
0
 /**
  * Executes a GET request
  * @return \Mojavi\Form\AjaxForm
  */
 function executeGet($input_form)
 {
     // Handle GET Requests
     $ajax_form = new \Mojavi\Form\AjaxForm();
     if (\MongoId::isValid($input_form->getId())) {
         $input_form->query();
         $ajax_form->setRecord($input_form);
     } else {
         $entries = $input_form->queryAll();
         $ajax_form->setEntries($entries);
         $ajax_form->setTotal($input_form->getTotal());
         $ajax_form->setPage($input_form->getPage());
         $ajax_form->setItemsPerPage($input_form->getItemsPerPage());
     }
     return $ajax_form;
 }