Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit d2b63dd

Browse files
author
Jens Schulze
committed
feat(Category): support category key functionality
Closes #326
1 parent 02676ec commit d2b63dd

File tree

11 files changed

+259
-1
lines changed

11 files changed

+259
-1
lines changed

src/Core/Model/Category/Category.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
* @method Category setMetaKeywords(LocalizedString $metaKeywords = null)
4848
* @method AssetCollection getAssets()
4949
* @method Category setAssets(AssetCollection $assets = null)
50+
* @method string getKey()
51+
* @method Category setKey(string $key = null)
5052
* @method CategoryReference getReference()
5153
*/
5254
class Category extends Resource
@@ -76,6 +78,7 @@ public function fieldDefinitions()
7678
'metaKeywords' => [static::TYPE => LocalizedString::class],
7779
'custom' => [static::TYPE => CustomFieldObject::class],
7880
'assets' => [static::TYPE => AssetCollection::class],
81+
'key' => [static::TYPE => 'string'],
7982
];
8083
}
8184
}

src/Core/Model/Category/CategoryCollection.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class CategoryCollection extends Collection
1919
const SLUG = 'slug';
2020
const PARENT = 'parent';
2121
const ROOTS = 'roots';
22+
const KEY = 'key';
2223

2324
protected $type = Category::class;
2425

@@ -28,12 +29,15 @@ protected function indexRow($offset, $row)
2829
$slugs = $row->getSlug()->toArray();
2930
$parentId = !is_null($row->getParent()) ? $row->getParent()->getId() : null;
3031
$id = $row->getId();
32+
$key = $row->getKey();
3133
} else {
3234
$slugs = isset($row[static::SLUG]) ? $row[static::SLUG] : [];
3335
$id = isset($row[static::ID]) ? $row[static::ID] : null;
36+
$key = isset($row[static::KEY]) ? $row[static::KEY] : null;
3437
$parentId = isset($row[static::PARENT][static::ID]) ? $row[static::PARENT][static::ID] : null;
3538
}
3639
$this->addToIndex(static::ID, $offset, $id);
40+
$this->addToIndex(static::KEY, $offset, $key);
3741
foreach ($slugs as $locale => $slug) {
3842
$locale = \Locale::canonicalize($locale);
3943
$this->addToIndex(static::SLUG, $offset, $locale . '-' . $slug);
@@ -54,6 +58,15 @@ public function getById($id)
5458
return $this->getBy(static::ID, $id);
5559
}
5660

61+
/**
62+
* @param $key
63+
* @return Category|null
64+
*/
65+
public function getByKey($key)
66+
{
67+
return $this->getBy(static::KEY, $key);
68+
}
69+
5770
/**
5871
* @return Category[]
5972
*/

src/Core/Model/Category/CategoryDraft.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
* @method CategoryDraft setMetaKeywords(LocalizedString $metaKeywords = null)
3838
* @method AssetDraftCollection getAssets()
3939
* @method CategoryDraft setAssets(AssetDraftCollection $assets = null)
40+
* @method string getKey()
41+
* @method CategoryDraft setKey(string $key = null)
4042
*/
4143
class CategoryDraft extends JsonObject
4244
{
@@ -53,7 +55,8 @@ public function fieldDefinitions()
5355
'metaTitle' => [static::TYPE => LocalizedString::class],
5456
'metaKeywords' => [static::TYPE => LocalizedString::class],
5557
'custom' => [static::TYPE => CustomFieldObjectDraft::class],
56-
'assets' => [static::TYPE => AssetDraftCollection::class]
58+
'assets' => [static::TYPE => AssetDraftCollection::class],
59+
'key' => [static::TYPE => 'string'],
5760
];
5861
}
5962

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
* @created: 26.01.15, 14:56
5+
*/
6+
7+
namespace Commercetools\Core\Request\Categories;
8+
9+
use Commercetools\Core\Model\Common\Context;
10+
use Commercetools\Core\Model\Category\Category;
11+
use Commercetools\Core\Request\AbstractByKeyGetRequest;
12+
use Commercetools\Core\Response\ApiResponseInterface;
13+
use Commercetools\Core\Model\MapperInterface;
14+
15+
/**
16+
* @package Commercetools\Core\Request\Categories
17+
* @link https://dev.commercetools.com/http-api-projects-categories.html#get-category-by-key
18+
* @method Category mapResponse(ApiResponseInterface $response)
19+
* @method Category mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
20+
*/
21+
class CategoryByKeyGetRequest extends AbstractByKeyGetRequest
22+
{
23+
protected $resultClass = Category::class;
24+
25+
/**
26+
* @param string $key
27+
* @param Context $context
28+
*/
29+
public function __construct($key, Context $context = null)
30+
{
31+
parent::__construct(CategoriesEndpoint::endpoint(), $key, $context);
32+
}
33+
34+
/**
35+
* @param string $key
36+
* @param Context $context
37+
* @return static
38+
*/
39+
public static function ofKey($key, Context $context = null)
40+
{
41+
return new static($key, $context);
42+
}
43+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
* @created: 26.01.15, 17:02
5+
*/
6+
7+
namespace Commercetools\Core\Request\Categories;
8+
9+
use Commercetools\Core\Model\Common\Context;
10+
use Commercetools\Core\Request\AbstractDeleteByKeyRequest;
11+
use Commercetools\Core\Model\Category\Category;
12+
use Commercetools\Core\Response\ApiResponseInterface;
13+
use Commercetools\Core\Model\MapperInterface;
14+
15+
/**
16+
* @package Commercetools\Core\Request\Categories
17+
* @link https://dev.commercetools.com/http-api-projects-categories.html#delete-category
18+
* @method Category mapResponse(ApiResponseInterface $response)
19+
* @method Category mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
20+
*/
21+
class CategoryDeleteByKeyRequest extends AbstractDeleteByKeyRequest
22+
{
23+
protected $resultClass = Category::class;
24+
25+
/**
26+
* @param string $key
27+
* @param int $version
28+
* @param Context $context
29+
*/
30+
public function __construct($key, $version, Context $context = null)
31+
{
32+
parent::__construct(CategoriesEndpoint::endpoint(), $key, $version, $context);
33+
}
34+
35+
/**
36+
* @param string $key
37+
* @param int $version
38+
* @param Context $context
39+
* @return static
40+
*/
41+
public static function ofKeyAndVersion($key, $version, Context $context = null)
42+
{
43+
return new static($key, $version, $context);
44+
}
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
* @created: 26.01.15, 18:14
5+
*/
6+
7+
namespace Commercetools\Core\Request\Categories;
8+
9+
use Commercetools\Core\Model\Common\Context;
10+
use Commercetools\Core\Request\AbstractUpdateByKeyRequest;
11+
use Commercetools\Core\Model\Category\Category;
12+
use Commercetools\Core\Response\ApiResponseInterface;
13+
use Commercetools\Core\Model\MapperInterface;
14+
15+
/**
16+
* @package Commercetools\Core\Request\Categories
17+
* @link https://dev.commercetools.com/http-api-projects-categories.html#update-category
18+
* @method Category mapResponse(ApiResponseInterface $response)
19+
* @method Category mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
20+
*/
21+
class CategoryUpdateByKeyRequest extends AbstractUpdateByKeyRequest
22+
{
23+
protected $resultClass = Category::class;
24+
25+
/**
26+
* @param string $key
27+
* @param int $version
28+
* @param array $actions
29+
* @param Context $context
30+
*/
31+
public function __construct($key, $version, array $actions = [], Context $context = null)
32+
{
33+
parent::__construct(CategoriesEndpoint::endpoint(), $key, $version, $actions, $context);
34+
}
35+
36+
/**
37+
* @param string $key
38+
* @param int $version
39+
* @param Context $context
40+
* @return static
41+
*/
42+
public static function ofKeyAndVersion($key, $version, Context $context = null)
43+
{
44+
return new static($key, $version, [], $context);
45+
}
46+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\Categories\Command;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Request\AbstractAction;
10+
11+
/**
12+
* @package Commercetools\Core\Request\Categories\Command
13+
* @link https://dev.commercetools.com/http-api-projects-categories.html#set-key
14+
* @method string getKey()
15+
* @method CategorySetKeyAction setKey(string $key = null)
16+
* @method string getAction()
17+
* @method CategorySetKeyAction setAction(string $action = null)
18+
*/
19+
class CategorySetKeyAction extends AbstractAction
20+
{
21+
public function fieldDefinitions()
22+
{
23+
return [
24+
'action' => [static::TYPE => 'string'],
25+
'key' => [static::TYPE => 'string']
26+
];
27+
}
28+
29+
/**
30+
* @param array $data
31+
* @param Context|callable $context
32+
*/
33+
public function __construct(array $data = [], $context = null)
34+
{
35+
parent::__construct($data, $context);
36+
$this->setAction('setKey');
37+
}
38+
39+
/**
40+
* @param string $key
41+
* @param Context|callable $context
42+
* @return CategorySetKeyAction
43+
*/
44+
public static function ofKey($key, $context = null)
45+
{
46+
return static::of($context)->setKey($key);
47+
}
48+
}

tests/fixtures/models.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ category:
172172
- metaKeywords
173173
- metaTitle
174174
- assets
175+
- key
175176

176177
categoryDraft:
177178
domain: category
@@ -188,6 +189,7 @@ categoryDraft:
188189
- metaTitle
189190
- custom
190191
- assets
192+
- key
191193

192194
address:
193195
domain: common

tests/integration/Category/CategoryCreateRequestTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Commercetools\Core\Model\Category\CategoryDraft;
1010
use Commercetools\Core\Model\Common\LocalizedString;
1111
use Commercetools\Core\Request\Categories\CategoryCreateRequest;
12+
use Commercetools\Core\Request\Categories\CategoryDeleteByKeyRequest;
1213
use Commercetools\Core\Request\Categories\CategoryDeleteRequest;
1314

1415
class CategoryCreateRequestTest extends ApiTestCase
@@ -52,4 +53,19 @@ public function testCreate()
5253
$this->assertNotEmpty($category->getId());
5354
$this->assertSame(1, $category->getVersion());
5455
}
56+
57+
public function testDeleteByKey()
58+
{
59+
$draft = $this->getDraft('myCategory', 'my-category')->setKey($this->getTestRun());
60+
$category = $this->createCategory($draft);
61+
62+
$request = CategoryDeleteByKeyRequest::ofKeyAndVersion(
63+
$category->getKey(),
64+
$category->getVersion()
65+
);
66+
$result = $this->getClient()->execute($request);
67+
$response = $request->mapFromResponse($result);
68+
69+
$this->assertSame($category->getId(), $response->getId());
70+
}
5571
}

tests/integration/Category/CategoryQueryRequestTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Commercetools\Core\Model\Category\CategoryDraft;
1111
use Commercetools\Core\Model\Category\CategoryReference;
1212
use Commercetools\Core\Model\Common\LocalizedString;
13+
use Commercetools\Core\Request\Categories\CategoryByKeyGetRequest;
1314
use Commercetools\Core\Request\Categories\CategoryQueryRequest;
1415
use Commercetools\Core\Request\Categories\CategoryCreateRequest;
1516
use Commercetools\Core\Request\Categories\CategoryDeleteRequest;
@@ -57,6 +58,19 @@ public function testGetById()
5758

5859
}
5960

61+
public function testGetByKey()
62+
{
63+
$category = $this->createCategory(
64+
$this->getDraft('myCategory', 'my-category')->setKey($this->getTestRun())
65+
);
66+
67+
$result = $this->getClient()->execute(CategoryByKeyGetRequest::ofKey($category->getKey()))->toObject();
68+
69+
$this->assertInstanceOf(Category::class, $result);
70+
$this->assertSame($category->getId(), $result->getId());
71+
72+
}
73+
6074
public function testQuery()
6175
{
6276
$category = $this->createCategory($this->getDraft('myCategory', 'my-category'));

0 commit comments

Comments
 (0)