Skip to content

Commit 97ca3db

Browse files
updates
1 parent 990fad3 commit 97ca3db

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed
Lines changed: 46 additions & 0 deletions
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/search/criteria_reference/product_search_criteria.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ Search Criterion let you filter product by specific attributes, for example, col
4242
|[RangeMeasurementAttributeMinimum](rangemeasurementattributeminimum_criterion.md)|Minimum value of product's measurement attribute|
4343
|[SelectionAttribute](selectionattribute_criterion.md)|Value of product's selection attribute|
4444
|[SimpleMeasurementAttribute](simplemeasurementattribute_criterion.md)|Value of product's measurement attribute|
45-
45+
|[SymbolAttribute](symbolattribute_criterion.md)|Value of product's symbol attribute|
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
description: SymbolAttribute Criterion
3+
---
4+
5+
# SymbolAttributeCriterion
6+
7+
The `SymbolAttribute` Search Criterion searches for products by [symbol attribute](symbol_attribute_type.md).
8+
9+
## Arguments
10+
11+
- `identifier` - identifier of the format
12+
- `value` - array with the values to search for
13+
14+
## Example
15+
16+
### PHP
17+
18+
``` php
19+
<?php
20+
21+
use Ibexa\Contracts\ProductCatalog\Values\Product\ProductQuery;
22+
use Ibexa\Contracts\ProductCatalogSymbolAttribute\Search\Criterion\SymbolAttribute;
23+
24+
$query = new ProductQuery();
25+
$query->setFilter(new SymbolAttribute('ean', ['5023920187205']));
26+
/** @var \Ibexa\Contracts\ProductCatalog\ProductServiceInterface $productService*/
27+
$results = $productService->findProducts($query);
28+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ nav:
358358
- Create product code generator: pim/create_product_code_generator.md
359359
- Create custom catalog filter: pim/create_custom_catalog_filter.md
360360
- Create custom name schema: pim/create_custom_name_schema_strategy.md
361+
- Use symbol attribute type: pim/symbol_attribute_type.md
361362
- Add remote PIM support: pim/add_remote_pim_support.md
362363
- Commerce:
363364
- Commerce: commerce/commerce.md

0 commit comments

Comments
 (0)