forked from ILIAS-eLearning/ILIAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Factory.php
184 lines (160 loc) · 4.77 KB
/
Factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
declare(strict_types=1);
/* Copyright (c) 2017 Richard Klees <[email protected]> Extended GPL, see docs/LICENSE */
namespace ILIAS\Data;
use ILIAS\Data\Clock\ClockFactory;
use ILIAS\Data\Clock\ClockFactoryImpl;
/**
* Builds data types.
*
* @author Richard Klees <[email protected]>
* @author Stefan Hecken <[email protected]>
* @author Nils Haagen <[email protected]>
* @author Michael Jansen <[email protected]>
*/
class Factory
{
/**
* cache for color factory.
*/
private ?Color\Factory $colorfactory = null;
private ?Dimension\Factory $dimensionfactory = null;
/**
* Get an ok result.
*
* @param mixed $value
*/
public function ok($value): Result
{
return new Result\Ok($value);
}
/**
* Get an error result.
*
* @param string|\Exception $e
* @return Result
*/
public function error($e): Result
{
return new Result\Error($e);
}
/**
* Color is a data type representing a color in HTML.
* Construct a color with a hex-value or list of RGB-values.
*
* @param string|int[] $value
*/
public function color($value): Color
{
if (!$this->colorfactory) {
$this->colorfactory = new Color\Factory();
}
return $this->colorfactory->build($value);
}
/**
* Object representing an uri valid according to RFC 3986
* with restrictions imposed on valid characters and obliagtory
* parts.
*/
public function uri(string $uri_string): URI
{
return new URI($uri_string);
}
/**
* Represents the size of some data.
*
* @param string|int $size string might be a string like "126 MB"
* @throw \InvalidArgumentException if first argument is int and second is not a valid unit.
* @throw \InvalidArgumentException if string size can't be interpreted
*/
public function dataSize($size, string $unit = null): DataSize
{
if (is_string($size)) {
$match = [];
if (!preg_match("/(\d+)\s*([a-zA-Z]+)/", $size, $match)) {
throw new \InvalidArgumentException("'$size' can't be interpreted as data size.");
}
return $this->dataSize((int) $match[1], $match[2]);
}
if (is_int($size) && (is_null($unit) || !array_key_exists($unit, DataSize::$abbreviations))) {
throw new \InvalidArgumentException(
"Expected second argument to be a unit for data, '$unit' is unknown."
);
}
$unit_size = DataSize::$abbreviations[$unit];
return new DataSize($size * $unit_size, $unit_size);
}
public function password(string $pass): Password
{
return new Password($pass);
}
public function clientId(string $clientId): ClientId
{
return new ClientId($clientId);
}
public function refId(int $ref_id): ReferenceId
{
return new ReferenceId($ref_id);
}
public function objId(int $obj_id): ObjectId
{
return new ObjectId($obj_id);
}
/**
* @param mixed $value
*/
public function alphanumeric($value): Alphanumeric
{
return new Alphanumeric($value);
}
public function positiveInteger(int $value): PositiveInteger
{
return new PositiveInteger($value);
}
public function dateFormat(): DateFormat\Factory
{
$builder = new DateFormat\FormatBuilder();
return new DateFormat\Factory($builder);
}
public function range(int $start, int $length): Range
{
return new Range($start, $length);
}
/**
* @param string $direction Order::ASC|Order::DESC
*/
public function order(string $subject, string $direction): Order
{
return new Order($subject, $direction);
}
/**
* @param string $version in the form \d+([.]\d+([.]\d+)?)?
* @throws \InvalidArgumentException if version string does not match \d+([.]\d+([.]\d+)?)?
*/
public function version(string $version): Version
{
return new Version($version);
}
public function link(string $label, URI $url): Link
{
return new Link($label, $url);
}
public function clock(): ClockFactory
{
return new ClockFactoryImpl();
}
public function dimension(): Dimension\Factory
{
if (!$this->dimensionfactory) {
$this->dimensionfactory = new Dimension\Factory();
}
return $this->dimensionfactory;
}
/**
* @param array<string, Dimension\Dimension> $dimensions Dimensions with their names as keys
*/
public function dataset(array $dimensions): Chart\Dataset
{
return new Chart\Dataset($dimensions);
}
}