# sync-basalam/1.10.0/includes/Admin/Product/Data/ProductDataBuilder.php

ووسلام – همگام سازی ووکامرس و باسلام, version 1.10.0. 90 lines.

- Page: https://pluginprobe.com/plugins/sync-basalam/1.10.0/code/includes/Admin/Product/Data/ProductDataBuilder.php
- Raw: https://pluginprobe.com/plugins/sync-basalam/1.10.0/raw/includes/Admin/Product/Data/ProductDataBuilder.php
- Modified: 2026-07-11T07:54:58+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/sync-basalam/1.10.0/code/includes/Admin/Product/Data/ProductDataBuilder.php#L10-L20`.

```php
<?php

namespace SyncBasalam\Admin\Product\Data;

use SyncBasalam\Admin\Product\Data\Strategies\DataStrategyInterface;
use SyncBasalam\Admin\Product\Data\Validators\ValidatorChain;
use SyncBasalam\Admin\Product\ProductDataFactory;

defined('ABSPATH') || exit;

class ProductDataBuilder
{
    private array $data = [];
    private $validator;
    private $factory;
    private DataStrategyInterface $strategy;

    public function __construct(
        ?ValidatorChain $validator,
        ProductDataFactory $factory
    ) {
        $this->validator = $validator;
        $this->factory = $factory;
        $this->initializeData();
    }

    public function setStrategy(DataStrategyInterface $strategy): self
    {
        $this->strategy = $strategy;
        return $this;
    }

    public function fromWooProduct(int $productId): self
    {
        $product = wc_get_product($productId);

        if (!$product) throw new \InvalidArgumentException(esc_html("Product with ID {$productId} not found"));

        // Validate product first
        if ($this->validator) $this->validator->validate($product);

        // Use factory to get appropriate data handler
        $handler = $this->factory->createHandler($product);

        // Apply strategy to collect data
        $this->data = $this->strategy->collect($product, $handler);

        return $this;
    }

    public function withCategoryIds(array $categoryIds = null): self
    {
        if ($categoryIds !== null) $this->data['category_ids'] = $categoryIds;
        return $this;
    }

    public function build(): array
    {
        return $this->data;
    }

    public function reset(): self
    {
        $this->initializeData();
        return $this;
    }

    private function initializeData(): void
    {
        $this->data = [
            'name' => '',
            'description' => '',
            'category_id' => null,
            'category_ids' => [],
            'primary_price' => null,
            'stock' => null,
            'weight' => null,
            'package_weight' => null,
            'photo' => null,
            'photos' => [],
            'preparation_days' => null,
            'unit_type' => 6304,
            'unit_quantity' => 1,
            'is_wholesale' => false,
            'variants' => [],
            'product_attribute' => [],
        ];
    }
}

```
