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

Commit d1c22df

Browse files
committed
Add ValidProps formatters
1 parent ade6234 commit d1c22df

File tree

5 files changed

+60
-7
lines changed

5 files changed

+60
-7
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
},
3636
"require": {
3737
"php": ">=7.4",
38+
"ext-json": "*",
3839
"nette/application": "^3.0",
3940
"nette/schema": "^1.0",
4041
"wavevision/utils": "^2.5"

docs/_coverpage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<a href="https://github.com/wavevision"><img alt="Wavevision s.r.o." src="https://wavevision.com/images/wavevision-logo.png" width="120" /></a>
2-
<h1>PropsControl <small>6.0.0</small></h1>
2+
<h1>PropsControl <small>6.2.0</small></h1>
33

44
> Create smart UI components for [@nette](https://github.com) framework.
55

docs/guide/working-with-props.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Working with Props
22

3-
As demonstrated in [First Component](getting-started/first-component.md) example, each `PropsControl` component needs its
4-
Props for data consumption and output rendering.
3+
As demonstrated in [First Component](getting-started/first-component.md) example, each `PropsControl` component needs
4+
its Props for data consumption and output rendering.
55

66
## Definition
77

8-
The definition will give us the shape of our props, so we can structure and validate them.
9-
Also, the constants used for the definition can be later used to create and get the data while avoiding typos in prop names.
10-
Constants are also extremely useful when re-defining your props. If you refactor any constant in your IDE, the change will
11-
get propagated through your whole application.
8+
The definition will give us the shape of our props, so we can structure and validate them. Also, the constants used for
9+
the definition can be later used to create and get the data while avoiding typos in prop names. Constants are also
10+
extremely useful when re-defining your props. If you refactor any constant in your IDE, the change will get propagated
11+
through your whole application.
1212

1313
After we created our definition and linked it to our component we can use the definition object to wrap our data.
1414

@@ -64,3 +64,11 @@ Props are read-only, `Wavevision\PropsControl\Exceptions\NotAllowed` exception w
6464
- you're trying to access an undefined prop
6565
- you're trying to write to a prop
6666
- you're calling any method not defined in `Wavevision\PropsControl\ValidProps` class
67+
68+
## Formatting props
69+
70+
You can use a few predefined methods to format values of the `ValidProps` object.
71+
72+
- `toArray(): array<mixed>`
73+
- `toArrayHash(): Nette\Utils\ArrayHash<mixed>`
74+
- `toJson(): string`

src/PropsControl/ValidProps.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Wavevision\PropsControl;
44

5+
use Nette\Utils\ArrayHash;
6+
use Nette\Utils\Json;
57
use stdClass;
68
use Wavevision\PropsControl\Exceptions\NotAllowed;
79
use function array_key_exists;
@@ -55,6 +57,27 @@ public function getProps(): Props
5557
return $this->props;
5658
}
5759

60+
/**
61+
* @return array<mixed>
62+
*/
63+
public function toArray(): array
64+
{
65+
return $this->values;
66+
}
67+
68+
/**
69+
* @return ArrayHash<mixed>
70+
*/
71+
public function toArrayHash(): ArrayHash
72+
{
73+
return ArrayHash::from($this->toArray());
74+
}
75+
76+
public function toJson(): string
77+
{
78+
return Json::encode($this->toArray());
79+
}
80+
5881
/**
5982
* @param mixed[] $arguments
6083
*/

tests/PropsControlTests/ValidPropsTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Wavevision\PropsControlTests;
44

5+
use Nette\Utils\ArrayHash;
56
use PHPUnit\Framework\TestCase;
67
use Wavevision\PropsControl\Exceptions\NotAllowed;
78
use Wavevision\PropsControl\ValidProps;
@@ -33,6 +34,26 @@ public function testGetProps(): void
3334
$this->assertSame($props, $validProps->getProps());
3435
}
3536

37+
public function testToArray(): void
38+
{
39+
$props = [];
40+
$validProps = new ValidProps(new TestComponentProps(), $props);
41+
$this->assertSame($validProps->toArray(), $props);
42+
}
43+
44+
public function testToArrayHash(): void
45+
{
46+
$props = ['prop' => 'value'];
47+
$validProps = new ValidProps(new TestComponentProps(), $props);
48+
$this->assertEquals(ArrayHash::from($props), $validProps->toArrayHash());
49+
}
50+
51+
public function testToJson(): void
52+
{
53+
$validProps = new ValidProps(new TestComponentProps(), []);
54+
$this->assertIsString($validProps->toJson());
55+
}
56+
3657
public function testGetThrowsNotAllowed(): void
3758
{
3859
$validProps = new ValidProps(new TestComponentProps(), ['prop' => 'value']);

0 commit comments

Comments
 (0)