<commit_before><?php

/*
 * This file is part of the kompakt/b3d package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 */

namespace Kompakt\B3d\Canonical\Repository;

use Kompakt\B3d\Canonical\Entity\Product;

class ProductRepository
{
    protected $products = array();

    public function add($product)
    {
        $this->addProduct($product);
    }

    public function getAll()
    {
        return $this->products;
    }

    protected function addProduct(Product $product)
    {
        $this->products[] = $product;
    }
}<commit_msg>Find products by barcode and catalog number<commit_after><?php

/*
 * This file is part of the kompakt/b3d package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 */

namespace Kompakt\B3d\Canonical\Repository;

use Kompakt\B3d\Canonical\Entity\Product;

class ProductRepository
{
    protected $products = array();
    protected $barcodes = array();
    protected $catalogNumbers = array();

    public function add($product)
    {
        $this->addProduct($product);
    }

    public function getAll()
    {
        return $this->products;
    }

    public function getAllByBarcode($barcode)
    {
        $barcode = $this->prepareField($barcode);

        return
            (array_key_exists($barcode, $this->barcodes))
            ? $this->barcodes[$barcode]
            : array()
        ;
    }

    public function getAllByCatalogNumber($catalogNumber)
    {
        $catalogNumber = $this->prepareField($catalogNumber);

        return
            (array_key_exists($catalogNumber, $this->catalogNumbers))
            ? $this->catalogNumbers[$catalogNumber]
            : array()
        ;
    }

    protected function addProduct(Product $product)
    {
        $this->products[] = $product;
        $this->addByBarcode($product);
        $this->addByCatalogNumber($product);
    }

    protected function addByBarcode(Product $product)
    {
        $barcode = $this->prepareField($product->getBarcode());

        if (!array_key_exists($barcode, $this->barcodes))
        {
            $this->barcodes[$barcode] = array();
        }

        $this->barcodes[$barcode][] = $product;
    }

    protected function addByCatalogNumber(Product $product)
    {
        $catalogNumbers = $this->prepareField($product->getBarcode());

        if (!array_key_exists($catalogNumbers, $this->catalogNumbers))
        {
            $this->catalogNumbers[$catalogNumbers] = array();
        }

        $this->catalogNumbers[$catalogNumbers][] = $product;
    }

    protected function prepareField($s)
    {
        return strtolower(trim($s));
    }
}
{'fraction_non_alphanumeric': 0.1357976653696498, 'fraction_numerical': 0.0023346303501945525, 'mean_word_length': 2.5560165975103732, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2132077', 'n_tokens_mistral': 779, 'n_tokens_neox': 760, 'n_words': 137}