Firestore/src/WriteBatch.php
public function set($document, array $fields, array $options = [])
    {
        $merge = $this->pluck('merge', $options, false) ?: false;

        // Record whether the document is empty before any filtering.
        $emptyDocument = count($fields) === 0;

        list ($fields, $sentinels, $metadata) = $this->filterFields($fields);

        if (!$merge && $metadata['hasDelete']) {
            throw new \InvalidArgumentException('Delete cannot appear in data unless `$options[\'merge\']` is set.');
        }

        // Enqueue a write if any of the following conditions are met
        // - if there are still fields remaining after sentinels were removed
        // - if the user provided an empty set to begin with
        // - if the user provided only transform sentinel values AND did not specify merge behavior
        // - if the user provided only delete sentinel field values.

        $updateNotRequired = count($fields) === 0
            && !$emptyDocument
            && !$metadata['hasUpdateMask']
            && $metadata['hasTransform'];

        $shouldEnqueueUpdate = $fields
            || $emptyDocument
            || ($updateNotRequired && !$merge)
            || $metadata['hasUpdateMask'];

        if ($shouldEnqueueUpdate) {
            $write = [
                'fields' => $this->valueMapper->encodeValues($fields),
            ];

            if ($merge) {
                $write['updateMask'] = $this->pathsToStrings($this->encodeFieldPaths($fields), $sentinels);
            }

            $this->writes[] = $this->createDatabaseWrite(self::TYPE_UPDATE, $document, $write, $options);
        }

        // document transform operations are enqueued as a separate mutation.
        $this->enqueueTransforms($document, $sentinels, $options);

        return $this;
    }
public function set($document, array $fields, array $options = [])
    {
        $merge = $this->pluck('merge', $options, false) ?: false;

        // Record whether the document is empty before any filtering.
        $emptyDocument = count($fields) === 0;

        list ($fields, $sentinels, $metadata) = $this->filterFields($fields);

        if (!$merge && $metadata['hasDelete']) {
            throw new \InvalidArgumentException('Delete cannot appear in data unless `$options[\'merge\']` is set.');
        }

        // Enqueue a write if any of the following conditions are met
        // - if there are still fields remaining after sentinels were removed
        // - if the user provided an empty set to begin with
        // - if the user provided only transform sentinel values AND did not specify merge behavior
        // - if the user provided only delete sentinel field values.

        $updateNotRequired = count($fields) === 0
            && !$emptyDocument
            && !$metadata['hasUpdateMask']
            && $metadata['hasTransform'];

        $shouldEnqueueUpdate = $fields
            || $emptyDocument
            || ($updateNotRequired && !$merge)
            || $metadata['hasUpdateMask'];

        if ($shouldEnqueueUpdate) {
            $write = [
                'fields' => $this->valueMapper->encodeValues($fields),
            ];

            if ($merge) {
                $write['updateMask'] = $this->pathsToStrings($this->encodeFieldPaths($fields), $sentinels);
            }

            $this->writes[] = $this->createDatabaseWrite(self::TYPE_UPDATE, $document, $write, $options);
        }

        // document transform operations are enqueued as a separate mutation.
        $this->enqueueTransforms($document, $sentinels, $options);

        return $this;
    }
public function set ( $ document , array $ fields , array $ options = [ ] ) { $ merge = $ this -> pluck ( 'merge' , $ options , false ) ? : false ; // Record whether the document is empty before any filtering. $ emptyDocument = count ( $ fields ) === 0 ; list ( $ fields , $ sentinels , $ metadata ) = $ this -> filterFields ( $ fields ) ; if ( ! $ merge && $ metadata [ 'hasDelete' ] ) { throw new \ InvalidArgumentException ( 'Delete cannot appear in data unless `$options[\'merge\']` is set.' ) ; } // Enqueue a write if any of the following conditions are met // - if there are still fields remaining after sentinels were removed // - if the user provided an empty set to begin with // - if the user provided only transform sentinel values AND did not specify merge behavior // - if the user provided only delete sentinel field values. $ updateNotRequired = count ( $ fields ) === 0 && ! $ emptyDocument && ! $ metadata [ 'hasUpdateMask' ] && $ metadata [ 'hasTransform' ] ; $ shouldEnqueueUpdate = $ fields || $ emptyDocument || ( $ updateNotRequired && ! $ merge ) || $ metadata [ 'hasUpdateMask' ] ; if ( $ shouldEnqueueUpdate ) { $ write = [ 'fields' => $ this -> valueMapper -> encodeValues ( $ fields ) , ] ; if ( $ merge ) { $ write [ 'updateMask' ] = $ this -> pathsToStrings ( $ this -> encodeFieldPaths ( $ fields ) , $ sentinels ) ; } $ this -> writes [ ] = $ this -> createDatabaseWrite ( self :: TYPE_UPDATE , $ document , $ write , $ options ) ; } // document transform operations are enqueued as a separate mutation. $ this -> enqueueTransforms ( $ document , $ sentinels , $ options ) ; return $ this ; }
Enqueue a set mutation.

Unless `$options['merge']` is set to `true, this method replaces all
fields in a Firestore document.

Example:
```
$batch->set($documentName, [
'name' => 'John'
]);

@codingStandardsIgnoreStart
@param DocumentReference|string $document The document to target, either
as a string document name, or DocumentReference object. Please
note that DocumentReferences will be used only for the document
name. Field data must be provided in the `$fields` argument.
@param array $fields An array containing fields, where keys are the field
names, and values are field values. Nested arrays are allowed.
Note that unlike {@see Google\Cloud\Firestore\WriteBatch::update()},
field paths are NOT supported by this method.
@param array $options {
Configuration Options

@type bool $merge If true, unwritten fields will be preserved.
Otherwise, they will be overwritten (removed). **Defaults to**
`false`.
}
@return WriteBatch
@codingStandardsIgnoreEnd
@throws \InvalidArgumentException If `FieldValue::deleteField()` is found in the document when `$options.merge`
is not `true`.
@throws \InvalidArgumentException If `FieldValue::serverTimestamp()` is found in an array value.
Enqueue a set mutation .