Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

Commit a762278

Browse files
committed
✨ better API & structure
1 parent 01aa129 commit a762278

15 files changed

+693
-312
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor

Attribute.php

+11-28
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@ public function __construct($name, $type)
1919
$this->name = $name ?? null;
2020
$this->type = $type ?? null;
2121

22-
if ($this->isValid()) {
23-
return (array) $this->toArray();
24-
}
22+
return $this->toArray();
23+
}
2524

26-
return null;
25+
/**
26+
* Enforces attribute as an array
27+
*
28+
* @param void
29+
* @return array attribute
30+
*/
31+
public function toArray()
32+
{
33+
return [$this->name => ['type' => $this->type]];
2734
}
2835

2936
/**
@@ -64,28 +71,4 @@ public function setType($type)
6471

6572
return $self;
6673
}
67-
68-
/**
69-
* Enforces attribute as an array
70-
*
71-
* @param void
72-
* @return array attribute
73-
*/
74-
public function toArray()
75-
{
76-
return (array) [$this->name => ['type' => $this->type]];
77-
}
78-
79-
/**
80-
* Checks if attribute is miniminally valid
81-
*
82-
* @param void
83-
* @return binary validity
84-
*/
85-
public function isValid()
86-
{
87-
return (isset($this->name) && isset($this->type))
88-
? true
89-
: false;
90-
}
9174
}

Builder.php

+25-152
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace Blocks;
44

5-
use Blocks\Script;
6-
use Blocks\Attribute;
5+
use \Blocks\Script;
6+
use \Blocks\Attribute;
7+
8+
use \Blocks\Traits\Register;
9+
use \Blocks\Traits\Compose;
710

811
use function Roots\view;
912
use function Roots\asset;
@@ -13,54 +16,33 @@
1316
*/
1417
class Builder
1518
{
19+
public $namespace;
20+
21+
public $name;
22+
23+
public $designation;
24+
25+
public $attributes;
26+
27+
public $type;
28+
29+
public $editor_script;
30+
31+
public $style;
32+
33+
public $handling;
34+
1635
/**
1736
* Construct
1837
*/
19-
public function __construct($namespace)
38+
public function __construct()
2039
{
21-
/**
22-
* Block namespace
23-
*/
24-
$this->namespace = 'sage';
25-
26-
/**
27-
* Block name
28-
*/
29-
$this->name = 'card';
30-
31-
/**
32-
* Block attributes
33-
**/
34-
$this->attributes = [];
35-
36-
/**
37-
* Editor script
38-
*/
39-
$this->editor_script = object;
40-
41-
/**
42-
* Style
43-
*/
44-
$this->style = object;
45-
46-
/**
47-
* Handling
48-
*
49-
* Use in usingScript, etc. to determine
50-
* what parameter we are manipulating call-to-call
51-
*/
52-
$this->handling = '';
53-
54-
/**
55-
* Set namespace if passed as param
56-
*/
57-
if (isset($namespace)) {
58-
$this->setNamespace($namespace);
59-
}
60-
40+
$this->compose();
6141
return $this;
6242
}
6343

44+
use Compose;
45+
6446
/**
6547
* Set block namespace
6648
*
@@ -74,19 +56,6 @@ public function setNamespace($name)
7456
return $this;
7557
}
7658

77-
/**
78-
* setNamespace() sugar
79-
*
80-
* @param string namespace
81-
* @return self
82-
*/
83-
public function namespace($name)
84-
{
85-
$this->setNamespace($name);
86-
87-
return $this;
88-
}
89-
9059
/**
9160
* Set block name
9261
*
@@ -100,19 +69,6 @@ public function setName($name)
10069
return $this;
10170
}
10271

103-
/**
104-
* setName() sugar
105-
*
106-
* @param string name
107-
* @return self
108-
*/
109-
public function name($name)
110-
{
111-
$this->setName($name);
112-
113-
return $this;
114-
}
115-
11672
/**
11773
* Get qualified name
11874
*/
@@ -160,87 +116,4 @@ public function addString($name)
160116

161117
return $this;
162118
}
163-
164-
/**
165-
* Set Style
166-
*
167-
* @param string style reference
168-
* @return self
169-
* @todo #todo incomplete
170-
*/
171-
public function style($style_name)
172-
{
173-
$this->assets->style = "{$this->namespace}-{$style_name}";
174-
175-
return $this;
176-
}
177-
178-
/**
179-
* Add Script
180-
*/
181-
public function addEditorScript($definition = null)
182-
{
183-
$this->editor_script = $definition
184-
? new Script($definition)
185-
: new Script([
186-
'namespace' => $this->namespace,
187-
'name' => $this->name,
188-
'type' => 'block',
189-
]);
190-
191-
$this->handling = 'editor_script';
192-
193-
return $this->editor_script;
194-
}
195-
196-
/**
197-
* Uses existing editor_script
198-
*
199-
* @param mixed script
200-
* @return self
201-
*/
202-
public function usesEditorScript($namespace, $name)
203-
{
204-
if (is_string($script)) {
205-
$this->editor_script = new Script([
206-
'name' => $name,
207-
'namespace' => $namespace,
208-
]);
209-
}
210-
211-
return $this;
212-
}
213-
214-
/**
215-
* Register blocktype
216-
*
217-
* @param object data
218-
* @return void
219-
*/
220-
public function register()
221-
{
222-
add_action('init', function () {
223-
register_block_type($this->getDesignation(), [
224-
'attributes' => $this->attributes,
225-
'editor_script' => $this->editor_script->getDesignation(),
226-
'render_callback' => function ($attributes) {
227-
return view(
228-
"blocks.{$this->name}.{$this->name}",
229-
$this->data($attributes)
230-
);
231-
}
232-
]);
233-
});
234-
}
235-
236-
/**
237-
* Data
238-
*
239-
* @param array block attributes
240-
* @return array view data
241-
*/
242-
public function data($attributes)
243-
{
244-
return $attributes;
245-
}
246119
}

0 commit comments

Comments
 (0)