> For the complete documentation index, see [llms.txt](https://php-fhir-tools.ardenexal.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://php-fhir-tools.ardenexal.net/sdc/overview.md). Agents can also search and read this documentation through its [MCP server](https://php-fhir-tools.ardenexal.net/~gitbook/mcp).

# Overview

Populate QuestionnaireResponses and extract FHIR resources for SDC Questionnaires.

The SDC component implements [Structured Data Capture](https://build.fhir.org/ig/HL7/sdc/) operations for FHIR PHP model objects. Today it delivers two operations: `QuestionnaireResponse/$extract` — turning a completed `QuestionnaireResponse` into FHIR resources per the [SDC extraction operation](https://build.fhir.org/ig/HL7/sdc/en/extraction.html) — and `Questionnaire/$populate` — pre-filling a `QuestionnaireResponse` from launch context per the [SDC populate operation](https://build.fhir.org/ig/HL7/sdc/en/populate.html). It supports R4, R4B, and R5.

## `$extract` quick start

```php
use Ardenexal\FHIRTools\Component\Sdc\ExtractContext;
use Ardenexal\FHIRTools\Component\Sdc\FHIRQuestionnaireResponseExtractService;
use Ardenexal\FHIRTools\Component\Serialization\FhirVersion;

$service = new FHIRQuestionnaireResponseExtractService();

$result = $service->extract($questionnaireResponse, new ExtractContext(
    fhirVersion:    FhirVersion::R4, // output model namespace (R4 / R4B / R5)
    questionnaire:  $questionnaire,  // the source Questionnaire carrying the extract directives
    emitProvenance: false,           // opt-in Provenance entry
));

$bundle = $result->getResource(); // a transaction Bundle (always)
$issues = $result->getIssues();   // an OperationOutcome, or null when nothing to report
```

## `$populate` quick start

```php
use Ardenexal\FHIRTools\Component\Sdc\BundlePopulationDataProvider;
use Ardenexal\FHIRTools\Component\Sdc\FHIRQuestionnairePopulateService;
use Ardenexal\FHIRTools\Component\Sdc\PopulateContext;
use Ardenexal\FHIRTools\Component\Serialization\FhirVersion;

$service = new FHIRQuestionnairePopulateService();

$result = $service->populate($questionnaire, new PopulateContext(
    fhirVersion:            FhirVersion::R4,                       // output model namespace (R4 / R4B / R5)
    launchContextResources: ['patient' => $patient],              // bound as FHIRPath %patient, …
    subject:                'Patient/123',                         // sets QuestionnaireResponse.subject (optional)
    dataProvider:           new BundlePopulationDataProvider($dataBundle), // observation-based (optional)
));

$response = $result->getResponse(); // a QuestionnaireResponse (status: in-progress)
$issues   = $result->getIssues();   // an OperationOutcome, or null when nothing to report
```

## What it does

* **Three extraction methods** — observation-based (R4 only), definition-based (`definitionExtract` / `definitionExtractValue`), and template-based (`#contained` templates). A single Questionnaire may mix all three; results merge into one Bundle.
* **Transaction-Bundle output** — the payload is always a `transaction` Bundle. Each `entry.request` is `POST Type` (no logical id) or `PUT Type/id` (id present) — extraction is create/update only, never delete.
* **Graceful degradation** — an empty extraction yields an empty Bundle plus an `information` `OperationOutcome`; a malformed expression warns and skips that entry rather than failing the run.
* **Opt-in `Provenance`** — pass `emitProvenance: true` to add a `Provenance` entry linking the extracted resources back to the source `QuestionnaireResponse`.

`Questionnaire/$populate` pre-fills a `QuestionnaireResponse` from a `Questionnaire`'s SDC population directives — expression-based (`launchContext` + `initialExpression`, `variable` chains, `itemPopulationContext`) and observation-based (`observationLinkPeriod`). Call `FHIRQuestionnairePopulateService::populate($questionnaire, new PopulateContext(...))`; the returned `PopulateResult` carries the generated `QuestionnaireResponse` (status `in-progress`) plus an optional `OperationOutcome`. Population is offline-first and FHIRPath-only — the component README is canonical for the supported-mechanisms table and exclusions.

## Reference

The component README is canonical for the full supported-methods table, output contract, and current exclusions (StructureMap-based extraction, `templateExtractBundle`, live persistence, and access control): see `src/Component/Sdc/README.md`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://php-fhir-tools.ardenexal.net/sdc/overview.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
