<commit_before><?php

namespace BoldApps\ShopifyToolkit\Services;

use BoldApps\ShopifyToolkit\Models\Metafield as ShopifyMetafield;
use BoldApps\ShopifyToolkit\Models\Product as ShopifyProduct;
use BoldApps\ShopifyToolkit\Models\Product as ProductModel;
use BoldApps\ShopifyToolkit\Models\Variant as VariantModel;
use BoldApps\ShopifyToolkit\Models\Option as OptionModel;
use BoldApps\ShopifyToolkit\Models\Image as ImageModel;
use BoldApps\ShopifyToolkit\Services\Client as ShopifyClient;
use BoldApps\ShopifyToolkit\Services\Variant as VariantService;
use BoldApps\ShopifyToolkit\Services\Option as OptionService;
use BoldApps\ShopifyToolkit\Services\Image as ImageService;
use BoldApps\ShopifyToolkit\Services\Metafield as MetafieldService;
use Illuminate\Support\Collection;

/**
 * Class Product.
 */
class Product extends CollectionEntity
{
    /**
     * @var Variant
     */
    protected $variantService;
    /**
     * @var Option
     */
    protected $optionService;
    /**
     * @var Image
     */
    protected $imageService;

    /**
     * @var Metafield
     */
    protected $metafieldService;

    /**
     * @var array
     */
    protected $unserializationExceptions = [
        'variants' => 'unserializeVariants',
        'options' => 'unserializeOptions',
        'image' => 'unserializeImage',
        'images' => 'unserializeImages',
        'metafields' => 'unserializeMetafields',
    ];

    /**
     * @var array
     */
    protected $serializationExceptions = [
        'variants' => 'serializeVariants',
        'options' => 'serializeOptions',
        'image' => 'serializeImage',
        'images' => 'serializeImages',
        'metafields' => 'serializeMetafields',
    ];

    /**
     * Product constructor.
     *
     * @param Client  $client
     * @param Variant $variantService
     * @param Option  $optionService
     * @param Image   $imageService
     * @param MetafieldService $metafieldService
     */
    public function __construct(
        ShopifyClient $client,
        VariantService $variantService,
        OptionService $optionService,
        ImageService $imageService,
        MetafieldService $metafieldService
    ) {
        $this->variantService = $variantService;
        $this->optionService = $optionService;
        $this->imageService = $imageService;
        $this->metafieldService = $metafieldService;
        parent::__construct($client);
    }

    /**
     * @param ShopifyProduct $product
     * @param bool           $publish
     *
     * @return object
     */
    public function create(ShopifyProduct $product, $publish = true)
    {
        $serializedModel = ['product' => array_merge($this->serializeModel($product), ['published' => $publish])];

        $raw = $this->client->post('admin/products.json', [], $serializedModel);

        return $this->unserializeModel($raw['product'], ShopifyProduct::class);
    }

    /**
     * @param $id
     * @param $filter
     *
     * @return ShopifyProduct
     */
    public function getById($id, $filter = [])
    {
        $raw = $this->client->get("admin/products/$id.json", $filter);

        return $this->unserializeModel($raw['product'], ShopifyProduct::class);
    }

    /**
     * @param int   $page
     * @param int   $limit
     * @param array $filter
     *
     * @return Collection
     */
    public function getAll($page = 1, $limit = 50, $filter = [])
    {
        $raw = $this->client->get('admin/products.json', array_merge(['page' => $page, 'limit' => $limit], $filter));

        $products = array_map(function ($product) {
            return $this->unserializeModel($product, ShopifyProduct::class);
        }, $raw['products']);

        return new Collection($products);
    }

    /**
     * @param $parms
     *
     * @return \Illuminate\Support\Collection
     */
    public function getByParams($parms)
    {
        $raw = $this->client->get('admin/products.json', $parms);

        $products = array_map(function ($product) {
            return $this->unserializeModel($product, ShopifyProduct::class);
        }, $raw['products']);

        return new Collection($products);
    }

    /**
     * @param $productIds
     * @param array $filter
     *
     * @return Collection
     */
    public function getByProductIdArray($productIds, $filter = [])
    {
        $products = [];
        $limit = 250;

        for ($i = 0; $i < ceil(count($productIds) / $limit); ++$i) {
            $sliced_ids = implode(',', array_slice($productIds, ($i * $limit), $limit));

            $params = array_merge(
                $filter,
                [
                    'ids' => $sliced_ids,
                    'limit' => $limit,
                ]
            );

            $shopifyProductsBatch = $this->getByParams($params);

            /** @var ShopifyProduct $shopifyProduct */
            foreach ($shopifyProductsBatch as $shopifyProduct) {
                $products[] = $shopifyProduct;
            }
        }

        return new Collection($products);
    }

    /**
     * @param ShopifyProduct $product
     *
     * @return object
     */
    public function update(ShopifyProduct $product)
    {<commit_msg>Fixing up get by product id array (#51)<commit_after><?php

namespace BoldApps\ShopifyToolkit\Services;

use BoldApps\ShopifyToolkit\Models\Metafield as ShopifyMetafield;
use BoldApps\ShopifyToolkit\Models\Product as ShopifyProduct;
use BoldApps\ShopifyToolkit\Models\Product as ProductModel;
use BoldApps\ShopifyToolkit\Models\Variant as VariantModel;
use BoldApps\ShopifyToolkit\Models\Option as OptionModel;
use BoldApps\ShopifyToolkit\Models\Image as ImageModel;
use BoldApps\ShopifyToolkit\Services\Client as ShopifyClient;
use BoldApps\ShopifyToolkit\Services\Variant as VariantService;
use BoldApps\ShopifyToolkit\Services\Option as OptionService;
use BoldApps\ShopifyToolkit\Services\Image as ImageService;
use BoldApps\ShopifyToolkit\Services\Metafield as MetafieldService;
use Illuminate\Support\Collection;

/**
 * Class Product.
 */
class Product extends CollectionEntity
{
    /**
     * @var Variant
     */
    protected $variantService;
    /**
     * @var Option
     */
    protected $optionService;
    /**
     * @var Image
     */
    protected $imageService;

    /**
     * @var Metafield
     */
    protected $metafieldService;

    /**
     * @var array
     */
    protected $unserializationExceptions = [
        'variants' => 'unserializeVariants',
        'options' => 'unserializeOptions',
        'image' => 'unserializeImage',
        'images' => 'unserializeImages',
        'metafields' => 'unserializeMetafields',
    ];

    /**
     * @var array
     */
    protected $serializationExceptions = [
        'variants' => 'serializeVariants',
        'options' => 'serializeOptions',
        'image' => 'serializeImage',
        'images' => 'serializeImages',
        'metafields' => 'serializeMetafields',
    ];

    /**
     * Product constructor.
     *
     * @param Client  $client
     * @param Variant $variantService
     * @param Option  $optionService
     * @param Image   $imageService
     * @param MetafieldService $metafieldService
     */
    public function __construct(
        ShopifyClient $client,
        VariantService $variantService,
        OptionService $optionService,
        ImageService $imageService,
        MetafieldService $metafieldService
    ) {
        $this->variantService = $variantService;
        $this->optionService = $optionService;
        $this->imageService = $imageService;
        $this->metafieldService = $metafieldService;
        parent::__construct($client);
    }

    /**
     * @param ShopifyProduct $product
     * @param bool           $publish
     *
     * @return object
     */
    public function create(ShopifyProduct $product, $publish = true)
    {
        $serializedModel = ['product' => array_merge($this->serializeModel($product), ['published' => $publish])];

        $raw = $this->client->post('admin/products.json', [], $serializedModel);

        return $this->unserializeModel($raw['product'], ShopifyProduct::class);
    }

    /**
     * @param $id
     * @param $filter
     *
     * @return ShopifyProduct
     */
    public function getById($id, $filter = [])
    {
        $raw = $this->client->get("admin/products/$id.json", $filter);

        return $this->unserializeModel($raw['product'], ShopifyProduct::class);
    }

    /**
     * @param int   $page
     * @param int   $limit
     * @param array $filter
     *
     * @return Collection
     */
    public function getAll($page = 1, $limit = 50, $filter = [])
    {
        $raw = $this->client->get('admin/products.json', array_merge(['page' => $page, 'limit' => $limit], $filter));

        $products = array_map(function ($product) {
            return $this->unserializeModel($product, ShopifyProduct::class);
        }, $raw['products']);

        return new Collection($products);
    }

    /**
     * @param $parms
     *
     * @return \Illuminate\Support\Collection
     */
    public function getByParams($parms)
    {
        $raw = $this->client->get('admin/products.json', $parms);

        $products = array_map(function ($product) {
            return $this->unserializeModel($product, ShopifyProduct::class);
        }, $raw['products']);

        return new Collection($products);
    }

    /**
     * @param $productIds
     * @param array $filter
     *
     * @return Collection
     */
    public function getByProductIdArray($productIds, $filter = [])
    {
        $products = [];
        $limit = 250;

        for ($i = 0; $i < ceil(count($productIds) / $limit); ++$i) {
            $idList = implode(',', array_slice($productIds, ($i * $limit), $limit));

            $params = array_merge(
                $filter,
                [
                    'ids' => $idList,
                    'limit' => $limit,
                ]
            );

            $shopifyProductsBatch = $this->getByParams($params);

            /** @var ShopifyProduct $shopifyProduct */
            foreach ($shopifyProductsBatch as $shopifyProduct) {
                $products[] = $shopifyProduct;
            }
        }

        return new Collection($products);
    }

    /**
     * @param ShopifyProduct $product
     *
     * @return object
     */
    public function update(ShopifyProduct $product)
    {
{'fraction_non_alphanumeric': 0.1337892506260836, 'fraction_numerical': 0.001541128876902331, 'mean_word_length': 2.4358041032428854, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 71, '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': '2939685', 'n_tokens_mistral': 3017, 'n_tokens_neox': 2921, 'n_words': 589}