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

Commit 9fa6527

Browse files
author
Jens Schulze
committed
feat(Search): add subtree filter model
Closes #244
1 parent 83b400d commit 9fa6527

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <[email protected]>
4+
*/
5+
6+
namespace Commercetools\Core\Model\Product\Search;
7+
8+
class FilterSubtree
9+
{
10+
/**
11+
* @var mixed
12+
*/
13+
protected $id;
14+
15+
/**
16+
* FilterRange constructor.
17+
* @param mixed $id
18+
*/
19+
public function __construct($id)
20+
{
21+
$this->id = $id;
22+
}
23+
24+
/**
25+
* @param $value
26+
* @return string
27+
*/
28+
protected function valueToString($value)
29+
{
30+
$value = (string)$value;
31+
return '"' . (string)$value . '"';
32+
}
33+
34+
/**
35+
* @return string
36+
*/
37+
public function __toString()
38+
{
39+
return sprintf('subtree(%s)', $this->valueToString($this->getId()));
40+
}
41+
42+
/**
43+
* @param $id
44+
* @return static
45+
*/
46+
public static function ofId($id)
47+
{
48+
return new static($id);
49+
}
50+
51+
/**
52+
* @return mixed
53+
*/
54+
public function getId()
55+
{
56+
return $this->id;
57+
}
58+
59+
/**
60+
* @param mixed $id
61+
* @return $this
62+
*/
63+
public function setId($id)
64+
{
65+
$this->id = $id;
66+
return $this;
67+
}
68+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <[email protected]>
4+
*/
5+
6+
namespace Commercetools\Core\Model\Product\Search;
7+
8+
use Commercetools\Core\Model\Common\Collection;
9+
10+
/**
11+
* @package Commercetools\Core\Model\Product\Search
12+
*
13+
* @method FilterSubtree current()
14+
* @method FilterSubtreeCollection add(FilterSubtree $element)
15+
* @method FilterSubtree getAt($offset)
16+
*/
17+
class FilterSubtreeCollection extends Collection
18+
{
19+
protected $type = '\Commercetools\Core\Model\Product\Search\FilterSubtree';
20+
21+
public function __toString()
22+
{
23+
$values = [];
24+
foreach ($this as $value) {
25+
$values[] = (string)$value;
26+
}
27+
return implode(',', $values);
28+
}
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <[email protected]>
4+
*/
5+
6+
namespace Commercetools\Core\Model\Product\Search;
7+
8+
class FilterSubtreeTest extends \PHPUnit_Framework_TestCase
9+
{
10+
public function testMapValue()
11+
{
12+
$subtree = FilterSubtree::ofId('12345');
13+
$this->assertSame('subtree("12345")', (string)$subtree);
14+
}
15+
16+
public function testDefaultType()
17+
{
18+
$subtrees = FilterSubtreeCollection::of();
19+
$subtrees
20+
->add(FilterSubtree::ofId('12345'))
21+
->add(FilterSubtree::ofId('abcde'))
22+
;
23+
$this->assertSame('subtree("12345"),subtree("abcde")', (string)$subtrees);
24+
}
25+
26+
public function testIntValue()
27+
{
28+
$subtree = FilterSubtree::ofId(12345);
29+
$this->assertSame('subtree("12345")', (string)$subtree);
30+
}
31+
}

0 commit comments

Comments
 (0)