Example #1
0
try {
    //Получение информации о базе данных
    //@see https://tech.yandex.ru/datasync/http/doc/tasks/get-database-docpage/
    $database = $dataSync->getDatabase($databaseId);
    if (isset($_REQUEST['action'])) {
        if ($_REQUEST['action'] === 'editTitle' && isset($_REQUEST['title']) && $_REQUEST['title']) {
            //Изменение заголовка базы данных
            //@see https://tech.yandex.ru/datasync/http/doc/tasks/edit-title-docpage/
            $database = $dataSync->updateDatabaseTitle($_REQUEST['title'], $databaseId);
        } elseif ($_REQUEST['action'] === 'createField' && isset($_REQUEST['collectionId']) && $_REQUEST['collectionId'] && isset($_REQUEST['recordId']) && $_REQUEST['recordId'] && isset($_REQUEST['recordChangeType']) && $_REQUEST['recordChangeType']) {
            if ($_REQUEST['recordChangeType'] === Record::CHANGE_TYPE_INSERT || $_REQUEST['recordChangeType'] === Record::CHANGE_TYPE_UPDATE || $_REQUEST['recordChangeType'] === Record::CHANGE_TYPE_SET || $_REQUEST['recordChangeType'] === Record::CHANGE_TYPE_DELETE) {
                $delta = new Delta();
                //Поясняющий комментарий к изменению.
                $delta->setDeltaId('insert record ' . $_REQUEST['recordId']);
                //Запись
                $record = new Record();
                //Тип изменения, применяемого к записи.
                //@see https://tech.yandex.ru/datasync/http/doc/tasks/create-changes-docpage/#changes-types
                $record->setChangeType($_REQUEST['recordChangeType'])->setCollectionId($_REQUEST['collectionId'])->setRecordId($_REQUEST['recordId']);
                if (isset($_REQUEST['fieldChangeType']) && $_REQUEST['fieldChangeType'] && isset($_REQUEST['fieldId']) && $_REQUEST['fieldId']) {
                    //Поле
                    $field = new RecordField();
                    //Тип изменения, применяемого к полю записи
                    //@see https://tech.yandex.ru/datasync/http/doc/tasks/create-changes-docpage/#changes-types
                    $field->setChangeType($_REQUEST['fieldChangeType']);
                    //Идентификатор поля.
                    $field->setFieldId($_REQUEST['fieldId']);
                    if (isset($_REQUEST['value']) && $_REQUEST['value']) {
                        //Указание значение и его типа
                        //Если Тип значения не указан, то он определиться автоматически
                        //@see https://tech.yandex.ru/datasync/http/doc/tasks/create-changes-docpage/#data-types
 function testFillDelta()
 {
     $fixture = array('delta_id' => 'add new object to collection', 'changes' => array(array('change_type' => 'insert', 'collection_id' => 'my_schedule', 'record_id' => 'monday', 'changes' => array(array('change_type' => 'set', 'field_id' => 'work', 'value' => array('string' => 'from 11am to 7pm', 'type' => 'string'))))));
     $recordFieldValue = new RecordFieldValue();
     $recordFieldValue->setValue($fixture['changes'][0]['changes'][0]['value']['string']);
     //Field changes
     $recordField = new RecordField();
     $recordField->setValue($recordFieldValue)->setFieldId($fixture['changes'][0]['changes'][0]['field_id'])->setChangeType(RecordField::CHANGE_TYPE_SET);
     //Record changes
     $record = new Record();
     $record->setChangeType(Record::CHANGE_TYPE_INSERT)->setCollectionId($fixture['changes'][0]['collection_id'])->setRecordId($fixture['changes'][0]['record_id'])->setChanges(array($recordField))->setFields(array($recordField))->setRevision(0);
     $delta = new Database\Delta();
     $delta->setDeltaId($fixture['delta_id'])->setChanges(array($record));
     $delta2 = new Database\Delta();
     $delta2->setDeltaId($fixture['delta_id'])->setChanges(new Database\Delta\Records(array($record)));
     $array1 = $delta->toArray();
     $array2 = $delta2->toArray();
     $this->assertEquals($fixture['delta_id'], $delta->getDeltaId());
     $this->assertEquals($delta2->getChanges(), $delta->getChanges());
     $this->assertTrue($array1 === $array2, 'Is Equals arrays');
 }
 function testFillDatabaseRecord()
 {
     $fixture = array('change_type' => 'insert', 'collection_id' => 'my_schedule', 'record_id' => 'monday', 'changes' => array(array('change_type' => 'set', 'field_id' => 'work', 'value' => array('string' => 'from 11am to 7pm', 'type' => 'string'))));
     $record = new Record($fixture);
     $this->assertEquals($fixture['change_type'], $record->getChangeType());
     $this->assertEquals($fixture['collection_id'], $record->getCollectionId());
     $this->assertEquals($fixture['record_id'], $record->getRecordId());
     $this->assertEquals($fixture['changes'][0]['change_type'], $record->getChanges()->getAll()[0]->getChangeType());
     $this->assertEquals($fixture['changes'][0]['field_id'], $record->getChanges()->getAll()[0]->getFieldId());
     $this->assertEquals($fixture['changes'][0]['value']['type'], $record->getChanges()->getAll()[0]->getValue()->getType());
     $this->assertEquals($fixture['changes'][0]['value'][$fixture['changes'][0]['value']['type']], $record->getChanges()->getAll()[0]->getValue()->getValue());
     $changedFiled = new RecordField();
     $changedFiled->setChangeType($fixture['changes'][0]['change_type'])->setFieldId($fixture['changes'][0]['field_id'])->setValue(new RecordFieldValue($fixture['changes'][0]['value']));
     $record2 = new Record();
     $record2->setChangeType($fixture['change_type'])->setCollectionId($fixture['collection_id'])->setChanges([$changedFiled])->setRecordId($fixture['record_id']);
     $this->assertTrue($record->toArray() === $record2->toArray(), 'Is Equals records');
     $record2->setFields($fixture['changes']);
     $this->assertTrue($record2->getFields() instanceof RecordFields, 'Is RecordFields');
     $this->assertTrue($record2->getChanges() instanceof RecordFields, 'Is RecordFields');
     $field = new RecordFields();
     $field->add($fixture['changes'][0]);
     $record3 = new Record();
     $record3->setChangeType($fixture['change_type'])->setCollectionId($fixture['collection_id'])->setChanges($field)->setRecordId($fixture['record_id'])->setFields($field);
     $this->assertTrue($record2->toArray() === $record3->toArray(), 'Is Equals records');
 }