Skip to content

Commit c71d87d

Browse files
committed
Model: add types
1 parent a0b2840 commit c71d87d

25 files changed

+191
-193
lines changed

src/PHPDraft/Model/Category.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212

1313
namespace PHPDraft\Model;
1414

15-
use PHPDraft\Model\Elements\BasicStructureElement;
1615
use PHPDraft\Model\Elements\ObjectStructureElement;
17-
use stdClass;
1816

1917
/**
2018
* Class Category.
@@ -26,28 +24,19 @@ class Category extends HierarchyElement
2624
*
2725
* @var ObjectStructureElement[]
2826
*/
29-
public $structures = [];
30-
31-
/**
32-
* Category type.
33-
*
34-
* @var ?string
35-
*/
36-
public $type = null;
27+
public array $structures = [];
3728

3829
/**
3930
* Fill class values based on JSON object.
4031
*
41-
* @param stdClass $object JSON object
32+
* @param object $object JSON object
4233
*
43-
* @return $this self-reference
34+
* @return self self-reference
4435
*/
45-
public function parse(stdClass $object)
36+
public function parse(object $object): self
4637
{
4738
parent::parse($object);
4839

49-
$this->type = $object->meta->classes->content ?? null;
50-
5140
foreach ($object->content as $item) {
5241
switch ($item->element) {
5342
case 'resource':

src/PHPDraft/Model/Comparable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ interface Comparable
1616
/**
1717
* Check if item is the same as other item.
1818
*
19-
* @param self $b Object to compare to
19+
* @param object $b Object to compare to
2020
*
2121
* @return bool
2222
*/
23-
public function is_equal_to($b): bool;
23+
public function is_equal_to(object $b): bool;
2424
}

src/PHPDraft/Model/Elements/ArrayStructureElement.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ArrayStructureElement extends BasicStructureElement
2323
* Parse an array object.
2424
*
2525
* @param object|null $object APIB Item to parse
26-
* @param array $dependencies List of dependencies build
26+
* @param string[] $dependencies List of dependencies build
2727
*
2828
* @return self Self reference
2929
*/
@@ -60,8 +60,7 @@ public function __toString(): string
6060
if (is_string($this->value)) {
6161
$type = $this->get_element_as_html($this->element);
6262
$desc = '';
63-
if ($this->description !== NULL)
64-
{
63+
if ($this->description !== null) {
6564
$desc = MarkdownExtra::defaultTransform($this->description);
6665
}
6766

src/PHPDraft/Model/Elements/BasicStructureElement.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@ abstract class BasicStructureElement implements StructureElement
1515
*
1616
* @var ElementStructureElement|null
1717
*/
18-
public $key;
18+
public ?ElementStructureElement $key = null;
1919
/**
2020
* Object JSON type.
2121
*
2222
* @var string|null
2323
*/
24-
public $type;
24+
public ?string $type;
2525
/**
2626
* Object description.
2727
*
2828
* @var string|null
2929
*/
30-
public $description;
30+
public ?string $description = null;
3131
/**
3232
* Type of element.
3333
*
3434
* @var string|null
3535
*/
36-
public $element = null;
36+
public ?string $element = null;
3737
/**
3838
* Object value.
3939
*
@@ -45,31 +45,31 @@ abstract class BasicStructureElement implements StructureElement
4545
*
4646
* @var string|null
4747
*/
48-
public $status = '';
48+
public ?string $status = '';
4949
/**
5050
* Parent structure.
5151
*
5252
* @var string|null
5353
*/
54-
public $ref;
54+
public ?string $ref;
5555
/**
5656
* Is variable.
5757
*
5858
* @var bool
5959
*/
60-
public $is_variable;
60+
public bool $is_variable = false;
6161
/**
6262
* List of object dependencies.
6363
*
6464
* @var string[]|null
6565
*/
66-
public $deps;
66+
public ?array $deps;
6767

6868
/**
6969
* Parse a JSON object to a structure.
7070
*
7171
* @param object|null $object An object to parse
72-
* @param array $dependencies Dependencies of this object
72+
* @param string[] $dependencies Dependencies of this object
7373
*
7474
* @return StructureElement self reference
7575
*/
@@ -92,8 +92,8 @@ abstract protected function new_instance(): StructureElement;
9292
/**
9393
* Parse common fields to give context.
9494
*
95-
* @param object $object APIB object
96-
* @param array $dependencies Object dependencies
95+
* @param object $object APIB object
96+
* @param string[] $dependencies Object dependencies
9797
*
9898
* @return void
9999
*/

src/PHPDraft/Model/Elements/ElementStructureElement.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ class ElementStructureElement implements StructureElement
77
/**
88
* Object JSON type.
99
*
10-
* @var string|null
10+
* @var string
1111
*/
12-
public $type;
12+
public string $type;
1313

1414
/**
1515
* Object description.
1616
*
1717
* @var string|null
1818
*/
19-
public $description;
19+
public ?string $description = null;
2020

2121
/**
2222
* Object value.
@@ -26,11 +26,14 @@ class ElementStructureElement implements StructureElement
2626
public $value = null;
2727

2828
/**
29-
* @param object|null $object
30-
* @param array $dependencies
31-
* @return StructureElement
29+
* Parse a JSON object to a structure.
30+
*
31+
* @param object|null $object An object to parse
32+
* @param string[] $dependencies Dependencies of this object
33+
*
34+
* @return self self reference
3235
*/
33-
public function parse(?object $object, array &$dependencies): StructureElement
36+
public function parse(?object $object, array &$dependencies): self
3437
{
3538
if (!in_array($object->element, self::DEFAULTS)) {
3639
$dependencies[] = $object->element;
@@ -77,8 +80,7 @@ public function string_value(bool $flat = false)
7780
*/
7881
protected function get_element_as_html(?string $element): string
7982
{
80-
if ($element === null)
81-
{
83+
if ($element === null) {
8284
return '<code>null</code>';
8385
}
8486

src/PHPDraft/Model/Elements/EnumStructureElement.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class EnumStructureElement extends BasicStructureElement
2020
* Parse an array object.
2121
*
2222
* @param object|null $object APIB Item to parse
23-
* @param array $dependencies List of dependencies build
23+
* @param string[] $dependencies List of dependencies build
2424
*
2525
* @return $this
2626
*/
@@ -79,23 +79,19 @@ public function parse(?object $object, array &$dependencies): StructureElement
7979
*/
8080
public function __toString(): string
8181
{
82-
if (is_string($this->value)) {
83-
$type = $this->get_element_as_html($this->element);
84-
$desc = '';
85-
if ($this->description !== NULL)
86-
{
87-
$desc = MarkdownExtra::defaultTransform($this->description);
82+
if (is_iterable($this->value)) {
83+
$return = '';
84+
foreach ($this->value as $item) {
85+
$return .= $item->__toString();
8886
}
8987

90-
return "<tr><td>{$this->key->value}</td><td>{$type}</td><td>{$desc}</td></tr>";
88+
return '<ul class="list-group mdl-list">' . $return . '</ul>';
9189
}
9290

93-
$return = '';
94-
foreach ($this->value as $item) {
95-
$return .= $item->__toString();
96-
}
91+
$type = $this->get_element_as_html($this->element);
92+
$desc = $this->description === null ? '' : MarkdownExtra::defaultTransform($this->description);
9793

98-
return '<ul class="list-group mdl-list">' . $return . '</ul>';
94+
return "<tr><td>{$this->key->value}</td><td>{$type}</td><td>{$desc}</td></tr>";
9995
}
10096

10197
/**

src/PHPDraft/Model/Elements/ObjectStructureElement.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
namespace PHPDraft\Model\Elements;
1414

1515
use Michelf\MarkdownExtra;
16-
use stdClass;
1716

1817
/**
1918
* Class ObjectStructureElement.
@@ -22,10 +21,10 @@ class ObjectStructureElement extends BasicStructureElement
2221
{
2322
/**
2423
* Object representation before parsing
25-
* @var stdClass|null
24+
* @var object|null
2625
* @phpstan-ignore-next-line
2726
*/
28-
private $object;
27+
private ?object $object;
2928

3029
/**
3130
* Unset object function.
@@ -40,7 +39,7 @@ public function __clearForTest(): void
4039
* Parse a JSON object to a data structure.
4140
*
4241
* @param object|null $object An object to parse
43-
* @param array $dependencies Dependencies of this object
42+
* @param string[] $dependencies Dependencies of this object
4443
*
4544
* @return ObjectStructureElement self reference
4645
*/
@@ -84,8 +83,8 @@ public function parse(?object $object, array &$dependencies): StructureElement
8483
/**
8584
* Parse $this->value as a structure based on given content.
8685
*
87-
* @param object $object APIB content
88-
* @param array $dependencies Object dependencies
86+
* @param object $object APIB content
87+
* @param string[] $dependencies Object dependencies
8988
*
9089
* @return void
9190
*/
@@ -118,8 +117,8 @@ protected function new_instance(): StructureElement
118117
/**
119118
* Parse content formed as an array.
120119
*
121-
* @param object $object APIB content
122-
* @param array $dependencies Object dependencies
120+
* @param object $object APIB content
121+
* @param string[] $dependencies Object dependencies
123122
*
124123
* @return void
125124
*/
@@ -211,8 +210,7 @@ protected function construct_string_return(string $value): string
211210
$variable = '<a class="variable-key" title="' . $this->key->type . '" href="#object-' . $link_name . '"><span class="fas fa-info variable-info" data-toggle="tooltip" data-placement="top" data-tooltip="' . $tooltip . '" title="' . $tooltip . '"></span></a>';
212211
}
213212
$desc = '';
214-
if ($this->description !== NULL)
215-
{
213+
if ($this->description !== null) {
216214
$desc = MarkdownExtra::defaultTransform($this->description);
217215
}
218216

src/PHPDraft/Model/Elements/RequestBodyElement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ public function print_request(?string $type = 'application/x-www-form-urlencoded
5353

5454
switch ($type) {
5555
case 'application/x-www-form-urlencoded':
56-
return $this->key . '=<span>' . $value . '</span>';
56+
return "{$this->key->value}=<span>$value</span>";
5757
default:
5858
$object = [];
59-
$object[$this->key] = $value;
59+
$object[$this->key->value] = $value;
6060

6161
return json_encode($object);
6262
}

src/PHPDraft/Model/Elements/StructureElement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface StructureElement
2525
* Parse a JSON object to a structure.
2626
*
2727
* @param object|null $object An object to parse
28-
* @param array $dependencies Dependencies of this object
28+
* @param string[] $dependencies Dependencies of this object
2929
*
3030
* @return self self reference
3131
*/

src/PHPDraft/Model/Elements/Tests/ArrayStructureElementTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function parseObjectProvider(): array
7070
$base1->type = null;
7171
$base1->is_variable = false;
7272
$base1->description = null;
73+
$base1->ref = null;
7374
$base1->deps = [];
7475

7576
$base2 = new ArrayStructureElement();
@@ -87,6 +88,7 @@ public function parseObjectProvider(): array
8788
$base2->is_variable = false;
8889
$base2->description = null;
8990
$base2->deps = ['Some simple array'];
91+
$base2->ref = null;
9092

9193
$base3 = new ArrayStructureElement();
9294
$base3->key = new ElementStructureElement();
@@ -105,6 +107,7 @@ public function parseObjectProvider(): array
105107
$base3->is_variable = false;
106108
$base3->description = "List of car identifiers to retrieve";
107109
$base3->deps = [];
110+
$base3->ref = null;
108111

109112
$return['generic value type'] = [
110113
'{
@@ -190,6 +193,7 @@ public function testToStringWithArray(): void
190193
$val1 = new ElementStructureElement();
191194
$val1->type = 'string';
192195
$val1->value = 'stuff';
196+
$val1->description = null;
193197
$val2 = new ElementStructureElement();
194198
$val2->type = 'int';
195199
$val2->value = 'class';
@@ -207,6 +211,7 @@ public function testToStringWithComplexArray(): void
207211
$val1 = new ElementStructureElement();
208212
$val1->type = 'Bike';
209213
$val1->value = 'type';
214+
$val1->description = null;
210215
$val2 = new ElementStructureElement();
211216
$val2->type = 'car';
212217
$val2->value = 'stuff';

0 commit comments

Comments
 (0)