Skip to content

Commit 50bd037

Browse files
committed
Added begin of HasResolver concern (building)
1 parent a9e0a28 commit 50bd037

File tree

4 files changed

+224
-7
lines changed

4 files changed

+224
-7
lines changed

README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,25 @@ Base library for repeated layout fields, content builders and other collection c
55

66
- Installation & base use cases
77
- layouts registration
8+
- `register($layout, $limit)`
89
- get all registered layouts
9-
- limiting layouts count (global & layout-specific)
10-
- inserting instances
10+
- `layouts()`
11+
- limiting layouts
12+
- `limit($instances)`
13+
- `$layout->limit($instances)`
14+
- building instances
15+
- `build($data)`
16+
- `buildUsing($callback)`
17+
- `insert($key, $attributes, $index, $id)`
1118
- get all inserted instances, and manipulate the LayoutCollection :
12-
- `find($key)`
13-
- `whereKey($key)`
14-
- `whereKeyNot($key)`
15-
- `whereKeyIn($key)`
16-
- `whereKeyNotIn($key)`
19+
- `instances()`
20+
- `$instances->find($key)`
21+
- `$instances->whereKey($key)`
22+
- `$instances->whereKeyNot($key)`
23+
- `$instances->whereKeyIn($key)`
24+
- `$instances->whereKeyNotIn($key)`
1725
- counting instances
26+
- `count($key = null)`
1827

1928
## Testing
2029

src/Concerns/HasResolver.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace Whitecube\LaravelFlexibleContent\Concerns;
4+
5+
use Illuminate\Support\Collection;
6+
7+
trait HasResolver
8+
{
9+
/**
10+
* The callback to be used to setup the Flexible container's value.
11+
*
12+
* @var null|callable
13+
*/
14+
public $buildCallback;
15+
16+
/**
17+
* The callback to be used to save the Flexible container's value.
18+
*
19+
* @var null|callable
20+
*/
21+
public $saveCallback;
22+
23+
/**
24+
* Define the callback that should be used to setup the Flexible container's value.
25+
*
26+
* @param callable $buildCallback
27+
* @return $this
28+
*/
29+
public function buildUsing(callable $buildCallback)
30+
{
31+
$this->buildCallback = $buildCallback;
32+
33+
return $this;
34+
}
35+
36+
/**
37+
* Define the callback that should be used to save the Flexible container's value.
38+
*
39+
* @param callable $saveCallback
40+
* @return $this
41+
*/
42+
public function saveUsing(callable $saveCallback)
43+
{
44+
$this->saveCallback = $saveCallback;
45+
46+
return $this;
47+
}
48+
49+
/**
50+
* Create the Flexible container's base layout instances.
51+
*
52+
* @param mixed $data
53+
* @return $this
54+
*/
55+
public function build($data = null)
56+
{
57+
if(! is_null($this->buildCallback)) {
58+
call_user_func($this->buildCallback, $this, $data);
59+
return $this;
60+
}
61+
62+
return $this->buildFromData($data);
63+
}
64+
65+
/**
66+
* Create the Flexible container's base layout instances from given value.
67+
*
68+
* @param mixed $data
69+
* @return $this
70+
*/
71+
public function buildFromData($data = null)
72+
{
73+
if($data instanceof Collection) {
74+
$data = $data->toArray();
75+
} else if (is_string($data)) {
76+
$data = json_decode($data, true) ?? [];
77+
}
78+
79+
if(! $data || ! is_array($data)) {
80+
return $this;
81+
}
82+
83+
foreach ($data as $item) {
84+
$this->buildItem($item);
85+
}
86+
87+
return $this;
88+
}
89+
90+
/**
91+
* Attempt to insert a single instance from serialized value.
92+
*
93+
* @param mixed $item
94+
* @return $this
95+
*/
96+
public function buildItem($item)
97+
{
98+
if(! is_array($item) && ! is_object($item)) {
99+
return $this;
100+
}
101+
102+
$key = data_get($item, 'key');
103+
$id = data_get($item, 'id');
104+
$attributes = data_get($item, 'attributes', []);
105+
106+
$this->insert($key, (array) $attributes, null, $id ? strval($id) : null);
107+
108+
return $this;
109+
}
110+
111+
/**
112+
* Execute what has to be done in order to save the current value for next build.
113+
*
114+
* @return $this
115+
*/
116+
public function save()
117+
{
118+
// TODO.
119+
return $this;
120+
}
121+
}

src/Flexible.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ class Flexible implements FlexibleInterface
88
{
99
use Concerns\HasLayouts;
1010
use Concerns\HasLayoutInstances;
11+
use Concerns\HasResolver;
1112
}

tests/Unit/InstanceResolvingTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Whitecube\LaravelFlexibleContent\Flexible;
6+
7+
it('can build instances from serialized data (array)', function() {
8+
$data = [
9+
['key' => 'bar', 'id' => 'one', 'attributes' => []],
10+
['key' => 'foo', 'id' => 'two', 'attributes' => []],
11+
['key' => 'bar', 'id' => 'three', 'attributes' => []],
12+
];
13+
14+
$flexible = (new Flexible())
15+
->register(fn ($layout) => $layout->key('foo'))
16+
->register(fn ($layout) => $layout->key('bar'))
17+
->build($data);
18+
19+
$values = $flexible
20+
->instances()
21+
->map(fn ($layout) => $layout->getId());
22+
23+
expect($values->implode(','))->toBe('one,two,three');
24+
});
25+
26+
it('can build instances from serialized data (collection)', function() {
27+
$data = collect([
28+
(object) ['key' => 'bar', 'id' => 'one', 'attributes' => []],
29+
(object) ['key' => 'foo', 'id' => 'two', 'attributes' => []],
30+
(object) ['key' => 'bar', 'id' => 'three', 'attributes' => []],
31+
]);
32+
33+
$flexible = (new Flexible())
34+
->register(fn ($layout) => $layout->key('foo'))
35+
->register(fn ($layout) => $layout->key('bar'))
36+
->build($data);
37+
38+
$values = $flexible
39+
->instances()
40+
->map(fn ($layout) => $layout->getId());
41+
42+
expect($values->implode(','))->toBe('one,two,three');
43+
});
44+
45+
it('can build instances from serialized data (json)', function() {
46+
$data = json_encode([
47+
['key' => 'bar', 'id' => 'one', 'attributes' => []],
48+
['key' => 'foo', 'id' => 'two', 'attributes' => []],
49+
['key' => 'bar', 'id' => 'three', 'attributes' => []],
50+
]);
51+
52+
$flexible = (new Flexible())
53+
->register(fn ($layout) => $layout->key('foo'))
54+
->register(fn ($layout) => $layout->key('bar'))
55+
->build($data);
56+
57+
$values = $flexible
58+
->instances()
59+
->map(fn ($layout) => $layout->getId());
60+
61+
expect($values->implode(','))->toBe('one,two,three');
62+
});
63+
64+
it('can build instances from serialized data using custom callable', function() {
65+
$data = [
66+
['key' => 'bar', 'id' => 'one', 'attributes' => []],
67+
['key' => 'foo', 'id' => 'two', 'attributes' => []],
68+
['key' => 'bar', 'id' => 'three', 'attributes' => []],
69+
];
70+
71+
$flexible = (new Flexible())
72+
->register(fn ($layout) => $layout->key('foo'))
73+
->register(fn ($layout) => $layout->key('bar'))
74+
->buildUsing(function(Flexible $container, $items) {
75+
foreach ($items as $item) {
76+
$container->insert($item['key'], $item['attributes'], null, $item['id'] . '!');
77+
}
78+
})
79+
->build($data);
80+
81+
$values = $flexible
82+
->instances()
83+
->map(fn ($layout) => $layout->getId());
84+
85+
expect($values->implode(','))->toBe('one!,two!,three!');
86+
});

0 commit comments

Comments
 (0)