Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the CategoryAttribute data type #13

Merged
merged 7 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Attribute/DataType/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getEshopModel(): EshopAttributeModel
*/
public function getTitle(): string
{
return (string)$this->attribute->getRawFieldData('oxtitle');
return (string)($this->attribute->getRawFieldData('oxtitle') ?? $this->attribute->getTitle());
}

public static function getModelClass(): string
Expand Down
61 changes: 61 additions & 0 deletions src/Category/DataType/CategoryAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\GraphQL\Storefront\Category\DataType;

use OxidEsales\Eshop\Application\Model\Attribute as EshopAttributeModel;
use OxidEsales\GraphQL\Base\DataType\ShopModelAwareInterface;
use OxidEsales\GraphQL\Storefront\Attribute\DataType\Attribute;
use TheCodingMachine\GraphQLite\Annotations\Field;
use TheCodingMachine\GraphQLite\Annotations\Type;

/**
* @Type()
*/
final class CategoryAttribute implements ShopModelAwareInterface
{
/** @var EshopAttributeModel */
private $attribute;

public function __construct(EshopAttributeModel $attribute)
{
$this->attribute = $attribute;
}

public function getEshopModel(): EshopAttributeModel
{
return $this->attribute;
}

/**
* @Field()
*/
public function getAttribute(): Attribute
{
return new Attribute($this->attribute);
}

/**
* @Field()
*
* @return string[]
*/
public function getValues(): array
{
return $this->attribute->getValues();
}

/**
* @return class-string
*/
public static function getModelClass(): string
{
return EshopAttributeModel::class;
}
}
43 changes: 43 additions & 0 deletions src/Category/Infrastructure/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\GraphQL\Storefront\Category\Infrastructure;

use OxidEsales\Eshop\Application\Model\Attribute as EshopAttributeModel;
use OxidEsales\Eshop\Application\Model\AttributeList as EshopAttributeListModel;
use OxidEsales\GraphQL\Storefront\Category\DataType\Category as CategoryDataType;
use OxidEsales\GraphQL\Storefront\Category\DataType\CategoryAttribute as CategoryAttributeDataType;

use function count;
use function is_iterable;

final class Category
{
/**
* @return CategoryAttributeDataType[]
*/
public function getAttributes(CategoryDataType $category): array
{
/** @var EshopAttributeListModel $categoryAttributes */
$categoryAttributes = $category->getEshopModel()->getAttributes();

if (!is_iterable($categoryAttributes) || count($categoryAttributes) === 0) {
return [];
}

$attributes = [];

/** @var EshopAttributeModel $attribute */
foreach ($categoryAttributes as $key => $attribute) {
$attributes[$key] = new CategoryAttributeDataType($attribute);
}

return $attributes;
}
}
19 changes: 18 additions & 1 deletion src/Category/Service/RelationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
use OxidEsales\GraphQL\Storefront\Product\DataType\Product;
use OxidEsales\GraphQL\Storefront\Product\DataType\ProductFilterList;
use OxidEsales\GraphQL\Storefront\Product\DataType\Sorting as ProductSorting;
use OxidEsales\GraphQL\Storefront\Category\Infrastructure\Category as CategoryInfrastructure;
use OxidEsales\GraphQL\Storefront\Product\Service\Product as ProductService;
use OxidEsales\GraphQL\Storefront\Shared\DataType\Seo;
use TheCodingMachine\GraphQLite\Annotations\ExtendType;
use TheCodingMachine\GraphQLite\Annotations\Field;
use TheCodingMachine\GraphQLite\Types\ID;
use OxidEsales\GraphQL\Storefront\Category\DataType\CategoryAttribute;

/**
* @ExtendType(class=Category::class)
Expand All @@ -38,12 +40,17 @@ final class RelationService
/** @var CategoryService */
private $categoryService;

/** @var CategoryInfrastructure */
private $categoryInfrastructure;

public function __construct(
ProductService $productService,
CategoryService $categoryService
CategoryService $categoryService,
CategoryInfrastructure $categoryInfrastructure
) {
$this->productService = $productService;
$this->categoryService = $categoryService;
$this->categoryInfrastructure = $categoryInfrastructure;
}

/**
Expand Down Expand Up @@ -127,4 +134,14 @@ private function getCategoryById(ID $id): ?Category

return $result;
}

/**
* @Field()
*
* @return CategoryAttribute[]
*/
public function getAttributes(Category $category): array
{
return $this->categoryInfrastructure->getAttributes($category);
}
}