ProductGroupsCollection
extends Collection
in package
Table of Contents
Properties
- $alias : string|null
- $aliasBase : string|null
- $blogId : int|null
- $brand : string|null
- $country : string|null
- $fields : array<string|int, mixed>|null
- $locale : string|null
- $useBrand : bool
- $useCountry : bool
- $useLocale : bool
Methods
- __wakeup() : mixed
- createNewCollection() : string|false
- Creates a new collection in TypeSense if it doesn't already exist.
- deleteCollection() : void
- Deletes the current TypeSense collection and its associated alias.
- deleteDocument() : bool
- Deletes a document from the collection by its ID.
- ensureCollectionExists() : void
- Ensures that the collection exists.
- fetchData() : array<string|int, mixed>
- Fetches data from the desired source based on the collection's locale and country.
- fetchSingle() : array<string|int, mixed>|false
- Fetches data for single document from the desired source based on the collection's locale and country.
- getAlias() : string
- Retrieves the current alias used for the collection.
- getBlogId() : int|null
- Get the blog ID associated with the collection.
- getCollectionMeta() : array<string|int, mixed>
- Retrieves the metadata of the TypeSense collection.
- getCountry() : string|null
- Get the country code used by the collection.
- getDocument() : array<string|int, mixed>|false
- Retrieves a document from TypeSense by its ID.
- getDocuments() : array<string|int, mixed>
- Retrieves all documents from the TypeSense collection.
- getInstance() : static
- Returns the singleton instance of the Collection class based on the given blog ID.
- getLocale() : string|null
- Get the locale value used by the collection.
- getLocalizationString() : mixed
- getRealCollectionName() : string|false
- Retrieves the real collection name associated with the current alias.
- isDocumentExists() : bool
- Checks if a document with the given ID exists in the collection.
- populateCollection() : bool
- Populates the source data into a TypeSense collection for the current alias.
- populateDocument() : bool
- Populates a single document in TypeSense by fetching data from the desired source and converting it into the required format.
- rePopulateDocument() : bool
- Attempts to repopulate a document in the collection based on the given ID.
- saveDataToCollection() : bool
- Saves data to an existing TypeSense collection or creates a new collection if it doesn't exist.
- saveDataToNewCollection() : string|false
- Saves data to a newly created TypeSense collection.
- search() : array<string|int, mixed>
- Executes a search query on TypeSense and returns a single page of results.
- searchAll() : array<string|int, mixed>
- Executes a search query and fetches all results from TypeSense.
- switchCollectionAlias() : void
- Switches the collection alias to point to a new collection.
- transformData() : array<string|int, mixed>
- Transforms the input data structure into the format required by TypeSense.
- updateDocument() : bool
- Updates a document in the specified collection with the provided data.
- __clone() : mixed
- __construct() : mixed
- Constructor for the specific TypeSense collection implementation.
- generateAlias() : void
- Generates an alias based on the base alias and optionally appends locale and country.
- getGroupProducts() : array<string|int, mixed>
- Retrieves products belonging to a specific group based on the group ID and group name.
- getLocalizedInstance() : static
- Retrieves a localized singleton instance of the class.
- getNewCollectionName() : string|false
- Generates a name for a new collection with a datetime suffix.
- initProperties() : void
- Initializes the internal configuration of the collection instance.
Properties
$alias
protected
string|null
$alias
= null
$aliasBase
protected
string|null
$aliasBase
= null
$blogId
protected
int|null
$blogId
= null
$brand
protected
string|null
$brand
= null
$country
protected
string|null
$country
= null
$fields
protected
array<string|int, mixed>|null
$fields
= null
$locale
protected
string|null
$locale
= null
$useBrand
protected
bool
$useBrand
= true
$useCountry
protected
bool
$useCountry
= true
$useLocale
protected
bool
$useLocale
= true
Methods
__wakeup()
public
__wakeup() : mixed
createNewCollection()
Creates a new collection in TypeSense if it doesn't already exist.
public
createNewCollection() : string|false
This method first checks if a collection with the given name already exists.
If not, it creates a new collection using the defined schema.
The schema can either be predefined in $this->fields or default to a generic schema
with automatic type detection.
If the collection is successfully created, a log message is recorded. If the collection already exists, an error is logged.
Return values
string|false —The name of the created collection, or false if the collection already exists.
deleteCollection()
Deletes the current TypeSense collection and its associated alias.
public
deleteCollection() : void
This method performs the following actions:
- Deletes the collection identified by the current collection name, if it exists.
- Deletes the alias associated with the current collection, if it exists.
Logs success messages for both the collection and alias deletion operations.
deleteDocument()
Deletes a document from the collection by its ID.
public
deleteDocument(string $id) : bool
This function retrieves the collection alias and attempts to delete the document with the specified ID. If the deletion is successful, it returns true. If an exception occurs during the process, the exception is logged, and the function returns false.
Parameters
- $id : string
-
The ID of the document to be deleted.
Return values
bool —True if the document is successfully deleted, false on failure.
ensureCollectionExists()
Ensures that the collection exists.
public
ensureCollectionExists() : void
This method checks if the current collection exists. If it does not, it creates a new collection and switches the alias to point to the newly created collection.
fetchData()
Fetches data from the desired source based on the collection's locale and country.
public
fetchData([int $page = 1 ]) : array<string|int, mixed>
The data is fetched in a paginated manner to optimize performance and manage large datasets.
Parameters
- $page : int = 1
-
The page number from which to fetch data. Defaults to 1.
Return values
array<string|int, mixed> —The fetched data for the given page.
fetchSingle()
Fetches data for single document from the desired source based on the collection's locale and country.
public
fetchSingle(string $id) : array<string|int, mixed>|false
Parameters
- $id : string
-
The id of document to fetch data for.
Return values
array<string|int, mixed>|false —The fetched data as array or false if response is empty
getAlias()
Retrieves the current alias used for the collection.
public
getAlias() : string
This method returns the alias associated with the collection, which is used to reference the collection in TypeSense.
Return values
string —The alias of the collection.
getBlogId()
Get the blog ID associated with the collection.
public
getBlogId() : int|null
Return values
int|null —The blog ID or null if not set.
getCollectionMeta()
Retrieves the metadata of the TypeSense collection.
public
getCollectionMeta() : array<string|int, mixed>
This method fetches details about the TypeSense collection associated with the current alias, including schema and settings. It returns the collection information as an associative array.
Return values
array<string|int, mixed> —The collection metadata.
getCountry()
Get the country code used by the collection.
public
getCountry() : string|null
Return values
string|null —The country code (e.g., 'US', 'DE') or null if not set.
getDocument()
Retrieves a document from TypeSense by its ID.
public
getDocument(string $id[, bool $rePopulate = true ]) : array<string|int, mixed>|false
This method attempts to fetch a document from the TypeSense collection using
the provided document ID. If the document does not exist, it returns false.
In case of any other errors during the retrieval, the exception is logged,
and false is returned.
Parameters
- $id : string
-
The ID of the document to retrieve.
- $rePopulate : bool = true
-
Flag indicating whether to attempt repopulating the document if it is not found in the collection.
Return values
array<string|int, mixed>|false —The document data if found, or false if the document does not exist
or if an error occurs during retrieval.
getDocuments()
Retrieves all documents from the TypeSense collection.
public
getDocuments() : array<string|int, mixed>
This method ensures that the collection exists before fetching and exporting all documents from the TypeSense collection associated with the current alias. The data is returned as an array, where the documents are originally in JSON Lines (JSONL) format, but are decoded into an array for easier handling.
Return values
array<string|int, mixed> —The exported documents as an array, decoded from JSON Lines (JSONL) format.
getInstance()
Returns the singleton instance of the Collection class based on the given blog ID.
public
static getInstance([int|null $blogId = null ]) : static
Ensures a single instance per unique blog context by using the blog ID to derive locale, country, and brand internally. If no blog ID is provided, the current blog ID (from the active site in a multisite setup) is used.
Internally calls getLocalizedInstance() with flags that control scoping by locale and country.
Parameters
- $blogId : int|null = null
-
Optional blog identifier. If null, the current blog ID is used.
Return values
static —The singleton instance of the Collection class for the resolved context.
getLocale()
Get the locale value used by the collection.
public
getLocale() : string|null
Return values
string|null —The locale string (e.g., 'en_US') or null if not set.
getLocalizationString()
public
getLocalizationString() : mixed
getRealCollectionName()
Retrieves the real collection name associated with the current alias.
public
getRealCollectionName() : string|false
This method checks if the alias exists in TypeSense and, if it does, retrieves the real collection name that the alias points to. If the alias does not exist, it returns false.
Return values
string|false —The real collection name if the alias exists; otherwise, false.
isDocumentExists()
Checks if a document with the given ID exists in the collection.
public
isDocumentExists(string $id) : bool
This method retrieves the document from the specified collection using the provided ID. If the document is found, it returns true. If the document is not found or an exception occurs (e.g., the document does not exist), it returns false.
Parameters
- $id : string
-
The ID of the document to check.
Return values
bool —Returns true if the document exists, or false if it does not exist or an error occurs.
populateCollection()
Populates the source data into a TypeSense collection for the current alias.
public
populateCollection() : bool
The data is fetched, transformed, and processed in paginated chunks. Each chunk is saved to a new collection, and once all data is successfully stored, the alias is switched to point to the new collection.
The process logs progress and errors, ensuring that data is saved incrementally. If any data fails to save, the process stops and returns false.
Return values
bool —True on success, false on failure.
populateDocument()
Populates a single document in TypeSense by fetching data from the desired source and converting it into the required format.
public
populateDocument(string $id) : bool
Parameters
- $id : string
-
The ID of the document to populate in TypeSense.
Return values
bool —true if data was saved, false if not
rePopulateDocument()
Attempts to repopulate a document in the collection based on the given ID.
public
rePopulateDocument(string $id) : bool
This method fetches the data for the specified document ID, transforms it, and repopulates the document in the collection. If the document already exists, it will update it with the transformed data. Otherwise, it will attempt to save the data as a new document. If no valid data is found, the method returns false.
Parameters
- $id : string
-
The ID of the document to repopulate.
Return values
bool —Returns true if the document is successfully updated or saved, or false if no valid data is found or the operation fails.
saveDataToCollection()
Saves data to an existing TypeSense collection or creates a new collection if it doesn't exist.
public
saveDataToCollection(array<string|int, mixed> $data[, bool $logProgress = true ]) : bool
This method performs the following actions:
- Checks if the current collection exists.
- If the collection does not exist, creates a new one.
- Saves the provided data to the existing or newly created collection.
If you need to modify the format of the data before saving, override this method in a subclass.
Parameters
- $data : array<string|int, mixed>
-
The data to be saved to the collection in its raw format.
- $logProgress : bool = true
Return values
bool —true if the data was successfully saved; false otherwise.
saveDataToNewCollection()
Saves data to a newly created TypeSense collection.
public
saveDataToNewCollection(array<string|int, mixed> $data) : string|false
This method performs the following actions:
- Creates a new collection with a unique name.
- Saves the provided data to the newly created collection.
Returns the name of the newly created collection if the data was successfully saved.
Returns false if the data could not be saved to the new collection.
Parameters
- $data : array<string|int, mixed>
-
The data to be saved to the new collection.
Return values
string|false —The name of the newly created collection or false on failure.
search()
Executes a search query on TypeSense and returns a single page of results.
public
search(array<string|int, mixed> $query) : array<string|int, mixed>
This method interacts with the TypeSense client to perform the search operation using the provided query parameters. It returns the search results for the current page.
Parameters
- $query : array<string|int, mixed>
-
The search query parameters, including pagination details.
Return values
array<string|int, mixed> —The search results for the specified query.
searchAll()
Executes a search query and fetches all results from TypeSense.
public
searchAll(array<string|int, mixed> $query[, bool $logProgress = true ]) : array<string|int, mixed>
The method performs a paginated search, retrieving results in chunks to handle large data sets. It logs the progress and total fetched items, including the duration of the operation.
The search starts with an initial query and continues to fetch additional pages until all results are retrieved. The function logs progress at regular intervals and returns the complete set of results.
Parameters
- $query : array<string|int, mixed>
-
The search query parameters.
- $logProgress : bool = true
Return values
array<string|int, mixed> —The complete array of search results.
switchCollectionAlias()
Switches the collection alias to point to a new collection.
public
switchCollectionAlias(string $newCollectionName) : void
This method updates the alias in TypeSense to refer to the new collection specified
by $newCollectionName. It logs the switch, indicating whether the alias was
newly created or updated from an existing collection.
Parameters
- $newCollectionName : string
-
The name of the new collection to switch the alias to.
transformData()
Transforms the input data structure into the format required by TypeSense.
public
transformData(array<string|int, mixed> $inputData) : array<string|int, mixed>
Child classes must override this method to implement specific transformation logic depending on the structure of the input data.
Parameters
- $inputData : array<string|int, mixed>
-
The input data to transform.
Return values
array<string|int, mixed> —The transformed data ready to be indexed by TypeSense.
updateDocument()
Updates a document in the specified collection with the provided data.
public
updateDocument(string $id, array<string|int, mixed> $data) : bool
This method first ensures that the collection exists, and then attempts to update the document with the given ID. If the document is not found, it attempts to repopulate the document. If repopulation succeeds, the method retries the update. If any other exception is encountered during the update process, the error is logged and the method returns false.
Parameters
- $id : string
-
The ID of the document to update.
- $data : array<string|int, mixed>
-
The data to update the document with.
Return values
bool —Returns true on success, or false on failure.
__clone()
protected
__clone() : mixed
__construct()
Constructor for the specific TypeSense collection implementation.
protected
__construct([int|null $blogId = null ]) : mixed
Initializes the collection instance by resolving configuration (locale, country, brand) from the given blog ID or from the current site if none is provided. Uses the resolved values to configure collection-specific properties such as alias generation.
This constructor is typically called via the static getInstance() method.
Parameters
- $blogId : int|null = null
-
Optional blog identifier. If null, the current blog ID is used.
generateAlias()
Generates an alias based on the base alias and optionally appends locale and country.
protected
generateAlias() : void
This method constructs the alias by appending the locale and country to the base alias if
the corresponding flags are set to true. The resulting alias is stored in the alias property.
getGroupProducts()
Retrieves products belonging to a specific group based on the group ID and group name.
protected
getGroupProducts(string $groupId, string $groupName) : array<string|int, mixed>
The function performs a search query to find all products that belong to a group specified by the group ID and name. The following steps are performed:
- Constructs search parameters with a wildcard query (
*) and a filter by the given group ID and group name. - Executes the search on the ProductsCollection using the specified locale.
- Extracts and returns the relevant search hits from the result using the Helpers class.
Parameters
- $groupId : string
-
The ID of the group to filter products by.
- $groupName : string
-
The name of the group to filter products by.
Return values
array<string|int, mixed> —The array of products that belong to the specified group.
getLocalizedInstance()
Retrieves a localized singleton instance of the class.
protected
static getLocalizedInstance([bool $useLocale = true ][, bool $useCountry = true ][, int|null $blogId = null ]) : static
This method returns an instance of the class that is unique for a combination of the class name, locale, and country (if specified). It ensures that only one instance exists for a given locale and country. If an instance for the specific combination does not exist, it creates one.
Parameters
- $useLocale : bool = true
-
Determines whether the locale should be included in the instance key.
- $useCountry : bool = true
-
Determines whether the country should be included in the instance key.
- $blogId : int|null = null
Return values
static —Returns the singleton instance of the class, specific to the given locale and country.
getNewCollectionName()
Generates a name for a new collection with a datetime suffix.
protected
getNewCollectionName() : string|false
This method constructs a new collection name by appending the current datetime
(in ymdHi format) to the current alias. If the alias is not set, it returns false.
Return values
string|false —The generated collection name with datetime suffix, or false if no alias is set.
initProperties()
Initializes the internal configuration of the collection instance.
protected
initProperties(string $aliasBase[, bool $useLocale = true ][, bool $useCountry = true ][, bool $useBrand = true ][, int|null $blogId = null ]) : void
This method sets the alias base, locale, country, and brand based on the provided or resolved blog ID. It also configures whether the locale and country should be included in the generated alias.
If no blog ID is provided, the current site ID is used. Based on the final blog ID, the corresponding locale, country, and brand are fetched via ConfigProvider.
Parameters
- $aliasBase : string
-
The base string used to generate the alias.
- $useLocale : bool = true
-
Whether to include the locale in the alias.
- $useCountry : bool = true
-
Whether to include the country in the alias.
- $useBrand : bool = true
- $blogId : int|null = null
-
Optional blog ID. If null, the current blog ID is used.