Skip to content

Commit b2e93eb

Browse files
Fixes after review
1 parent d465e43 commit b2e93eb

File tree

3 files changed

+81
-81
lines changed

3 files changed

+81
-81
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\PIM\Symbol\Format\Checksum;
6+
7+
use Ibexa\Contracts\ProductCatalog\Values\AttributeDefinitionInterface;
8+
use Ibexa\Contracts\ProductCatalogSymbolAttribute\Value\ChecksumInterface;
9+
10+
final class LuhnChecksum implements ChecksumInterface
11+
{
12+
public function validate(AttributeDefinitionInterface $attributeDefinition, string $value): bool
13+
{
14+
$digits = $this->getDigits($value);
15+
16+
$count = count($digits);
17+
$total = 0;
18+
for ($i = $count - 2; $i >= 0; $i -= 2) {
19+
$digit = $digits[$i];
20+
if ($i % 2 === 0) {
21+
$digit *= 2;
22+
}
23+
24+
$total += $digit > 9 ? $digit - 9 : $digit;
25+
}
26+
27+
$checksum = $digits[$count - 1];
28+
29+
return $total + $checksum === 0;
30+
}
31+
32+
/**
33+
* Returns an array of digits from the given value (skipping any formatting characters).
34+
*
35+
* @return int[]
36+
*/
37+
private function getDigits(string $value): array
38+
{
39+
$chars = array_filter(
40+
str_split($value),
41+
static fn (string $char): bool => $char !== '-'
42+
);
43+
44+
return array_map('intval', array_values($chars));
45+
}
46+
}

docs/pim/symbol_attribute_type.md

+25-77
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,29 @@ description: Create a symbol attribute type that enables for the efficient repre
66

77
In product specifications, the symbol attribute type enables the efficient representation of string-based data and enforces their format.
88

9+
This feature allows you to store standard product identifiers (such as EAN or ISBN) in the [Product Information Management](pim_guide.md) system.
10+
911
## Installation
1012

1113
### Download the bundle
1214

1315
To get the most recent stable version of this bundle, open a command terminal, navigate to your project directory, and run the following command:
1416

1517
``` bash
16-
ibexa/product-catalog-symbol-attribute
18+
composer require ibexa/product-catalog-symbol-attribute
1719
```
1820

19-
!!! caution
20-
21-
To use this command requires you need to first install the Composer locally.
22-
For more information about Composer installation process, see [documentation](install_ibexa_dxp.md#get-composer).
23-
2421
### Enable the bundle
2522

26-
Flex enables and configures the `IbexaProductCatalogSymbolAttributeBundle` automatically.
27-
If you don't use it, you can manually enable Flex by adding the line below to the Kernel of your project:
23+
Symfony Flex enables and configures the `IbexaProductCatalogSymbolAttributeBundle` automatically.
24+
If you don't use it, you can manually enable this bundle by adding the line below to the Kernel of your project:
2825

2926
``` php
3027
// config/bundles.php
3128

3229
return [
3330
// ...
34-
Ibexa\Bundle\ProductCatalogSymbolAttribute\IbexaProductCatalogSymbolAttributeBundle => ['all' => true],
31+
Ibexa\Bundle\ProductCatalogSymbolAttribute\IbexaProductCatalogSymbolAttributeBundle::class => ['all' => true],
3532
// ...
3633
];
3734
```
@@ -41,7 +38,7 @@ return [
4138
To store symbol attribute values, the `IbexaProductCatalogSymbolAttributeBundle` needs an extra table.
4239
The following SQL query can be used to build the required database structure:
4340

44-
``` bash
41+
``` sql
4542
create table ibexa_product_specification_attribute_symbol (
4643
id int not null primary key,
4744
value varchar(255) null,
@@ -100,88 +97,39 @@ ibexa_product_catalog_symbol_attribute:
10097
- 'SEE-15444'
10198
```
10299
103-
The following example specifies the format for a "Manufacturer Part Number."
100+
This following example specifies the format for a "Manufacturer Part Number", defined with the `manufacturer_part_number` identifier.
104101

105-
According to the pattern option, the value:
102+
The pattern is specified using a regular expression.
103+
According to the pattern option, the attribute value:
106104

107105
- must be a string
108-
- uses a regular expression
109-
- begins with three capital letters
110-
- ends with five numbers
106+
- begins with three capital letters (A-Z), followed by a hyphen ("-")
107+
- ends with five numbers (0-9), with no other characters before or after
111108

112109
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.
113110

114111
To validate checksum of symbol:
115112

116-
1\. Implement `\Ibexa\Contracts\ProductCatalogSymbolAttribute\Value\ChecksumInterface`:
117-
118-
``` php
119-
<?php
120-
121-
declare(strict_types=1);
113+
1\. Create a class implementing the `\Ibexa\Contracts\ProductCatalogSymbolAttribute\Value\ChecksumInterface` interface.
122114

123-
namespace Ibexa\Contracts\ProductCatalogSymbolAttribute\Value;
115+
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.
124116

125-
use Ibexa\Contracts\ProductCatalog\Values\AttributeDefinitionInterface;
117+
See below the example implementation of checksum validation using Luhn formula:
126118

127-
interface ChecksumInterface
128-
{
129-
public function validate(AttributeDefinitionInterface $attributeDefinition, string $value): bool;
130-
}
119+
``` php
120+
[[= include_file('code_samples/pim/Symbol/Format/Checksum/LuhnChecksum.php') =]]
131121
```
132122

133-
2\. Register service with `ibexa.product_catalog.attribute.symbol.checksum` tag and format attribute.
123+
Example service definition:
134124

135-
See below the example implementation of checksum validation using Luhn formula:
136-
137-
``` php
138-
<?php
139-
140-
declare(strict_types=1);
141-
142-
namespace App\PIM\Symbol\Format\Checksum;
143-
144-
use Ibexa\Contracts\ProductCatalog\Values\AttributeDefinitionInterface;
145-
use Ibexa\Contracts\ProductCatalogSymbolAttribute\Value\ChecksumInterface;
146-
147-
final class LuhnChecksum implements ChecksumInterface
148-
{
149-
public function validate(AttributeDefinitionInterface $attributeDefinition, string $value): bool
150-
{
151-
$digits = $this->getDigits($value);
152-
153-
$count = count($digits);
154-
$total = 0;
155-
for ($i = $count - 2; $i >= 0; $i -= 2) {
156-
$digit = $digits[$i];
157-
if ($i % 2 === 0) {
158-
$digit *= 2;
159-
}
160-
161-
$total += $digit > 9 ? $digit - 9 : $digit;
162-
}
163-
164-
$checksum = $digits[$count - 1];
165-
166-
return $total + $checksum === 0;
167-
}
168-
169-
/**
170-
* Returns an array of digits from the given value (skipping any formatting characters).
171-
*
172-
* @return int[]
173-
*/
174-
private function getDigits(string $value): array
175-
{
176-
$chars = array_filter(
177-
str_split($value),
178-
static fn (string $char): bool => $char !== '-'
179-
);
180-
181-
return array_map('intval', array_values($chars));
182-
}
183-
}
125+
``` yaml
126+
services:
127+
App\PIM\Symbol\Format\Checksum\LuhnChecksum:
128+
tags:
129+
- name: ibexa.product_catalog.attribute.symbol.checksum
130+
format: my_format
184131
```
132+
The format attribute (`my_format`) is the identifier used under the `ibexa_product_catalog_symbol_attribute.formats` key.
185133

186134
## Search for products with given symbol attribute
187135

docs/search/criteria_reference/symbolattribute_criterion.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@ description: SymbolAttribute Criterion
44

55
# SymbolAttributeCriterion
66

7-
The `SymbolAttribute` Search Criterion searches for products by symbol attribute.
7+
The `SymbolAttribute` Search Criterion searches for products by [symbol attribute](symbol_attribute_type.md).
88

99
## Arguments
1010

11-
- `value` - string representing the attribute value
11+
- `identifier` - identifier of the format
12+
- `value` - array with the values to search for
1213

1314
## Example
1415

1516
### PHP
1617

1718
``` php
19+
<?php
20+
21+
use Ibexa\Contracts\ProductCatalog\Values\Product\ProductQuery;
22+
use Ibexa\Contracts\ProductCatalogSymbolAttribute\Search\Criterion\SymbolAttribute;
23+
1824
$query = new ProductQuery();
19-
$query->setFilter(new SymbolAttribute('ean', '5023920187205'));
20-
// ...
25+
$query->setFilter(new SymbolAttribute('ean', ['5023920187205']));
26+
/** @var \Ibexa\Contracts\ProductCatalog\ProductServiceInterface $productService*/
2127
$results = $productService->findProducts($query);
2228
```

0 commit comments

Comments
 (0)