forked from ILIAS-eLearning/ILIAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactory.php
175 lines (159 loc) · 4.28 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
<?php
/* Copyright (c) 2017 Richard Klees <[email protected]> Extended GPL, see docs/LICENSE */
namespace ILIAS\Data;
use ILIAS\Data\Interval\OpenedFloatInterval;
use ILIAS\Data\Interval\OpenedIntegerInterval;
use ILIAS\Data\Interval\ClosedFloatInterval;
use ILIAS\Data\Interval\ClosedIntegerInterval;
/**
* 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 $colorfactory;
/**
* Get an ok result.
*
* @param mixed $value
* @return Result
*/
public function ok($value)
{
return new Result\Ok($value);
}
/**
* Get an error result.
*
* @param string|\Exception $error
* @return Result
*/
public function error($e)
{
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
* @return Color
*/
public function color($value)
{
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.
*
* @param string $uri_string
* @return URI
*/
public function uri($uri_string)
{
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 \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);
}
/**
* Get a password.
*
* @param string
* @return Password
*/
public function password($pass)
{
return new Password($pass);
}
/**
* @param string $clientId
* @return ClientId
*/
public function clientId(string $clientId) : ClientId
{
return new ClientId($clientId);
}
/**
* @param int $ref_id
*
* @return ReferenceId
*/
public function refId(int $ref_id) : ReferenceId
{
return new ReferenceId($ref_id);
}
/**
* @param $value
* @return Alphanumeric
*/
public function alphanumeric($value) : Alphanumeric
{
return new Alphanumeric($value);
}
/**
* @param int $value
* @return PositiveInteger
*/
public function positiveInteger(int $value) : PositiveInteger
{
return new PositiveInteger($value);
}
/**
* @return DateFormat\Factory
*/
public function dateFormat() : DateFormat\Factory
{
$builder = new DateFormat\FormatBuilder();
return new DateFormat\Factory($builder);
}
/**
* @param int $start
* @param int $length
* @return Range
*/
public function range(int $start, int $length) : Range
{
return new Range($start, $length);
}
/**
* @param mixed $direction Order::ASC|Order::DESC
*/
public function order(string $subject, $direction) : Order
{
return new Order($subject, $direction);
}
}