1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-resource |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Resource |
8
|
|
|
* @subpackage Apparat\Resource\Infrastructure |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Apparat\Resource\Infrastructure\Model\Resource; |
38
|
|
|
|
39
|
|
|
use Apparat\Resource\Domain\Contract\ReaderInterface; |
40
|
|
|
use Apparat\Resource\Domain\Contract\WriterInterface; |
41
|
|
|
use Apparat\Resource\Domain\Model\Hydrator\HydratorInterface; |
42
|
|
|
use Apparat\Resource\Domain\Model\Part\AbstractContentPart; |
43
|
|
|
use Apparat\Resource\Domain\Model\Part\PartChoice; |
44
|
|
|
use Apparat\Resource\Domain\Model\Resource\AbstractResource; |
45
|
|
|
use Apparat\Resource\Infrastructure\Model\Hydrator\CommonMarkHydrator; |
46
|
|
|
use Apparat\Resource\Infrastructure\Model\Hydrator\FrontMarkHydrator; |
47
|
|
|
use Apparat\Resource\Infrastructure\Model\Hydrator\FrontMatterHydrator; |
48
|
|
|
use Apparat\Resource\Infrastructure\Model\Hydrator\JsonHydrator; |
49
|
|
|
use Apparat\Resource\Infrastructure\Model\Hydrator\YamlHydrator; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* FrontMark resource (CommonMark resource with YAML or JSON front matter) |
53
|
|
|
* |
54
|
|
|
* @package Apparat\Resource |
55
|
|
|
* @subpackage Apparat\Resource\Infrastructure |
56
|
|
|
* @method FrontMarkResource appendPart(string $data, string $part = '/') appendPart(string $data, string $part = '/') |
57
|
|
|
* Append content to the resource |
58
|
|
|
* @method FrontMarkResource prependPart(string $data, string $part = '/') prependPart(string $data, string $part = |
59
|
|
|
* '/') Prepend content to the resource |
60
|
|
|
* @method string getHtmlPart(string $part = '/') getHtmlPart(string $part = '/') Get the HTML content of the resource |
61
|
|
|
* @method array getDataPart(string $part = '/') getDataPart(string $part = '/') Get the YAML / JSON front matter data |
62
|
|
|
* of the resource |
63
|
|
|
* @method FrontMarkResource setDataPart(array $data, string $part = '/') setDataPart(array $data, string $part = '/') |
64
|
|
|
* Set the YAML / JSON front matter data of the resource |
65
|
|
|
* @method string getMediaTypePart(string $part = '/') getMediaTypePart(string $part = '/') Get the media type of this |
66
|
|
|
* part |
67
|
|
|
* @method FrontMarkResource from($src, ...$parameters) static from($src, ...$parameters) Instantiate from source |
68
|
|
|
* @method WriterInterface to($target, ...$parameters) to($target, ...$parameters) Write to target |
69
|
|
|
*/ |
70
|
|
|
class FrontMarkResource extends AbstractResource |
71
|
|
|
{ |
72
|
|
|
/** |
73
|
|
|
* Use resource factory methods and properties |
74
|
|
|
*/ |
75
|
|
|
use ResourceTrait; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* FrontMark resource constructor |
79
|
|
|
* |
80
|
|
|
* @param ReaderInterface $reader Reader instance |
81
|
|
|
*/ |
82
|
19 |
|
public function __construct(ReaderInterface $reader = null) |
83
|
|
|
{ |
84
|
19 |
|
parent::__construct( |
85
|
|
|
array( |
86
|
|
|
[ |
87
|
19 |
|
FrontMatterHydrator::FRONTMATTER => [ |
88
|
|
|
[ |
89
|
|
|
JsonHydrator::JSON => JsonHydrator::class, |
90
|
|
|
YamlHydrator::YAML => YamlHydrator::class |
91
|
|
|
], |
92
|
|
|
FrontMatterHydrator::class |
93
|
|
|
], |
94
|
19 |
|
HydratorInterface::STANDARD => CommonMarkHydrator::class, |
95
|
19 |
|
], |
96
|
|
|
FrontMarkHydrator::class |
97
|
|
|
), |
98
|
|
|
$reader |
99
|
|
|
); |
100
|
19 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set the content of the sole part |
104
|
|
|
* |
105
|
|
|
* @param string $data Content |
106
|
|
|
* @return FrontMarkResource Self reference |
107
|
|
|
*/ |
108
|
1 |
|
public function set($data) |
109
|
|
|
{ |
110
|
1 |
|
return $this->setPart($data, '/0/'.HydratorInterface::STANDARD); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Return the sole part's content |
115
|
|
|
* |
116
|
|
|
* @return string Part content |
117
|
|
|
*/ |
118
|
3 |
|
public function get() |
119
|
|
|
{ |
120
|
3 |
|
return $this->getPart('/0/'.HydratorInterface::STANDARD); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Append content to the sole part |
125
|
|
|
* |
126
|
|
|
* @param string $data Contents |
127
|
|
|
* @return FrontMarkResource New part |
128
|
|
|
*/ |
129
|
1 |
|
public function append($data) |
130
|
|
|
{ |
131
|
1 |
|
return $this->appendPart($data, '/0/'.HydratorInterface::STANDARD); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Prepend content to the sole part |
136
|
|
|
* |
137
|
|
|
* @param string $data Contents |
138
|
|
|
* @return FrontMarkResource New text part |
139
|
|
|
*/ |
140
|
1 |
|
public function prepend($data) |
141
|
|
|
{ |
142
|
1 |
|
return $this->prependPart($data, '/0/'.HydratorInterface::STANDARD); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Convert the sole CommonMark source to HTML |
147
|
|
|
* |
148
|
|
|
* @return string CommonMark HTML |
149
|
|
|
*/ |
150
|
1 |
|
public function getHtml() |
151
|
|
|
{ |
152
|
1 |
|
return $this->getHtmlPart('/0/'.HydratorInterface::STANDARD); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Return the unserialized sole data content |
157
|
|
|
* |
158
|
|
|
* @return array Unserialized data content |
159
|
|
|
*/ |
160
|
1 |
|
public function getData() |
161
|
|
|
{ |
162
|
1 |
|
return $this->getDataPart('/0/'.FrontMatterHydrator::FRONTMATTER.'/0/'.PartChoice::WILDCARD); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Set the sole data content |
167
|
|
|
* |
168
|
|
|
* @param array $data New data |
169
|
|
|
* @return AbstractContentPart Self reference |
170
|
|
|
*/ |
171
|
1 |
|
public function setData(array $data) |
172
|
|
|
{ |
173
|
1 |
|
return $this->setDataPart($data, '/0/'.FrontMatterHydrator::FRONTMATTER.'/0/'.PartChoice::WILDCARD); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|