public function testIfWillCompareModel() { $langs = []; $model = new WithEmbeddedArrayI18NModel(); // Attach first $m = new ModelWithI18N(); $m->setLang('en'); $m->_id = new \MongoId(); $m->setLanguages($langs); $m->layout = 'new'; $m->title = 'New York'; $m->setLang('pl'); $m->title = 'Nowy Jork'; $m->setLang('en'); $model->pages[] = $m; // Attach second $m2 = new ModelWithI18N(); $m2->setLang('en'); $m2->_id = new \MongoId(); $m2->setLanguages($langs); $m2->layout = 'new'; $m2->title = 'Prague'; $m2->setLang('pl'); $m2->title = 'Praga'; $m2->setLang('en'); $model->pages[] = $m2; $comparator = new ModelComparator($this); $data = RawArray::fromModel($model); foreach ([0, 1] as $i) { $this->assertSame($model->pages[$i]->_id, $data['pages'][$i]['_id']); $this->assertSame($model->pages[$i]->title, $data['pages'][$i]['title']['en']); $this->assertSame($model->pages[$i]->layout, $data['pages'][$i]['layout']); } $comparator->compare($data, $model); }
/** * Return the current element * @return Document|null * @since v1.3.4 */ public function current() { $document = $this->_cursor->current(); if (empty($document)) { return null; } return RawArray::toModel($document, $this->_model); }
public function testIfWillPopulateSimplePlainModel() { $data = ['_class' => PlainWithBasicAttributes::class, 'int' => 12345, 'string' => 'foo', 'bool' => false, 'float' => 110.23, 'array' => ['new', 'array'], 'null' => null]; $model = RawArray::toModel($data); $this->assertTrue($model instanceof PlainWithBasicAttributes); unset($data['_class']); foreach ($data as $field => $value) { $this->assertSame($value, $model->{$field}); } }
public function testToArray() { $model = new BaseAttributesNoAnnotations(); $array = RawArray::fromModel($model); $this->assertSame($model->int, $array['int']); $this->assertSame($model->string, $array['string']); $this->assertSame($model->bool, $array['bool']); $this->assertSame($model->float, $array['float']); $this->assertSame($model->array, $array['array']); $this->assertSame($model->null, $array['null']); }
public function testIfWillConvertToArrayWithEmbeddedDocuments() { $model = new WithPlainEmbedded(); $model->title = 'stats'; $model->stats = new SimplePlainEmbedded(); $model->stats->active = true; $model->stats->name = 'foo'; $model->stats->visits = 1233; $raw = RawArray::fromModel($model); $this->assertSame($raw['title'], $model->title); foreach (['active', 'name', 'visits'] as $name) { $this->assertSame($model->stats->{$name}, $raw['stats'][$name]); } }
/** * This is use case, where model is initialized from external JSON * And order of elements are changed in json. */ public function testIfWillProperlyStoreI18NFieldsWhenChangedOrderFromExternalSource() { $langs = ['en', 'pl']; $model = new WithEmbeddedArrayI18NModel(); // Attach single $one = new ModelWithI18N(); $one->setLanguages($langs); $one->layout = 'new'; $one->title = 'New York'; $one->setLang('pl'); $one->title = 'Nowy Jork'; $one->setLang('en'); $model->page = $one; // Attach first $m = new ModelWithI18N(); $m->setLanguages($langs); $m->layout = 'new'; $m->title = 'New York'; $m->setLang('pl'); $m->title = 'Nowy Jork'; $m->setLang('en'); $model->pages[] = $m; // Attach second $m = new ModelWithI18NSecond(); $m->setLanguages($langs); $m->layout = 'new'; $m->title = 'Prague'; $m->setLang('pl'); $m->title = 'Praga'; $m->setLang('en'); $model->pages[] = $m; // This are expected values $expectedData = RawArray::fromModel($model); $expectedData['pages'] = array_reverse($expectedData['pages']); // Now assume that external json data arrived $externalData = JsonArray::fromModel($model); $externalData['pages'] = array_reverse($externalData['pages']); $expectedModel = JsonArray::toModel($externalData, $model, $model); $comparator = new ModelComparator($this); $comparator->compare($expectedData, $expectedModel); $raw = RawArray::fromModel($expectedModel); }
/** * Reloads document from database. * It return true if document is reloaded and false if it's no longer exists. * * @return boolean */ public function refresh() { $conditions = PkManager::prepareFromModel($this->model)->getConditions(); $data = $this->getCollection()->findOne($conditions); if (null !== $data) { RawArray::toModel($data, $this->model, $this->model); return true; } else { return false; } }
/** * Creates an model with the given attributes. * This method is internally used by the find methods. * @param mixed[] $data attribute values (column name=>column value) * @return AnnotatedInterface|null the newly created document. The class of the object is the same as the model class. * Null is returned if the input data is false. * @since v1.0 */ protected function populateRecord($data) { if ($data !== null) { if (!empty($data['$err'])) { throw new ManganException(sprintf("There is an error in query: %s", $data['$err'])); } $model = RawArray::toModel($data, $this->model); ScenarioManager::setScenario($model, ScenariosInterface::Update); Event::trigger($model, FinderInterface::EventAfterFind); return $model; } else { return null; } }