-
Notifications
You must be signed in to change notification settings - Fork 84
IBX-9147: Symbol attribute type described in Developer Documentation - v4.6 #2525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
julitafalcondusza
wants to merge
12
commits into
4.6
Choose a base branch
from
IBX-9147
base: 4.6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0ff312b
Symbol attribute type described
julitafalcondusza 60fdb8b
Fixes after review
julitafalcondusza b037ab1
PHP CS Fixes
julitafalcondusza 528bf4e
Resolved conflicts in product search criteria
mnocon 4dec593
Added composer dependency
mnocon 4db77ba
fixes
julitafalcondusza dc91ecb
review fixes
julitafalcondusza c83c9a2
PHP & JS CS Fixes
julitafalcondusza a7f9a21
symbol_attribute_type.md moved to Attributes folder
julitafalcondusza a139c9c
fixes; mysql added
julitafalcondusza eb06677
fix in the table
julitafalcondusza 2115481
fix
julitafalcondusza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
code_samples/back_office/search/src/Query/SymbolAttributeTypeQuery.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php declare(strict_types=1); | ||
|
||
use Ibexa\Contracts\ProductCatalog\Values\Product\ProductQuery; | ||
use Ibexa\Contracts\ProductCatalogSymbolAttribute\Search\Criterion\SymbolAttribute; | ||
|
||
$query = new ProductQuery(); | ||
$query->setFilter(new SymbolAttribute('ean', ['5023920187205'])); | ||
/** @var \Ibexa\Contracts\ProductCatalog\ProductServiceInterface $productService */ | ||
$results = $productService->findProducts($query); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\PIM\Symbol\Format\Checksum; | ||
|
||
use Ibexa\Contracts\ProductCatalog\Values\AttributeDefinitionInterface; | ||
use Ibexa\Contracts\ProductCatalogSymbolAttribute\Value\ChecksumInterface; | ||
|
||
final class LuhnChecksum implements ChecksumInterface | ||
{ | ||
public function validate(AttributeDefinitionInterface $attributeDefinition, string $value): bool | ||
{ | ||
$digits = $this->getDigits($value); | ||
|
||
$count = count($digits); | ||
$total = 0; | ||
for ($i = $count - 2; $i >= 0; $i -= 2) { | ||
$digit = $digits[$i]; | ||
if ($i % 2 === 0) { | ||
$digit *= 2; | ||
} | ||
|
||
$total += $digit > 9 ? $digit - 9 : $digit; | ||
} | ||
|
||
$checksum = $digits[$count - 1]; | ||
|
||
return $total + $checksum === 0; | ||
} | ||
|
||
/** | ||
* Returns an array of digits from the given value (skipping any formatting characters). | ||
* | ||
* @return int[] | ||
*/ | ||
private function getDigits(string $value): array | ||
{ | ||
$chars = array_filter( | ||
str_split($value), | ||
static fn (string $char): bool => $char !== '-' | ||
); | ||
|
||
return array_map('intval', array_values($chars)); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
code_samples/symbol_attribute/config/mysql/symbol_attribute.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
CREATE TABLE ibexa_product_specification_attribute_symbol (id INT NOT NULL, value VARCHAR(160) DEFAULT NULL, INDEX ibexa_product_specification_attribute_symbol_value_idx (value), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_520_ci` ENGINE = InnoDB; | ||
ALTER TABLE ibexa_product_specification_attribute_symbol ADD CONSTRAINT ibexa_product_specification_attribute_symbol_fk FOREIGN KEY (id) REFERENCES ibexa_product_specification_attribute (id) ON UPDATE CASCADE ON DELETE CASCADE |
3 changes: 3 additions & 0 deletions
3
code_samples/symbol_attribute/config/postgresql/symbol_attribute.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
CREATE TABLE ibexa_product_specification_attribute_symbol (id INT NOT NULL, value VARCHAR(160) DEFAULT NULL, PRIMARY KEY(id)); | ||
CREATE INDEX ibexa_product_specification_attribute_symbol_value_idx ON ibexa_product_specification_attribute_symbol (value); | ||
ALTER TABLE ibexa_product_specification_attribute_symbol ADD CONSTRAINT ibexa_product_specification_attribute_symbol_fk FOREIGN KEY (id) REFERENCES ibexa_product_specification_attribute (id) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
--- | ||
Check warning on line 1 in docs/pim/attributes/symbol_attribute_type.md
|
||
description: Create a symbol attribute type that enables for the efficient representation of string-based values while enforcing their format in product specifications. | ||
edition: lts-update | ||
--- | ||
|
||
# Symbol attribute type | ||
|
||
The Symbol attribute type LTS update enables the efficient representation of string-based data and enforces their format in product specifications. | ||
|
||
This feature allows you to store standard product identifiers (such as EAN or ISBN) in the [Product Information Management](pim_guide.md) system. | ||
|
||
## Installation | ||
|
||
### Download the bundle | ||
|
||
To get the most recent stable version of this bundle, open a command terminal, navigate to your project directory, and run the following command: | ||
|
||
``` bash | ||
composer require ibexa/product-catalog-symbol-attribute | ||
``` | ||
|
||
### Enable the bundle | ||
|
||
Symfony Flex enables and configures the `IbexaProductCatalogSymbolAttributeBundle` automatically. | ||
If you don't use it, you can manually enable this bundle by adding the line below to the Kernel of your project: | ||
|
||
``` php | ||
// config/bundles.php | ||
|
||
return [ | ||
// ... | ||
Ibexa\Bundle\ProductCatalogSymbolAttribute\IbexaProductCatalogSymbolAttributeBundle::class => ['all' => true], | ||
// ... | ||
]; | ||
``` | ||
|
||
### Update database schema | ||
|
||
To store symbol attribute values, the `IbexaProductCatalogSymbolAttributeBundle` needs an extra table. | ||
The following SQL query can be used to build the required database structure: | ||
|
||
=== "MySQL" | ||
|
||
``` sql | ||
[[= include_file('code_samples/symbol_attribute/config/mysql/symbol_attribute.sql', glue=' ') =]] | ||
``` | ||
|
||
=== "PostgreSQL" | ||
|
||
``` sql | ||
[[= include_file('code_samples/symbol_attribute/config/postgresql/symbol_attribute.sql', glue=' ') =]] | ||
``` | ||
|
||
### Create symbol attribute definition (optional) | ||
|
||
Now, you're able to define symbol attributes at this point. | ||
|
||
To create symbol attribute definition, in the back office, go to **Product catalog** -> **Attributes**, and click **Create**. | ||
Then, choose **Symbol** attribute type. | ||
|
||
## Build-in symbol attribute formats | ||
|
||
The built-in symbol attribute formats in `ibexa/product-catalog-symbol-attribute` are listed below: | ||
|
||
| Name | Description | Example | | ||
|-----------------|-----------------|-----------------| | ||
| Generic | Accepts any string value | #FR1.2 | | ||
| Generic (alphabetic characters only) | Accepts any string value that contais only letters | ABCD | | ||
| Generic (digits only) | Accepts any string value that contais only digits | 123456 | | ||
| Generic (alphanumeric characters only) | Accepts any string value that contains only letters or digits | 2N6405G | | ||
| Generic (hexadecimal digits only) | Accepts any string value that contains only hexadecimal digits (digits or A-F characters) | DEADBEEF | | ||
| EAN-8 | European Article Number (8 characters) | 96385074 | | ||
| EAN-13 | European Article Number (13 characters) | 5023920187205 | | ||
| EAN-14 | European Article Number (14 characters) | 12345678901231 | | ||
| ISBN-10 | International Standard Book Number (10 characters) | 0-19-852663-6 | | ||
| ISBN-13 | International Standard Book Number (13 characters) | 978-1-86197-876-9 | | ||
|
||
!!! caution | ||
|
||
Maximum length of the symbol value is 160 characters. | ||
|
||
## Create custom symbol attribute format | ||
|
||
Under the `ibexa_product_catalog_symbol_attribute.formats` key, you can use configuration to create your own symbol format. | ||
|
||
See the example below: | ||
|
||
``` yaml | ||
ibexa_product_catalog_symbol_attribute: | ||
formats: | ||
manufacturer_part_number: | ||
name: 'Manufacturer Part Number' | ||
pattern: '/^[A-Z]{3}-\d{5}$/' | ||
examples: | ||
- 'RPI-14645' | ||
- 'MSS-24827' | ||
- 'SEE-15444' | ||
``` | ||
|
||
This following example specifies the format for a "Manufacturer Part Number", defined with the `manufacturer_part_number` identifier. | ||
|
||
The pattern is specified using a regular expression. | ||
According to the pattern option, the attribute value: | ||
|
||
- must be a string | ||
- begins with three capital letters (A-Z), followed by a hyphen ("-") | ||
- ends with five digits (0-9), with no other characters before or after | ||
|
||
Certain formats, such as the International Standard Book Number (ISBN-10) and the European Article Number (EAN-13), contain checksum digits and are self-validating. | ||
|
||
To validate checksum of symbol: | ||
|
||
1\. Create a class implementing the `\Ibexa\Contracts\ProductCatalogSymbolAttribute\Value\ChecksumInterface` interface. | ||
|
||
2\. Register the class as a service using the `ibexa.product_catalog.attribute.symbol.checksum` tag and specify the format identifier using the `format` attribute. | ||
|
||
See below the example implementation of checksum validation using Luhn formula: | ||
|
||
``` php | ||
[[= include_file('code_samples/pim/Symbol/Format/Checksum/LuhnChecksum.php') =]] | ||
``` | ||
|
||
Example service definition: | ||
|
||
``` yaml | ||
services: | ||
App\PIM\Symbol\Format\Checksum\LuhnChecksum: | ||
tags: | ||
- name: ibexa.product_catalog.attribute.symbol.checksum | ||
format: my_format | ||
``` | ||
The format attribute (`my_format`) is the identifier used under the `ibexa_product_catalog_symbol_attribute.formats` key. | ||
|
||
## Search for products with given symbol attribute | ||
|
||
You can use `SymbolAttribute` Search Criterion to find products by symbol attribute: | ||
|
||
For more information, see [SymbolAttribute Criterion](symbolattribute_criterion.md). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
docs/search/criteria_reference/symbolattribute_criterion.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
Check warning on line 1 in docs/search/criteria_reference/symbolattribute_criterion.md
|
||
description: SymbolAttribute Criterion | ||
edition: lts-update | ||
--- | ||
|
||
# SymbolAttributeCriterion | ||
|
||
The `SymbolAttribute` Search Criterion searches for products by [symbol attribute](symbol_attribute_type.md). | ||
|
||
## Arguments | ||
|
||
- `identifier` - identifier of the format | ||
- `value` - array with the values to search for | ||
|
||
## Example | ||
|
||
### PHP | ||
|
||
``` php | ||
[[= include_file('code_samples/back_office/search/src/Query/SymbolAttributeTypeQuery.php') =]] | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.