Skip to content

Commit 4bb4964

Browse files
use a new class for W inclusions close #342
This also fix a bug intruduced recently: the `type` key previously used in Servicerender::match() was hydrated by mistake to some Items.
1 parent 8ce04fb commit 4bb4964

13 files changed

+131
-250
lines changed

app/class/Bookmark.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ public function ispublished(): bool
6464
return $this->published;
6565
}
6666

67+
/**
68+
* @return array<string, mixed> Assoc array representing the query
69+
*/
70+
public function querydata(): array
71+
{
72+
parse_str(ltrim($this->query, "?"), $data);
73+
return $data;
74+
}
75+
76+
6777

6878
// _____________________________ G E T __________________________________
6979

app/class/Clock.php

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88

99
class Clock extends Item
1010
{
11-
protected string $fullmatch;
12-
13-
protected string $options;
14-
1511
protected DateTimeInterface $datetime;
1612

1713
protected string $type;
@@ -53,31 +49,18 @@ class Clock extends Item
5349
*
5450
* @param string $type Define if it must display time or date.
5551
* @param Page $page The page which is rendered
56-
* @param string $fullmatch
57-
* @param string $options
5852
*/
5953
public function __construct(
6054
string $type,
6155
Page $page,
62-
string $fullmatch,
63-
string $options,
64-
string $lang = null
56+
?string $lang = null,
57+
string $format = self::SHORT
6558
) {
6659
$this->settype($type);
6760
$datetype = self::TYPES[$this->type];
6861
$this->datetime = $page->$datetype();
69-
$this->fullmatch = $fullmatch;
70-
$this->options = $options;
7162
$this->lang = empty($lang) ? Config::lang() : $lang;
72-
73-
$this->readoptions();
74-
}
75-
76-
protected function readoptions(): void
77-
{
78-
parse_str(htmlspecialchars_decode($this->options), $datas);
79-
$datas = array_diff_key($datas, ['type' => 0]); // To avoid erasing type
80-
$this->hydrate($datas);
63+
$this->format = $format;
8164
}
8265

8366
protected function visible(): string
@@ -114,7 +97,7 @@ protected function attribute(): string
11497
/**
11598
* Produce HTML time tag ready to be included.
11699
*/
117-
public function format(): string
100+
public function render(): string
118101
{
119102
$attribute = $this->attribute();
120103
$title = $this->title();
@@ -123,20 +106,6 @@ public function format(): string
123106
return "<time datetime=\"$attribute\" title=\"$title\" class=\"$type\">$visible</time>";
124107
}
125108

126-
127-
128-
// ______________________ G E T _____________________________
129-
130-
public function fullmatch(): string
131-
{
132-
return $this->fullmatch;
133-
}
134-
135-
public function options(): string
136-
{
137-
return $this->options;
138-
}
139-
140109
// _______________________ S E T ______________________________
141110

142111
public function settype(string $type): void

app/class/Element.php

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
*/
88
abstract class Element extends Item
99
{
10-
protected string $fullmatch;
11-
protected string $options;
1210
protected int $everylink = 0;
1311
protected bool $markdown = true;
1412
protected string $content = '';
@@ -27,26 +25,8 @@ abstract class Element extends Item
2725
self::NO_HEADER_ANCHOR, self::HEADER_ANCHOR_LINK, self::HEADER_ANCHOR_HASH
2826
];
2927

30-
// ______________________________________________ F U N ________________________________________________________
31-
32-
33-
34-
abstract protected function analyse(string $pageid): void;
35-
36-
3728
// ______________________________________________ G E T ________________________________________________________
3829

39-
40-
public function fullmatch(): string
41-
{
42-
return $this->fullmatch;
43-
}
44-
45-
public function options(): string
46-
{
47-
return $this->options;
48-
}
49-
5030
public function everylink(): int
5131
{
5232
return $this->everylink;
@@ -95,18 +75,6 @@ public function urllinker(): bool
9575
// ______________________________________________ S E T ________________________________________________________
9676

9777

98-
public function setfullmatch(string $fullmatch): void
99-
{
100-
$this->fullmatch = $fullmatch;
101-
}
102-
103-
public function setoptions(string $options): void
104-
{
105-
if (!empty($options)) {
106-
$this->options = $options;
107-
}
108-
}
109-
11078
public function seteverylink(int $level): bool
11179
{
11280
if ($level >= 0 && $level <= 16) {

app/class/Elementv1.php

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,22 @@
77
class Elementv1 extends Element
88
{
99
/** @var string[] */
10-
protected array $sources = [];
10+
protected array $id = [];
1111
protected string $type;
1212

1313
/** @var bool Include element with HTML tags */
1414
protected bool $tag;
1515

16-
17-
public function __construct(string $pageid, string $fullmatch, string $type, string $options)
16+
public function __construct(string $pageid, string $type)
1817
{
18+
$this->id = [$pageid]; // default source is current page
1919
$this->tag = Config::htmltag();
2020
$this->urllinker = Config::urllinker();
21-
$this->fullmatch = $fullmatch;
22-
$type = strtolower($type);
21+
$type = strtolower($type);
2322
if (in_array($type, Pagev1::HTML_ELEMENTS)) {
2423
$this->type = $type;
2524
} else {
26-
throw new DomainException("$type is not a valid Page HTML Element Type");
27-
}
28-
$this->options = $options;
29-
$this->analyse($pageid);
30-
}
31-
32-
protected function analyse(string $pageid): void
33-
{
34-
if (!empty($this->options)) {
35-
$this->options = str_replace('*', $pageid, $this->options);
36-
parse_str($this->options, $datas);
37-
if (isset($datas['id'])) {
38-
$this->sources = explode(' ', $datas['id']);
39-
} else {
40-
$this->sources = [$pageid];
41-
}
42-
$this->hydrate($datas);
43-
} else {
44-
$this->sources = [$pageid];
25+
throw new DomainException("invalid element type inclusion: $type");
4526
}
4627
}
4728

@@ -51,9 +32,9 @@ protected function analyse(string $pageid): void
5132
/**
5233
* @return string[]
5334
*/
54-
public function sources(): array
35+
public function id(): array
5536
{
56-
return $this->sources;
37+
return $this->id;
5738
}
5839

5940
public function type(): string
@@ -66,6 +47,20 @@ public function tag(): bool
6647
return $this->tag;
6748
}
6849

50+
// ______________________________________________ S E T ________________________________________________________
51+
52+
/**
53+
* @param string[]|string $sources if provided as string, multiple IDs may be space separated
54+
*/
55+
public function setid($sources): void
56+
{
57+
if (is_string($sources)) {
58+
$this->id = explode(' ', $sources);
59+
} elseif (is_array($sources)) {
60+
$this->id = $sources;
61+
}
62+
}
63+
6964
public function settag(bool $tag): void
7065
{
7166
$this->tag = $tag;

app/class/Elementv2.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,10 @@ class Elementv2 extends Element
66
{
77
protected string $id;
88

9-
public function __construct(string $pageid, string $fullmatch, string $options)
9+
public function __construct(string $pageid)
1010
{
1111
$this->urllinker = Config::urllinker();
12-
$this->fullmatch = $fullmatch;
1312
$this->id = $pageid;
14-
$this->options = $options;
15-
$this->analyse($pageid);
16-
}
17-
18-
protected function analyse(string $pageid): void
19-
{
20-
parse_str($this->options, $datas);
21-
$this->hydrate($datas);
2213
}
2314

2415

app/class/Inclusion.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Wcms;
4+
5+
class Inclusion
6+
{
7+
protected string $fullmatch;
8+
protected string $type;
9+
/** @var string $options string encoded assoc array without `?` as first char */
10+
protected string $options;
11+
12+
/**
13+
* wildcard `*` in inclusion options is replaced by current page
14+
*/
15+
public function __construct(string $fullmatch, string $type, string $options, Page $currentpage)
16+
{
17+
$this->fullmatch = $fullmatch;
18+
$this->type = $type;
19+
$this->options = str_replace('*', $currentpage->id(), $options);
20+
}
21+
22+
/**
23+
* @return array<string, mixed> decoded options as assoc array
24+
*/
25+
public function readoptions(): array
26+
{
27+
parse_str(htmlspecialchars_decode($this->options), $datas);
28+
return $datas;
29+
}
30+
31+
public function fullmatch(): string
32+
{
33+
return $this->fullmatch;
34+
}
35+
36+
public function type(): string
37+
{
38+
return $this->type;
39+
}
40+
}

app/class/Mediaoptlist.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66

77
class Mediaoptlist extends Mediaopt
88
{
9-
/** @var string full regex match */
10-
protected $fullmatch;
11-
12-
/** @var string full options code line */
13-
protected $options = '';
14-
159
/** @var bool display the file name of the file */
1610
protected bool $filename = false;
1711

@@ -23,12 +17,6 @@ public function __construct($datas = [])
2317
parent::__construct($datas);
2418
}
2519

26-
public function readoptions(): void
27-
{
28-
parse_str(htmlspecialchars_decode($this->options), $datas);
29-
$this->hydrate($datas);
30-
}
31-
3220
/**
3321
* Generate HTML displaying list of medias
3422
*
@@ -101,16 +89,6 @@ public function getaddress(): string
10189
// ______________________________________________ G E T ________________________________________________________
10290

10391

104-
public function fullmatch(): string
105-
{
106-
return $this->fullmatch;
107-
}
108-
109-
public function options(): string
110-
{
111-
return $this->options;
112-
}
113-
11492
public function filename(): bool
11593
{
11694
return $this->filename;
@@ -119,19 +97,6 @@ public function filename(): bool
11997
// ______________________________________________ S E T ________________________________________________________
12098

12199

122-
public function setfullmatch(string $fullmatch): void
123-
{
124-
$this->fullmatch = $fullmatch;
125-
}
126-
127-
128-
public function setoptions(string $options): void
129-
{
130-
if (!empty($options)) {
131-
$this->options = $options;
132-
}
133-
}
134-
135100
public function setfilename(bool $filename): void
136101
{
137102
$this->filename = $filename;

app/class/Optcode.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,6 @@ class Optcode extends Opt
77
/** @var string $bookmark Associated bookmark ID */
88
protected string $bookmark = "";
99

10-
/**
11-
* Parse datas from HTTP encoded params, then hydrate the object.
12-
* This method also transform `*` into current page.
13-
*
14-
* @param string $encoded Options encoded as HTTP params. May start with a `?` or not.
15-
* @param Page $currentpage Used to substitute wildcard `*` with Page id.
16-
*/
17-
public function parsehydrate(string $encoded, Page $currentpage): void
18-
{
19-
$encoded = str_replace('*', $currentpage->id(), $encoded);
20-
parse_str(ltrim($encoded, "?"), $datas);
21-
$this->hydrate($datas);
22-
}
23-
2410
public function bookmark(): string
2511
{
2612
return $this->bookmark;

0 commit comments

Comments
 (0)