Documentation

SearchForm
in package

Embodies an API call we are in the process of building. This gets started with Prismic\Api.form, then you can chain instance method calls to build your query, and the query gets launched with Prismic\SearchForm.submit.

For instance, here's how you query all of the repository: $result = $api->form('everything')->ref($ref)->submit()

And here's an example of a more complex query: $result = $api->form('products') ->query('[[:d = any(document.tags, ["Featured"])]]')->pageSize(10)->page(2)->ref($ref)->submit()

Note that setting the ref is mandatory, or your submit call will fail.

Note also that SearchForm objects are immutable; the chainable methods all return new SearchForm objects.

Table of Contents

$cache  : CacheInterface
Cache Instance
$client  : ClientInterface
Http Client
$data  : array<string|int, mixed>
The parameters we're getting ready to submit
$form  : Form
The REST form we're querying on in the API
__construct()  : mixed
Constructs a SearchForm object
after()  : self
Set the after parameter: the id of the document to start the results from (excluding that document).
count()  : int|null
Get the result count for this form
fetch()  : self
Set the fetch parameter: restrict the fields to retrieve for a document
fetchLinks()  : self
Set the fetchLinks parameter: additional fields to retrieve for DocumentLink
getData()  : array<string|int, mixed>
Get the parameters we're about to submit.
graphQuery()  : self
Set the graphQuery parameter: GraphQL syntax based to select fields
isCached()  : bool
Checks if the results for this form are already cached
lang()  : self
Set the language for the query documents.
orderings()  : self
Set the query's ordering, setting in what order the documents must be retrieved.
page()  : self
Set the query result page number, for the pagination.
pageSize()  : self
Set the query's page size, for the pagination.
query()  : self
Set query predicates You can provide a single string, or one or multiple Predicate instances to build an "AND" query
ref()  : self
Set the repository ref to query at
set()  : self
Sets a value for a given parameter. For instance: set('orderings', '[product.price]'), or set('page', 2).
submit()  : stdClass
Submit the current API call, and unmarshalls the result into PHP objects.
url()  : string
Get the URL for this form
assertValidQueryParameters()  : void
Assert that the parameters used for a query contain either a single string, or an array of Predicates
submitRaw()  : stdClass
Performs the actual submit call, without the unmarshalling.

Properties

$client

Http Client

private ClientInterface $client

$data

The parameters we're getting ready to submit

private array<string|int, mixed> $data

Methods

__construct()

Constructs a SearchForm object

public __construct(ClientInterface $httpClient, CacheInterface $cache, Form $form, array<string|int, mixed> $data) : mixed
Parameters
$httpClient : ClientInterface

An HTTP Client for sending Requests

$cache : CacheInterface

A cache for storing responses

$form : Form

the REST form we're querying on in the API

$data : array<string|int, mixed>

the parameters we're getting ready to submit

Return values
mixed

after()

Set the after parameter: the id of the document to start the results from (excluding that document).

public after(string $documentId) : self
Parameters
$documentId : string
Return values
self

count()

Get the result count for this form

public count() : int|null

This uses a copy of the SearchForm with a page size of 1 (the smallest allowed) since all we care about is one of the returned non-result fields.

Return values
int|null

fetch()

Set the fetch parameter: restrict the fields to retrieve for a document

public fetch(array<string|int, string> ...$fields) : self

Pass multiple string arguments or an array of strings to unpack with the splat operator

Parameters
$fields : array<string|int, string>
Return values
self

Set the fetchLinks parameter: additional fields to retrieve for DocumentLink

public fetchLinks(array<string|int, string> ...$fields) : self

Pass multiple string arguments or an array of strings to unpack with the splat operator

Parameters
$fields : array<string|int, string>
Return values
self

getData()

Get the parameters we're about to submit.

public getData() : array<string|int, mixed>
Return values
array<string|int, mixed>

graphQuery()

Set the graphQuery parameter: GraphQL syntax based to select fields

public graphQuery(string $query) : self
Parameters
$query : string
Return values
self

isCached()

Checks if the results for this form are already cached

public isCached() : bool
Return values
bool

lang()

Set the language for the query documents.

public lang(string $lang) : self
Parameters
$lang : string
Return values
self

orderings()

Set the query's ordering, setting in what order the documents must be retrieved.

public orderings(string ...$fields) : self
Parameters
$fields : string
Return values
self

page()

Set the query result page number, for the pagination.

public page(int $page) : self
Parameters
$page : int
Return values
self

pageSize()

Set the query's page size, for the pagination.

public pageSize(int $pageSize) : self
Parameters
$pageSize : int
Return values
self

query()

Set query predicates You can provide a single string, or one or multiple Predicate instances to build an "AND" query

public query(mixed ...$params) : self
Parameters
$params : mixed
Return values
self

ref()

Set the repository ref to query at

public ref(string|Ref $ref) : self
Parameters
$ref : string|Ref

the ref we wish to query on, or its ID.

Return values
self

set()

Sets a value for a given parameter. For instance: set('orderings', '[product.price]'), or set('page', 2).

public set(string $key, string|int $value) : self

Checks that the parameter is expected in the RESTful form before allowing to add it.

Parameters
$key : string

the name of the parameter

$value : string|int

the value of the parameter

Tags
throws
ExceptionInterface
Return values
self

A clone of the SearchForm object with the new parameter added

submit()

Submit the current API call, and unmarshalls the result into PHP objects.

public submit() : stdClass
Return values
stdClass

url()

Get the URL for this form

public url() : string
Return values
string

assertValidQueryParameters()

Assert that the parameters used for a query contain either a single string, or an array of Predicates

private assertValidQueryParameters(array<string|int, mixed> $params) : void
Parameters
$params : array<string|int, mixed>
Tags
throws
InvalidArgumentException
Return values
void

submitRaw()

Performs the actual submit call, without the unmarshalling.

private submitRaw() : stdClass
Tags
throws
RuntimeException

if the Form type is not supported or the Response body is invalid

throws
RequestFailureException

if something went wrong retrieving data from the API

Return values
stdClass

Unserialized JSON Response

Search results