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\Ports; |
38
|
|
|
|
39
|
|
|
use Apparat\Kernel\Ports\Kernel; |
40
|
|
|
use Apparat\Resource\Domain\Contract\ReaderInterface; |
41
|
|
|
use Apparat\Resource\Domain\Model\Resource\AbstractResource; |
42
|
|
|
use Apparat\Resource\Infrastructure\Model\Resource\CommonMarkResource; |
43
|
|
|
use Apparat\Resource\Infrastructure\Model\Resource\FrontMarkResource; |
44
|
|
|
use Apparat\Resource\Infrastructure\Model\Resource\JsonResource; |
45
|
|
|
use Apparat\Resource\Infrastructure\Model\Resource\TextResource; |
46
|
|
|
use Apparat\Resource\Infrastructure\Model\Resource\YamlResource; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Resource factory |
50
|
|
|
* |
51
|
|
|
* @package Apparat\Resource |
52
|
|
|
* @subpackage Apparat\Resource\Ports |
53
|
|
|
*/ |
54
|
|
|
class Resource |
55
|
|
|
{ |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create and return a text resource instance |
59
|
|
|
* |
60
|
|
|
* @param string $src Stream-wrapped source |
61
|
|
|
* @param array $parameters Reader parameters |
62
|
|
|
* @return TextResource Text resource instance |
63
|
|
|
* @api |
64
|
|
|
*/ |
65
|
6 |
|
public static function text($src, ...$parameters) |
66
|
|
|
{ |
67
|
6 |
|
return self::fromSource($src, TextResource::class, ...$parameters); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create a reader instance from a stream-wrapped source |
72
|
|
|
* |
73
|
|
|
* @param string $src Stream-wrapped source |
74
|
|
|
* @param string $resourceClass Resource class name |
75
|
|
|
* @param array $parameters Reader parameters |
76
|
|
|
* @return AbstractResource Resource instance |
77
|
|
|
* @throws InvalidReaderArgumentException If an invalid reader stream wrapper is given |
78
|
|
|
*/ |
79
|
11 |
|
protected static function fromSource($src, $resourceClass, ...$parameters) |
80
|
|
|
{ |
81
|
11 |
|
$reader = Tools::reader($src, $parameters); |
82
|
11 |
|
if ($reader instanceof ReaderInterface) { |
83
|
10 |
|
return Kernel::create($resourceClass, [$reader]); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
throw new InvalidArgumentException( |
87
|
1 |
|
'Invalid reader stream wrapper', |
88
|
1 |
|
InvalidReaderArgumentException::INVALID_READER_STREAM_WRAPPER |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Create and return a YAML resource instance |
94
|
|
|
* |
95
|
|
|
* @param string $src Stream-wrapped source |
96
|
|
|
* @param array $parameters Reader parameters |
97
|
|
|
* @return YamlResource YAML resource instance |
98
|
|
|
* @api |
99
|
|
|
*/ |
100
|
1 |
|
public static function yaml($src, ...$parameters) |
101
|
|
|
{ |
102
|
1 |
|
return self::fromSource($src, YamlResource::class, ...$parameters); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Create and return a JSON resource instance |
107
|
|
|
* |
108
|
|
|
* @param string $src Stream-wrapped source |
109
|
|
|
* @param array $parameters Reader parameters |
110
|
|
|
* @return JsonResource JSON resource instance |
111
|
|
|
* @api |
112
|
|
|
*/ |
113
|
1 |
|
public static function json($src, ...$parameters) |
114
|
|
|
{ |
115
|
1 |
|
return self::fromSource($src, JsonResource::class, ...$parameters); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Create and return a CommonMark resource instance |
120
|
|
|
* |
121
|
|
|
* @param string $src Stream-wrapped source |
122
|
|
|
* @param array $parameters Reader parameters |
123
|
|
|
* @return CommonMarkResource CommonMark resource instance |
124
|
|
|
* @api |
125
|
|
|
*/ |
126
|
1 |
|
public static function commonMark($src, ...$parameters) |
127
|
|
|
{ |
128
|
1 |
|
return self::fromSource($src, CommonMarkResource::class, ...$parameters); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/******************************************************************************* |
132
|
|
|
* STATIC METHODS |
133
|
|
|
*******************************************************************************/ |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Create and return a FrontMark resource instance |
137
|
|
|
* |
138
|
|
|
* @param string $src Stream-wrapped source |
139
|
|
|
* @param array $parameters Reader parameters |
140
|
|
|
* @return FrontMarkResource FrontMark resource instance |
141
|
|
|
* @api |
142
|
|
|
*/ |
143
|
2 |
|
public static function frontMark($src, ...$parameters) |
144
|
|
|
{ |
145
|
2 |
|
return self::fromSource($src, FrontMarkResource::class, ...$parameters); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|