Elastica\Bulk\Action::toString PHP Method

toString() public method

public toString ( ) : string
return string
    public function toString()
    {
        $string = JSON::stringify($this->getActionMetadata(), JSON_FORCE_OBJECT) . Bulk::DELIMITER;
        if ($this->hasSource()) {
            $source = $this->getSource();
            if (is_string($source)) {
                $string .= $source;
            } elseif (is_array($source) && array_key_exists('doc', $source) && is_string($source['doc'])) {
                $docAsUpsert = isset($source['doc_as_upsert']) ? ', "doc_as_upsert": ' . $source['doc_as_upsert'] : '';
                $string .= '{"doc": ' . $source['doc'] . $docAsUpsert . '}';
            } else {
                $string .= JSON::stringify($source, JSON_UNESCAPED_UNICODE);
            }
            $string .= Bulk::DELIMITER;
        }
        return $string;
    }

Usage Example

Esempio n. 1
0
 /**
  * @group unit
  */
 public function testAction()
 {
     $action = new Action();
     $this->assertEquals('index', $action->getOpType());
     $this->assertFalse($action->hasSource());
     $expected = '{"index":{}}' . "\n";
     $this->assertEquals($expected, $action->toString());
     $action->setIndex('index');
     $expected = '{"index":{"_index":"index"}}' . "\n";
     $this->assertEquals($expected, $action->toString());
     $action->setType('type');
     $expected = '{"index":{"_index":"index","_type":"type"}}' . "\n";
     $this->assertEquals($expected, $action->toString());
     $action->setId(1);
     $expected = '{"index":{"_index":"index","_type":"type","_id":1}}' . "\n";
     $this->assertEquals($expected, $action->toString());
     $action->setRouting(1);
     $expected = '{"index":{"_index":"index","_type":"type","_id":1,"_routing":1}}' . "\n";
     $this->assertEquals($expected, $action->toString());
     $client = $this->_getClient();
     $index = new Index($client, 'index2');
     $type = new Type($index, 'type2');
     $action->setIndex($index);
     $expected = '{"index":{"_index":"index2","_type":"type","_id":1,"_routing":1}}' . "\n";
     $this->assertEquals($expected, $action->toString());
     $action->setType($type);
     $expected = '{"index":{"_index":"index2","_type":"type2","_id":1,"_routing":1}}' . "\n";
     $this->assertEquals($expected, $action->toString());
     $action->setSource(array('user' => 'name'));
     $expected = '{"index":{"_index":"index2","_type":"type2","_id":1,"_routing":1}}' . "\n";
     $expected .= '{"user":"******"}' . "\n";
     $this->assertEquals($expected, $action->toString());
     $this->assertTrue($action->hasSource());
     $this->assertFalse(Action::isValidOpType('foo'));
     $this->assertTrue(Action::isValidOpType('delete'));
 }