@@ -22,80 +22,80 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | function resolve($basePath, $newPath) { |
| 24 | 24 | |
| 25 | - $base = parse($basePath); |
|
| 26 | - $delta = parse($newPath); |
|
| 27 | - |
|
| 28 | - $pick = function($part) use ($base, $delta) { |
|
| 29 | - |
|
| 30 | - if ($delta[$part]) { |
|
| 31 | - return $delta[$part]; |
|
| 32 | - } elseif ($base[$part]) { |
|
| 33 | - return $base[$part]; |
|
| 34 | - } |
|
| 35 | - return null; |
|
| 36 | - |
|
| 37 | - }; |
|
| 38 | - |
|
| 39 | - // If the new path defines a scheme, it's absolute and we can just return |
|
| 40 | - // that. |
|
| 41 | - if ($delta['scheme']) { |
|
| 42 | - return build($delta); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - $newParts = []; |
|
| 46 | - |
|
| 47 | - $newParts['scheme'] = $pick('scheme'); |
|
| 48 | - $newParts['host'] = $pick('host'); |
|
| 49 | - $newParts['port'] = $pick('port'); |
|
| 50 | - |
|
| 51 | - $path = ''; |
|
| 52 | - if ($delta['path']) { |
|
| 53 | - // If the path starts with a slash |
|
| 54 | - if ($delta['path'][0] === '/') { |
|
| 55 | - $path = $delta['path']; |
|
| 56 | - } else { |
|
| 57 | - // Removing last component from base path. |
|
| 58 | - $path = $base['path']; |
|
| 59 | - if (strpos($path, '/') !== false) { |
|
| 60 | - $path = substr($path, 0, strrpos($path, '/')); |
|
| 61 | - } |
|
| 62 | - $path .= '/' . $delta['path']; |
|
| 63 | - } |
|
| 64 | - } else { |
|
| 65 | - $path = $base['path'] ?: '/'; |
|
| 66 | - } |
|
| 67 | - // Removing .. and . |
|
| 68 | - $pathParts = explode('/', $path); |
|
| 69 | - $newPathParts = []; |
|
| 70 | - foreach ($pathParts as $pathPart) { |
|
| 71 | - |
|
| 72 | - switch ($pathPart) { |
|
| 73 | - //case '' : |
|
| 74 | - case '.' : |
|
| 75 | - break; |
|
| 76 | - case '..' : |
|
| 77 | - array_pop($newPathParts); |
|
| 78 | - break; |
|
| 79 | - default : |
|
| 80 | - $newPathParts[] = $pathPart; |
|
| 81 | - break; |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - $path = implode('/', $newPathParts); |
|
| 86 | - |
|
| 87 | - // If the source url ended with a /, we want to preserve that. |
|
| 88 | - $newParts['path'] = $path; |
|
| 89 | - if ($delta['query']) { |
|
| 90 | - $newParts['query'] = $delta['query']; |
|
| 91 | - } elseif (!empty($base['query']) && empty($delta['host']) && empty($delta['path'])) { |
|
| 92 | - // Keep the old query if host and path didn't change |
|
| 93 | - $newParts['query'] = $base['query']; |
|
| 94 | - } |
|
| 95 | - if ($delta['fragment']) { |
|
| 96 | - $newParts['fragment'] = $delta['fragment']; |
|
| 97 | - } |
|
| 98 | - return build($newParts); |
|
| 25 | + $base = parse($basePath); |
|
| 26 | + $delta = parse($newPath); |
|
| 27 | + |
|
| 28 | + $pick = function($part) use ($base, $delta) { |
|
| 29 | + |
|
| 30 | + if ($delta[$part]) { |
|
| 31 | + return $delta[$part]; |
|
| 32 | + } elseif ($base[$part]) { |
|
| 33 | + return $base[$part]; |
|
| 34 | + } |
|
| 35 | + return null; |
|
| 36 | + |
|
| 37 | + }; |
|
| 38 | + |
|
| 39 | + // If the new path defines a scheme, it's absolute and we can just return |
|
| 40 | + // that. |
|
| 41 | + if ($delta['scheme']) { |
|
| 42 | + return build($delta); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + $newParts = []; |
|
| 46 | + |
|
| 47 | + $newParts['scheme'] = $pick('scheme'); |
|
| 48 | + $newParts['host'] = $pick('host'); |
|
| 49 | + $newParts['port'] = $pick('port'); |
|
| 50 | + |
|
| 51 | + $path = ''; |
|
| 52 | + if ($delta['path']) { |
|
| 53 | + // If the path starts with a slash |
|
| 54 | + if ($delta['path'][0] === '/') { |
|
| 55 | + $path = $delta['path']; |
|
| 56 | + } else { |
|
| 57 | + // Removing last component from base path. |
|
| 58 | + $path = $base['path']; |
|
| 59 | + if (strpos($path, '/') !== false) { |
|
| 60 | + $path = substr($path, 0, strrpos($path, '/')); |
|
| 61 | + } |
|
| 62 | + $path .= '/' . $delta['path']; |
|
| 63 | + } |
|
| 64 | + } else { |
|
| 65 | + $path = $base['path'] ?: '/'; |
|
| 66 | + } |
|
| 67 | + // Removing .. and . |
|
| 68 | + $pathParts = explode('/', $path); |
|
| 69 | + $newPathParts = []; |
|
| 70 | + foreach ($pathParts as $pathPart) { |
|
| 71 | + |
|
| 72 | + switch ($pathPart) { |
|
| 73 | + //case '' : |
|
| 74 | + case '.' : |
|
| 75 | + break; |
|
| 76 | + case '..' : |
|
| 77 | + array_pop($newPathParts); |
|
| 78 | + break; |
|
| 79 | + default : |
|
| 80 | + $newPathParts[] = $pathPart; |
|
| 81 | + break; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + $path = implode('/', $newPathParts); |
|
| 86 | + |
|
| 87 | + // If the source url ended with a /, we want to preserve that. |
|
| 88 | + $newParts['path'] = $path; |
|
| 89 | + if ($delta['query']) { |
|
| 90 | + $newParts['query'] = $delta['query']; |
|
| 91 | + } elseif (!empty($base['query']) && empty($delta['host']) && empty($delta['path'])) { |
|
| 92 | + // Keep the old query if host and path didn't change |
|
| 93 | + $newParts['query'] = $base['query']; |
|
| 94 | + } |
|
| 95 | + if ($delta['fragment']) { |
|
| 96 | + $newParts['fragment'] = $delta['fragment']; |
|
| 97 | + } |
|
| 98 | + return build($newParts); |
|
| 99 | 99 | |
| 100 | 100 | } |
| 101 | 101 | |
@@ -113,55 +113,55 @@ discard block |
||
| 113 | 113 | */ |
| 114 | 114 | function normalize($uri) { |
| 115 | 115 | |
| 116 | - $parts = parse($uri); |
|
| 117 | - |
|
| 118 | - if (!empty($parts['path'])) { |
|
| 119 | - $pathParts = explode('/', ltrim($parts['path'], '/')); |
|
| 120 | - $newPathParts = []; |
|
| 121 | - foreach ($pathParts as $pathPart) { |
|
| 122 | - switch ($pathPart) { |
|
| 123 | - case '.': |
|
| 124 | - // skip |
|
| 125 | - break; |
|
| 126 | - case '..' : |
|
| 127 | - // One level up in the hierarchy |
|
| 128 | - array_pop($newPathParts); |
|
| 129 | - break; |
|
| 130 | - default : |
|
| 131 | - // Ensuring that everything is correctly percent-encoded. |
|
| 132 | - $newPathParts[] = rawurlencode(rawurldecode($pathPart)); |
|
| 133 | - break; |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - $parts['path'] = '/' . implode('/', $newPathParts); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - if ($parts['scheme']) { |
|
| 140 | - $parts['scheme'] = strtolower($parts['scheme']); |
|
| 141 | - $defaultPorts = [ |
|
| 142 | - 'http' => '80', |
|
| 143 | - 'https' => '443', |
|
| 144 | - ]; |
|
| 145 | - |
|
| 146 | - if (!empty($parts['port']) && isset($defaultPorts[$parts['scheme']]) && $defaultPorts[$parts['scheme']] == $parts['port']) { |
|
| 147 | - // Removing default ports. |
|
| 148 | - unset($parts['port']); |
|
| 149 | - } |
|
| 150 | - // A few HTTP specific rules. |
|
| 151 | - switch ($parts['scheme']) { |
|
| 152 | - case 'http' : |
|
| 153 | - case 'https' : |
|
| 154 | - if (empty($parts['path'])) { |
|
| 155 | - // An empty path is equivalent to / in http. |
|
| 156 | - $parts['path'] = '/'; |
|
| 157 | - } |
|
| 158 | - break; |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - if ($parts['host']) $parts['host'] = strtolower($parts['host']); |
|
| 163 | - |
|
| 164 | - return build($parts); |
|
| 116 | + $parts = parse($uri); |
|
| 117 | + |
|
| 118 | + if (!empty($parts['path'])) { |
|
| 119 | + $pathParts = explode('/', ltrim($parts['path'], '/')); |
|
| 120 | + $newPathParts = []; |
|
| 121 | + foreach ($pathParts as $pathPart) { |
|
| 122 | + switch ($pathPart) { |
|
| 123 | + case '.': |
|
| 124 | + // skip |
|
| 125 | + break; |
|
| 126 | + case '..' : |
|
| 127 | + // One level up in the hierarchy |
|
| 128 | + array_pop($newPathParts); |
|
| 129 | + break; |
|
| 130 | + default : |
|
| 131 | + // Ensuring that everything is correctly percent-encoded. |
|
| 132 | + $newPathParts[] = rawurlencode(rawurldecode($pathPart)); |
|
| 133 | + break; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + $parts['path'] = '/' . implode('/', $newPathParts); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + if ($parts['scheme']) { |
|
| 140 | + $parts['scheme'] = strtolower($parts['scheme']); |
|
| 141 | + $defaultPorts = [ |
|
| 142 | + 'http' => '80', |
|
| 143 | + 'https' => '443', |
|
| 144 | + ]; |
|
| 145 | + |
|
| 146 | + if (!empty($parts['port']) && isset($defaultPorts[$parts['scheme']]) && $defaultPorts[$parts['scheme']] == $parts['port']) { |
|
| 147 | + // Removing default ports. |
|
| 148 | + unset($parts['port']); |
|
| 149 | + } |
|
| 150 | + // A few HTTP specific rules. |
|
| 151 | + switch ($parts['scheme']) { |
|
| 152 | + case 'http' : |
|
| 153 | + case 'https' : |
|
| 154 | + if (empty($parts['path'])) { |
|
| 155 | + // An empty path is equivalent to / in http. |
|
| 156 | + $parts['path'] = '/'; |
|
| 157 | + } |
|
| 158 | + break; |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + if ($parts['host']) $parts['host'] = strtolower($parts['host']); |
|
| 163 | + |
|
| 164 | + return build($parts); |
|
| 165 | 165 | |
| 166 | 166 | } |
| 167 | 167 | |
@@ -180,29 +180,29 @@ discard block |
||
| 180 | 180 | */ |
| 181 | 181 | function parse($uri) { |
| 182 | 182 | |
| 183 | - // Normally a URI must be ASCII, however. However, often it's not and |
|
| 184 | - // parse_url might corrupt these strings. |
|
| 185 | - // |
|
| 186 | - // For that reason we take any non-ascii characters from the uri and |
|
| 187 | - // uriencode them first. |
|
| 188 | - $uri = preg_replace_callback( |
|
| 189 | - '/[^[:ascii:]]/u', |
|
| 190 | - function($matches) { |
|
| 191 | - return rawurlencode($matches[0]); |
|
| 192 | - }, |
|
| 193 | - $uri |
|
| 194 | - ); |
|
| 195 | - |
|
| 196 | - return |
|
| 197 | - parse_url($uri) + [ |
|
| 198 | - 'scheme' => null, |
|
| 199 | - 'host' => null, |
|
| 200 | - 'path' => null, |
|
| 201 | - 'port' => null, |
|
| 202 | - 'user' => null, |
|
| 203 | - 'query' => null, |
|
| 204 | - 'fragment' => null, |
|
| 205 | - ]; |
|
| 183 | + // Normally a URI must be ASCII, however. However, often it's not and |
|
| 184 | + // parse_url might corrupt these strings. |
|
| 185 | + // |
|
| 186 | + // For that reason we take any non-ascii characters from the uri and |
|
| 187 | + // uriencode them first. |
|
| 188 | + $uri = preg_replace_callback( |
|
| 189 | + '/[^[:ascii:]]/u', |
|
| 190 | + function($matches) { |
|
| 191 | + return rawurlencode($matches[0]); |
|
| 192 | + }, |
|
| 193 | + $uri |
|
| 194 | + ); |
|
| 195 | + |
|
| 196 | + return |
|
| 197 | + parse_url($uri) + [ |
|
| 198 | + 'scheme' => null, |
|
| 199 | + 'host' => null, |
|
| 200 | + 'path' => null, |
|
| 201 | + 'port' => null, |
|
| 202 | + 'user' => null, |
|
| 203 | + 'query' => null, |
|
| 204 | + 'fragment' => null, |
|
| 205 | + ]; |
|
| 206 | 206 | |
| 207 | 207 | } |
| 208 | 208 | |
@@ -215,41 +215,41 @@ discard block |
||
| 215 | 215 | */ |
| 216 | 216 | function build(array $parts) { |
| 217 | 217 | |
| 218 | - $uri = ''; |
|
| 219 | - |
|
| 220 | - $authority = ''; |
|
| 221 | - if (!empty($parts['host'])) { |
|
| 222 | - $authority = $parts['host']; |
|
| 223 | - if (!empty($parts['user'])) { |
|
| 224 | - $authority = $parts['user'] . '@' . $authority; |
|
| 225 | - } |
|
| 226 | - if (!empty($parts['port'])) { |
|
| 227 | - $authority = $authority . ':' . $parts['port']; |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - if (!empty($parts['scheme'])) { |
|
| 232 | - // If there's a scheme, there's also a host. |
|
| 233 | - $uri = $parts['scheme'] . ':'; |
|
| 234 | - |
|
| 235 | - } |
|
| 236 | - if ($authority) { |
|
| 237 | - // No scheme, but there is a host. |
|
| 238 | - $uri .= '//' . $authority; |
|
| 239 | - |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - if (!empty($parts['path'])) { |
|
| 243 | - $uri .= $parts['path']; |
|
| 244 | - } |
|
| 245 | - if (!empty($parts['query'])) { |
|
| 246 | - $uri .= '?' . $parts['query']; |
|
| 247 | - } |
|
| 248 | - if (!empty($parts['fragment'])) { |
|
| 249 | - $uri .= '#' . $parts['fragment']; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - return $uri; |
|
| 218 | + $uri = ''; |
|
| 219 | + |
|
| 220 | + $authority = ''; |
|
| 221 | + if (!empty($parts['host'])) { |
|
| 222 | + $authority = $parts['host']; |
|
| 223 | + if (!empty($parts['user'])) { |
|
| 224 | + $authority = $parts['user'] . '@' . $authority; |
|
| 225 | + } |
|
| 226 | + if (!empty($parts['port'])) { |
|
| 227 | + $authority = $authority . ':' . $parts['port']; |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + if (!empty($parts['scheme'])) { |
|
| 232 | + // If there's a scheme, there's also a host. |
|
| 233 | + $uri = $parts['scheme'] . ':'; |
|
| 234 | + |
|
| 235 | + } |
|
| 236 | + if ($authority) { |
|
| 237 | + // No scheme, but there is a host. |
|
| 238 | + $uri .= '//' . $authority; |
|
| 239 | + |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + if (!empty($parts['path'])) { |
|
| 243 | + $uri .= $parts['path']; |
|
| 244 | + } |
|
| 245 | + if (!empty($parts['query'])) { |
|
| 246 | + $uri .= '?' . $parts['query']; |
|
| 247 | + } |
|
| 248 | + if (!empty($parts['fragment'])) { |
|
| 249 | + $uri .= '#' . $parts['fragment']; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + return $uri; |
|
| 253 | 253 | |
| 254 | 254 | } |
| 255 | 255 | |
@@ -273,10 +273,10 @@ discard block |
||
| 273 | 273 | */ |
| 274 | 274 | function split($path) { |
| 275 | 275 | |
| 276 | - $matches = []; |
|
| 277 | - if (preg_match('/^(?:(?:(.*)(?:\/+))?([^\/]+))(?:\/?)$/u', $path, $matches)) { |
|
| 278 | - return [$matches[1], $matches[2]]; |
|
| 279 | - } |
|
| 280 | - return [null,null]; |
|
| 276 | + $matches = []; |
|
| 277 | + if (preg_match('/^(?:(?:(.*)(?:\/+))?([^\/]+))(?:\/?)$/u', $path, $matches)) { |
|
| 278 | + return [$matches[1], $matches[2]]; |
|
| 279 | + } |
|
| 280 | + return [null,null]; |
|
| 281 | 281 | |
| 282 | 282 | } |
@@ -46,75 +46,75 @@ |
||
| 46 | 46 | */ |
| 47 | 47 | function coroutine(callable $gen) { |
| 48 | 48 | |
| 49 | - $generator = $gen(); |
|
| 50 | - if (!$generator instanceof Generator) { |
|
| 51 | - throw new \InvalidArgumentException('You must pass a generator function'); |
|
| 52 | - } |
|
| 49 | + $generator = $gen(); |
|
| 50 | + if (!$generator instanceof Generator) { |
|
| 51 | + throw new \InvalidArgumentException('You must pass a generator function'); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - // This is the value we're returning. |
|
| 55 | - $promise = new Promise(); |
|
| 54 | + // This is the value we're returning. |
|
| 55 | + $promise = new Promise(); |
|
| 56 | 56 | |
| 57 | - $lastYieldResult = null; |
|
| 57 | + $lastYieldResult = null; |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * So tempted to use the mythical y-combinator here, but it's not needed in |
|
| 61 | - * PHP. |
|
| 62 | - */ |
|
| 63 | - $advanceGenerator = function() use (&$advanceGenerator, $generator, $promise, &$lastYieldResult) { |
|
| 59 | + /** |
|
| 60 | + * So tempted to use the mythical y-combinator here, but it's not needed in |
|
| 61 | + * PHP. |
|
| 62 | + */ |
|
| 63 | + $advanceGenerator = function() use (&$advanceGenerator, $generator, $promise, &$lastYieldResult) { |
|
| 64 | 64 | |
| 65 | - while ($generator->valid()) { |
|
| 65 | + while ($generator->valid()) { |
|
| 66 | 66 | |
| 67 | - $yieldedValue = $generator->current(); |
|
| 68 | - if ($yieldedValue instanceof Promise) { |
|
| 69 | - $yieldedValue->then( |
|
| 70 | - function($value) use ($generator, &$advanceGenerator, &$lastYieldResult) { |
|
| 71 | - $lastYieldResult = $value; |
|
| 72 | - $generator->send($value); |
|
| 73 | - $advanceGenerator(); |
|
| 74 | - }, |
|
| 75 | - function($reason) use ($generator, $advanceGenerator) { |
|
| 76 | - if ($reason instanceof Exception) { |
|
| 77 | - $generator->throw($reason); |
|
| 78 | - } elseif (is_scalar($reason)) { |
|
| 79 | - $generator->throw(new Exception($reason)); |
|
| 80 | - } else { |
|
| 81 | - $type = is_object($reason) ? get_class($reason) : gettype($reason); |
|
| 82 | - $generator->throw(new Exception('Promise was rejected with reason of type: ' . $type)); |
|
| 83 | - } |
|
| 84 | - $advanceGenerator(); |
|
| 85 | - } |
|
| 86 | - )->error(function($reason) use ($promise) { |
|
| 87 | - // This error handler would be called, if something in the |
|
| 88 | - // generator throws an exception, and it's not caught |
|
| 89 | - // locally. |
|
| 90 | - $promise->reject($reason); |
|
| 91 | - }); |
|
| 92 | - // We need to break out of the loop, because $advanceGenerator |
|
| 93 | - // will be called asynchronously when the promise has a result. |
|
| 94 | - break; |
|
| 95 | - } else { |
|
| 96 | - // If the value was not a promise, we'll just let it pass through. |
|
| 97 | - $lastYieldResult = $yieldedValue; |
|
| 98 | - $generator->send($yieldedValue); |
|
| 99 | - } |
|
| 67 | + $yieldedValue = $generator->current(); |
|
| 68 | + if ($yieldedValue instanceof Promise) { |
|
| 69 | + $yieldedValue->then( |
|
| 70 | + function($value) use ($generator, &$advanceGenerator, &$lastYieldResult) { |
|
| 71 | + $lastYieldResult = $value; |
|
| 72 | + $generator->send($value); |
|
| 73 | + $advanceGenerator(); |
|
| 74 | + }, |
|
| 75 | + function($reason) use ($generator, $advanceGenerator) { |
|
| 76 | + if ($reason instanceof Exception) { |
|
| 77 | + $generator->throw($reason); |
|
| 78 | + } elseif (is_scalar($reason)) { |
|
| 79 | + $generator->throw(new Exception($reason)); |
|
| 80 | + } else { |
|
| 81 | + $type = is_object($reason) ? get_class($reason) : gettype($reason); |
|
| 82 | + $generator->throw(new Exception('Promise was rejected with reason of type: ' . $type)); |
|
| 83 | + } |
|
| 84 | + $advanceGenerator(); |
|
| 85 | + } |
|
| 86 | + )->error(function($reason) use ($promise) { |
|
| 87 | + // This error handler would be called, if something in the |
|
| 88 | + // generator throws an exception, and it's not caught |
|
| 89 | + // locally. |
|
| 90 | + $promise->reject($reason); |
|
| 91 | + }); |
|
| 92 | + // We need to break out of the loop, because $advanceGenerator |
|
| 93 | + // will be called asynchronously when the promise has a result. |
|
| 94 | + break; |
|
| 95 | + } else { |
|
| 96 | + // If the value was not a promise, we'll just let it pass through. |
|
| 97 | + $lastYieldResult = $yieldedValue; |
|
| 98 | + $generator->send($yieldedValue); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - } |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - // If the generator is at the end, and we didn't run into an exception, |
|
| 104 | - // we can fullfill the promise with the last thing that was yielded to |
|
| 105 | - // us. |
|
| 106 | - if (!$generator->valid() && $promise->state === Promise::PENDING) { |
|
| 107 | - $promise->fulfill($lastYieldResult); |
|
| 108 | - } |
|
| 103 | + // If the generator is at the end, and we didn't run into an exception, |
|
| 104 | + // we can fullfill the promise with the last thing that was yielded to |
|
| 105 | + // us. |
|
| 106 | + if (!$generator->valid() && $promise->state === Promise::PENDING) { |
|
| 107 | + $promise->fulfill($lastYieldResult); |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - }; |
|
| 110 | + }; |
|
| 111 | 111 | |
| 112 | - try { |
|
| 113 | - $advanceGenerator(); |
|
| 114 | - } catch (Exception $e) { |
|
| 115 | - $promise->reject($e); |
|
| 116 | - } |
|
| 112 | + try { |
|
| 113 | + $advanceGenerator(); |
|
| 114 | + } catch (Exception $e) { |
|
| 115 | + $promise->reject($e); |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - return $promise; |
|
| 118 | + return $promise; |
|
| 119 | 119 | |
| 120 | 120 | } |
@@ -31,30 +31,30 @@ discard block |
||
| 31 | 31 | */ |
| 32 | 32 | function all(array $promises) { |
| 33 | 33 | |
| 34 | - return new Promise(function($success, $fail) use ($promises) { |
|
| 35 | - |
|
| 36 | - $successCount = 0; |
|
| 37 | - $completeResult = []; |
|
| 38 | - |
|
| 39 | - foreach ($promises as $promiseIndex => $subPromise) { |
|
| 40 | - |
|
| 41 | - $subPromise->then( |
|
| 42 | - function($result) use ($promiseIndex, &$completeResult, &$successCount, $success, $promises) { |
|
| 43 | - $completeResult[$promiseIndex] = $result; |
|
| 44 | - $successCount++; |
|
| 45 | - if ($successCount === count($promises)) { |
|
| 46 | - $success($completeResult); |
|
| 47 | - } |
|
| 48 | - return $result; |
|
| 49 | - } |
|
| 50 | - )->error( |
|
| 51 | - function($reason) use ($fail) { |
|
| 52 | - $fail($reason); |
|
| 53 | - } |
|
| 54 | - ); |
|
| 55 | - |
|
| 56 | - } |
|
| 57 | - }); |
|
| 34 | + return new Promise(function($success, $fail) use ($promises) { |
|
| 35 | + |
|
| 36 | + $successCount = 0; |
|
| 37 | + $completeResult = []; |
|
| 38 | + |
|
| 39 | + foreach ($promises as $promiseIndex => $subPromise) { |
|
| 40 | + |
|
| 41 | + $subPromise->then( |
|
| 42 | + function($result) use ($promiseIndex, &$completeResult, &$successCount, $success, $promises) { |
|
| 43 | + $completeResult[$promiseIndex] = $result; |
|
| 44 | + $successCount++; |
|
| 45 | + if ($successCount === count($promises)) { |
|
| 46 | + $success($completeResult); |
|
| 47 | + } |
|
| 48 | + return $result; |
|
| 49 | + } |
|
| 50 | + )->error( |
|
| 51 | + function($reason) use ($fail) { |
|
| 52 | + $fail($reason); |
|
| 53 | + } |
|
| 54 | + ); |
|
| 55 | + |
|
| 56 | + } |
|
| 57 | + }); |
|
| 58 | 58 | |
| 59 | 59 | } |
| 60 | 60 | |
@@ -70,31 +70,31 @@ discard block |
||
| 70 | 70 | */ |
| 71 | 71 | function race(array $promises) { |
| 72 | 72 | |
| 73 | - return new Promise(function($success, $fail) use ($promises) { |
|
| 73 | + return new Promise(function($success, $fail) use ($promises) { |
|
| 74 | 74 | |
| 75 | - $alreadyDone = false; |
|
| 76 | - foreach ($promises as $promise) { |
|
| 75 | + $alreadyDone = false; |
|
| 76 | + foreach ($promises as $promise) { |
|
| 77 | 77 | |
| 78 | - $promise->then( |
|
| 79 | - function($result) use ($success, &$alreadyDone) { |
|
| 80 | - if ($alreadyDone) { |
|
| 81 | - return; |
|
| 82 | - } |
|
| 83 | - $alreadyDone = true; |
|
| 84 | - $success($result); |
|
| 85 | - }, |
|
| 86 | - function($reason) use ($fail, &$alreadyDone) { |
|
| 87 | - if ($alreadyDone) { |
|
| 88 | - return; |
|
| 89 | - } |
|
| 90 | - $alreadyDone = true; |
|
| 91 | - $fail($reason); |
|
| 92 | - } |
|
| 93 | - ); |
|
| 78 | + $promise->then( |
|
| 79 | + function($result) use ($success, &$alreadyDone) { |
|
| 80 | + if ($alreadyDone) { |
|
| 81 | + return; |
|
| 82 | + } |
|
| 83 | + $alreadyDone = true; |
|
| 84 | + $success($result); |
|
| 85 | + }, |
|
| 86 | + function($reason) use ($fail, &$alreadyDone) { |
|
| 87 | + if ($alreadyDone) { |
|
| 88 | + return; |
|
| 89 | + } |
|
| 90 | + $alreadyDone = true; |
|
| 91 | + $fail($reason); |
|
| 92 | + } |
|
| 93 | + ); |
|
| 94 | 94 | |
| 95 | - } |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - }); |
|
| 97 | + }); |
|
| 98 | 98 | |
| 99 | 99 | } |
| 100 | 100 | |
@@ -110,13 +110,13 @@ discard block |
||
| 110 | 110 | */ |
| 111 | 111 | function resolve($value) { |
| 112 | 112 | |
| 113 | - if ($value instanceof Promise) { |
|
| 114 | - return $value->then(); |
|
| 115 | - } else { |
|
| 116 | - $promise = new Promise(); |
|
| 117 | - $promise->fulfill($value); |
|
| 118 | - return $promise; |
|
| 119 | - } |
|
| 113 | + if ($value instanceof Promise) { |
|
| 114 | + return $value->then(); |
|
| 115 | + } else { |
|
| 116 | + $promise = new Promise(); |
|
| 117 | + $promise->fulfill($value); |
|
| 118 | + return $promise; |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | 121 | } |
| 122 | 122 | |
@@ -128,8 +128,8 @@ discard block |
||
| 128 | 128 | */ |
| 129 | 129 | function reject($reason) { |
| 130 | 130 | |
| 131 | - $promise = new Promise(); |
|
| 132 | - $promise->reject($reason); |
|
| 133 | - return $promise; |
|
| 131 | + $promise = new Promise(); |
|
| 132 | + $promise->reject($reason); |
|
| 133 | + return $promise; |
|
| 134 | 134 | |
| 135 | 135 | } |
@@ -26,379 +26,379 @@ |
||
| 26 | 26 | */ |
| 27 | 27 | class DateTime extends Property { |
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * In case this is a multi-value property. This string will be used as a |
|
| 31 | - * delimiter. |
|
| 32 | - * |
|
| 33 | - * @var string|null |
|
| 34 | - */ |
|
| 35 | - public $delimiter = ','; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Sets a multi-valued property. |
|
| 39 | - * |
|
| 40 | - * You may also specify DateTime objects here. |
|
| 41 | - * |
|
| 42 | - * @param array $parts |
|
| 43 | - * |
|
| 44 | - * @return void |
|
| 45 | - */ |
|
| 46 | - public function setParts(array $parts) { |
|
| 47 | - |
|
| 48 | - if (isset($parts[0]) && $parts[0] instanceof DateTimeInterface) { |
|
| 49 | - $this->setDateTimes($parts); |
|
| 50 | - } else { |
|
| 51 | - parent::setParts($parts); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Updates the current value. |
|
| 58 | - * |
|
| 59 | - * This may be either a single, or multiple strings in an array. |
|
| 60 | - * |
|
| 61 | - * Instead of strings, you may also use DateTime here. |
|
| 62 | - * |
|
| 63 | - * @param string|array|DateTimeInterface $value |
|
| 64 | - * |
|
| 65 | - * @return void |
|
| 66 | - */ |
|
| 67 | - public function setValue($value) { |
|
| 68 | - |
|
| 69 | - if (is_array($value) && isset($value[0]) && $value[0] instanceof DateTimeInterface) { |
|
| 70 | - $this->setDateTimes($value); |
|
| 71 | - } elseif ($value instanceof DateTimeInterface) { |
|
| 72 | - $this->setDateTimes([$value]); |
|
| 73 | - } else { |
|
| 74 | - parent::setValue($value); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Sets a raw value coming from a mimedir (iCalendar/vCard) file. |
|
| 81 | - * |
|
| 82 | - * This has been 'unfolded', so only 1 line will be passed. Unescaping is |
|
| 83 | - * not yet done, but parameters are not included. |
|
| 84 | - * |
|
| 85 | - * @param string $val |
|
| 86 | - * |
|
| 87 | - * @return void |
|
| 88 | - */ |
|
| 89 | - public function setRawMimeDirValue($val) { |
|
| 90 | - |
|
| 91 | - $this->setValue(explode($this->delimiter, $val)); |
|
| 92 | - |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Returns a raw mime-dir representation of the value. |
|
| 97 | - * |
|
| 98 | - * @return string |
|
| 99 | - */ |
|
| 100 | - public function getRawMimeDirValue() { |
|
| 101 | - |
|
| 102 | - return implode($this->delimiter, $this->getParts()); |
|
| 103 | - |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Returns true if this is a DATE-TIME value, false if it's a DATE. |
|
| 108 | - * |
|
| 109 | - * @return bool |
|
| 110 | - */ |
|
| 111 | - public function hasTime() { |
|
| 112 | - |
|
| 113 | - return strtoupper((string)$this['VALUE']) !== 'DATE'; |
|
| 114 | - |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Returns true if this is a floating DATE or DATE-TIME. |
|
| 119 | - * |
|
| 120 | - * Note that DATE is always floating. |
|
| 121 | - */ |
|
| 122 | - public function isFloating() { |
|
| 123 | - |
|
| 124 | - return |
|
| 125 | - !$this->hasTime() || |
|
| 126 | - ( |
|
| 127 | - !isset($this['TZID']) && |
|
| 128 | - strpos($this->getValue(), 'Z') === false |
|
| 129 | - ); |
|
| 130 | - |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Returns a date-time value. |
|
| 135 | - * |
|
| 136 | - * Note that if this property contained more than 1 date-time, only the |
|
| 137 | - * first will be returned. To get an array with multiple values, call |
|
| 138 | - * getDateTimes. |
|
| 139 | - * |
|
| 140 | - * If no timezone information is known, because it's either an all-day |
|
| 141 | - * property or floating time, we will use the DateTimeZone argument to |
|
| 142 | - * figure out the exact date. |
|
| 143 | - * |
|
| 144 | - * @param DateTimeZone $timeZone |
|
| 145 | - * |
|
| 146 | - * @return DateTimeImmutable |
|
| 147 | - */ |
|
| 148 | - public function getDateTime(DateTimeZone $timeZone = null) { |
|
| 149 | - |
|
| 150 | - $dt = $this->getDateTimes($timeZone); |
|
| 151 | - if (!$dt) return; |
|
| 152 | - |
|
| 153 | - return $dt[0]; |
|
| 154 | - |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Returns multiple date-time values. |
|
| 159 | - * |
|
| 160 | - * If no timezone information is known, because it's either an all-day |
|
| 161 | - * property or floating time, we will use the DateTimeZone argument to |
|
| 162 | - * figure out the exact date. |
|
| 163 | - * |
|
| 164 | - * @param DateTimeZone $timeZone |
|
| 165 | - * |
|
| 166 | - * @return DateTimeImmutable[] |
|
| 167 | - * @return \DateTime[] |
|
| 168 | - */ |
|
| 169 | - public function getDateTimes(DateTimeZone $timeZone = null) { |
|
| 170 | - |
|
| 171 | - // Does the property have a TZID? |
|
| 172 | - $tzid = $this['TZID']; |
|
| 173 | - |
|
| 174 | - if ($tzid) { |
|
| 175 | - $timeZone = TimeZoneUtil::getTimeZone((string)$tzid, $this->root); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - $dts = []; |
|
| 179 | - foreach ($this->getParts() as $part) { |
|
| 180 | - $dts[] = DateTimeParser::parse($part, $timeZone); |
|
| 181 | - } |
|
| 182 | - return $dts; |
|
| 183 | - |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Sets the property as a DateTime object. |
|
| 188 | - * |
|
| 189 | - * @param DateTimeInterface $dt |
|
| 190 | - * @param bool isFloating If set to true, timezones will be ignored. |
|
| 191 | - * |
|
| 192 | - * @return void |
|
| 193 | - */ |
|
| 194 | - public function setDateTime(DateTimeInterface $dt, $isFloating = false) { |
|
| 195 | - |
|
| 196 | - $this->setDateTimes([$dt], $isFloating); |
|
| 197 | - |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Sets the property as multiple date-time objects. |
|
| 202 | - * |
|
| 203 | - * The first value will be used as a reference for the timezones, and all |
|
| 204 | - * the otehr values will be adjusted for that timezone |
|
| 205 | - * |
|
| 206 | - * @param DateTimeInterface[] $dt |
|
| 207 | - * @param bool isFloating If set to true, timezones will be ignored. |
|
| 208 | - * |
|
| 209 | - * @return void |
|
| 210 | - */ |
|
| 211 | - public function setDateTimes(array $dt, $isFloating = false) { |
|
| 212 | - |
|
| 213 | - $values = []; |
|
| 214 | - |
|
| 215 | - if ($this->hasTime()) { |
|
| 216 | - |
|
| 217 | - $tz = null; |
|
| 218 | - $isUtc = false; |
|
| 219 | - |
|
| 220 | - foreach ($dt as $d) { |
|
| 221 | - |
|
| 222 | - if ($isFloating) { |
|
| 223 | - $values[] = $d->format('Ymd\\THis'); |
|
| 224 | - continue; |
|
| 225 | - } |
|
| 226 | - if (is_null($tz)) { |
|
| 227 | - $tz = $d->getTimeZone(); |
|
| 228 | - $isUtc = in_array($tz->getName(), ['UTC', 'GMT', 'Z', '+00:00']); |
|
| 229 | - if (!$isUtc) { |
|
| 230 | - $this->offsetSet('TZID', $tz->getName()); |
|
| 231 | - } |
|
| 232 | - } else { |
|
| 233 | - $d = $d->setTimeZone($tz); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - if ($isUtc) { |
|
| 237 | - $values[] = $d->format('Ymd\\THis\\Z'); |
|
| 238 | - } else { |
|
| 239 | - $values[] = $d->format('Ymd\\THis'); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - } |
|
| 243 | - if ($isUtc || $isFloating) { |
|
| 244 | - $this->offsetUnset('TZID'); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - } else { |
|
| 248 | - |
|
| 249 | - foreach ($dt as $d) { |
|
| 250 | - |
|
| 251 | - $values[] = $d->format('Ymd'); |
|
| 252 | - |
|
| 253 | - } |
|
| 254 | - $this->offsetUnset('TZID'); |
|
| 255 | - |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - $this->value = $values; |
|
| 259 | - |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Returns the type of value. |
|
| 264 | - * |
|
| 265 | - * This corresponds to the VALUE= parameter. Every property also has a |
|
| 266 | - * 'default' valueType. |
|
| 267 | - * |
|
| 268 | - * @return string |
|
| 269 | - */ |
|
| 270 | - public function getValueType() { |
|
| 271 | - |
|
| 272 | - return $this->hasTime() ? 'DATE-TIME' : 'DATE'; |
|
| 273 | - |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Returns the value, in the format it should be encoded for JSON. |
|
| 278 | - * |
|
| 279 | - * This method must always return an array. |
|
| 280 | - * |
|
| 281 | - * @return array |
|
| 282 | - */ |
|
| 283 | - public function getJsonValue() { |
|
| 284 | - |
|
| 285 | - $dts = $this->getDateTimes(); |
|
| 286 | - $hasTime = $this->hasTime(); |
|
| 287 | - $isFloating = $this->isFloating(); |
|
| 288 | - |
|
| 289 | - $tz = $dts[0]->getTimeZone(); |
|
| 290 | - $isUtc = $isFloating ? false : in_array($tz->getName(), ['UTC', 'GMT', 'Z']); |
|
| 291 | - |
|
| 292 | - return array_map( |
|
| 293 | - function(DateTimeInterface $dt) use ($hasTime, $isUtc) { |
|
| 294 | - |
|
| 295 | - if ($hasTime) { |
|
| 296 | - return $dt->format('Y-m-d\\TH:i:s') . ($isUtc ? 'Z' : ''); |
|
| 297 | - } else { |
|
| 298 | - return $dt->format('Y-m-d'); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - }, |
|
| 302 | - $dts |
|
| 303 | - ); |
|
| 304 | - |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * Sets the json value, as it would appear in a jCard or jCal object. |
|
| 309 | - * |
|
| 310 | - * The value must always be an array. |
|
| 311 | - * |
|
| 312 | - * @param array $value |
|
| 313 | - * |
|
| 314 | - * @return void |
|
| 315 | - */ |
|
| 316 | - public function setJsonValue(array $value) { |
|
| 317 | - |
|
| 318 | - // dates and times in jCal have one difference to dates and times in |
|
| 319 | - // iCalendar. In jCal date-parts are separated by dashes, and |
|
| 320 | - // time-parts are separated by colons. It makes sense to just remove |
|
| 321 | - // those. |
|
| 322 | - $this->setValue( |
|
| 323 | - array_map( |
|
| 324 | - public function($item) { |
|
| 325 | - |
|
| 326 | - return strtr($item, [':' => '', '-' => '']); |
|
| 327 | - |
|
| 328 | - }, |
|
| 329 | - $value |
|
| 330 | - ) |
|
| 331 | - ); |
|
| 332 | - |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * We need to intercept offsetSet, because it may be used to alter the |
|
| 337 | - * VALUE from DATE-TIME to DATE or vice-versa. |
|
| 338 | - * |
|
| 339 | - * @param string $name |
|
| 340 | - * @param mixed $value |
|
| 341 | - * |
|
| 342 | - * @return void |
|
| 343 | - */ |
|
| 344 | - public function offsetSet($name, $value) { |
|
| 345 | - |
|
| 346 | - parent::offsetSet($name, $value); |
|
| 347 | - if (strtoupper($name) !== 'VALUE') { |
|
| 348 | - return; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - // This will ensure that dates are correctly encoded. |
|
| 352 | - $this->setDateTimes($this->getDateTimes()); |
|
| 353 | - |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * Validates the node for correctness. |
|
| 358 | - * |
|
| 359 | - * The following options are supported: |
|
| 360 | - * Node::REPAIR - May attempt to automatically repair the problem. |
|
| 361 | - * |
|
| 362 | - * This method returns an array with detected problems. |
|
| 363 | - * Every element has the following properties: |
|
| 364 | - * |
|
| 365 | - * * level - problem level. |
|
| 366 | - * * message - A human-readable string describing the issue. |
|
| 367 | - * * node - A reference to the problematic node. |
|
| 368 | - * |
|
| 369 | - * The level means: |
|
| 370 | - * 1 - The issue was repaired (only happens if REPAIR was turned on) |
|
| 371 | - * 2 - An inconsequential issue |
|
| 372 | - * 3 - A severe issue. |
|
| 373 | - * |
|
| 374 | - * @param int $options |
|
| 375 | - * |
|
| 376 | - * @return array |
|
| 377 | - */ |
|
| 378 | - public function validate($options = 0) { |
|
| 379 | - |
|
| 380 | - $messages = parent::validate($options); |
|
| 381 | - $valueType = $this->getValueType(); |
|
| 382 | - $values = $this->getParts(); |
|
| 383 | - try { |
|
| 384 | - foreach ($values as $value) { |
|
| 385 | - switch ($valueType) { |
|
| 386 | - case 'DATE' : |
|
| 387 | - DateTimeParser::parseDate($value); |
|
| 388 | - break; |
|
| 389 | - case 'DATE-TIME' : |
|
| 390 | - DateTimeParser::parseDateTime($value); |
|
| 391 | - break; |
|
| 392 | - } |
|
| 393 | - } |
|
| 394 | - } catch (InvalidDataException $e) { |
|
| 395 | - $messages[] = [ |
|
| 396 | - 'level' => 3, |
|
| 397 | - 'message' => 'The supplied value (' . $value . ') is not a correct ' . $valueType, |
|
| 398 | - 'node' => $this, |
|
| 399 | - ]; |
|
| 400 | - } |
|
| 401 | - return $messages; |
|
| 402 | - |
|
| 403 | - } |
|
| 29 | + /** |
|
| 30 | + * In case this is a multi-value property. This string will be used as a |
|
| 31 | + * delimiter. |
|
| 32 | + * |
|
| 33 | + * @var string|null |
|
| 34 | + */ |
|
| 35 | + public $delimiter = ','; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Sets a multi-valued property. |
|
| 39 | + * |
|
| 40 | + * You may also specify DateTime objects here. |
|
| 41 | + * |
|
| 42 | + * @param array $parts |
|
| 43 | + * |
|
| 44 | + * @return void |
|
| 45 | + */ |
|
| 46 | + public function setParts(array $parts) { |
|
| 47 | + |
|
| 48 | + if (isset($parts[0]) && $parts[0] instanceof DateTimeInterface) { |
|
| 49 | + $this->setDateTimes($parts); |
|
| 50 | + } else { |
|
| 51 | + parent::setParts($parts); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Updates the current value. |
|
| 58 | + * |
|
| 59 | + * This may be either a single, or multiple strings in an array. |
|
| 60 | + * |
|
| 61 | + * Instead of strings, you may also use DateTime here. |
|
| 62 | + * |
|
| 63 | + * @param string|array|DateTimeInterface $value |
|
| 64 | + * |
|
| 65 | + * @return void |
|
| 66 | + */ |
|
| 67 | + public function setValue($value) { |
|
| 68 | + |
|
| 69 | + if (is_array($value) && isset($value[0]) && $value[0] instanceof DateTimeInterface) { |
|
| 70 | + $this->setDateTimes($value); |
|
| 71 | + } elseif ($value instanceof DateTimeInterface) { |
|
| 72 | + $this->setDateTimes([$value]); |
|
| 73 | + } else { |
|
| 74 | + parent::setValue($value); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Sets a raw value coming from a mimedir (iCalendar/vCard) file. |
|
| 81 | + * |
|
| 82 | + * This has been 'unfolded', so only 1 line will be passed. Unescaping is |
|
| 83 | + * not yet done, but parameters are not included. |
|
| 84 | + * |
|
| 85 | + * @param string $val |
|
| 86 | + * |
|
| 87 | + * @return void |
|
| 88 | + */ |
|
| 89 | + public function setRawMimeDirValue($val) { |
|
| 90 | + |
|
| 91 | + $this->setValue(explode($this->delimiter, $val)); |
|
| 92 | + |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Returns a raw mime-dir representation of the value. |
|
| 97 | + * |
|
| 98 | + * @return string |
|
| 99 | + */ |
|
| 100 | + public function getRawMimeDirValue() { |
|
| 101 | + |
|
| 102 | + return implode($this->delimiter, $this->getParts()); |
|
| 103 | + |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Returns true if this is a DATE-TIME value, false if it's a DATE. |
|
| 108 | + * |
|
| 109 | + * @return bool |
|
| 110 | + */ |
|
| 111 | + public function hasTime() { |
|
| 112 | + |
|
| 113 | + return strtoupper((string)$this['VALUE']) !== 'DATE'; |
|
| 114 | + |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Returns true if this is a floating DATE or DATE-TIME. |
|
| 119 | + * |
|
| 120 | + * Note that DATE is always floating. |
|
| 121 | + */ |
|
| 122 | + public function isFloating() { |
|
| 123 | + |
|
| 124 | + return |
|
| 125 | + !$this->hasTime() || |
|
| 126 | + ( |
|
| 127 | + !isset($this['TZID']) && |
|
| 128 | + strpos($this->getValue(), 'Z') === false |
|
| 129 | + ); |
|
| 130 | + |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Returns a date-time value. |
|
| 135 | + * |
|
| 136 | + * Note that if this property contained more than 1 date-time, only the |
|
| 137 | + * first will be returned. To get an array with multiple values, call |
|
| 138 | + * getDateTimes. |
|
| 139 | + * |
|
| 140 | + * If no timezone information is known, because it's either an all-day |
|
| 141 | + * property or floating time, we will use the DateTimeZone argument to |
|
| 142 | + * figure out the exact date. |
|
| 143 | + * |
|
| 144 | + * @param DateTimeZone $timeZone |
|
| 145 | + * |
|
| 146 | + * @return DateTimeImmutable |
|
| 147 | + */ |
|
| 148 | + public function getDateTime(DateTimeZone $timeZone = null) { |
|
| 149 | + |
|
| 150 | + $dt = $this->getDateTimes($timeZone); |
|
| 151 | + if (!$dt) return; |
|
| 152 | + |
|
| 153 | + return $dt[0]; |
|
| 154 | + |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Returns multiple date-time values. |
|
| 159 | + * |
|
| 160 | + * If no timezone information is known, because it's either an all-day |
|
| 161 | + * property or floating time, we will use the DateTimeZone argument to |
|
| 162 | + * figure out the exact date. |
|
| 163 | + * |
|
| 164 | + * @param DateTimeZone $timeZone |
|
| 165 | + * |
|
| 166 | + * @return DateTimeImmutable[] |
|
| 167 | + * @return \DateTime[] |
|
| 168 | + */ |
|
| 169 | + public function getDateTimes(DateTimeZone $timeZone = null) { |
|
| 170 | + |
|
| 171 | + // Does the property have a TZID? |
|
| 172 | + $tzid = $this['TZID']; |
|
| 173 | + |
|
| 174 | + if ($tzid) { |
|
| 175 | + $timeZone = TimeZoneUtil::getTimeZone((string)$tzid, $this->root); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + $dts = []; |
|
| 179 | + foreach ($this->getParts() as $part) { |
|
| 180 | + $dts[] = DateTimeParser::parse($part, $timeZone); |
|
| 181 | + } |
|
| 182 | + return $dts; |
|
| 183 | + |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Sets the property as a DateTime object. |
|
| 188 | + * |
|
| 189 | + * @param DateTimeInterface $dt |
|
| 190 | + * @param bool isFloating If set to true, timezones will be ignored. |
|
| 191 | + * |
|
| 192 | + * @return void |
|
| 193 | + */ |
|
| 194 | + public function setDateTime(DateTimeInterface $dt, $isFloating = false) { |
|
| 195 | + |
|
| 196 | + $this->setDateTimes([$dt], $isFloating); |
|
| 197 | + |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Sets the property as multiple date-time objects. |
|
| 202 | + * |
|
| 203 | + * The first value will be used as a reference for the timezones, and all |
|
| 204 | + * the otehr values will be adjusted for that timezone |
|
| 205 | + * |
|
| 206 | + * @param DateTimeInterface[] $dt |
|
| 207 | + * @param bool isFloating If set to true, timezones will be ignored. |
|
| 208 | + * |
|
| 209 | + * @return void |
|
| 210 | + */ |
|
| 211 | + public function setDateTimes(array $dt, $isFloating = false) { |
|
| 212 | + |
|
| 213 | + $values = []; |
|
| 214 | + |
|
| 215 | + if ($this->hasTime()) { |
|
| 216 | + |
|
| 217 | + $tz = null; |
|
| 218 | + $isUtc = false; |
|
| 219 | + |
|
| 220 | + foreach ($dt as $d) { |
|
| 221 | + |
|
| 222 | + if ($isFloating) { |
|
| 223 | + $values[] = $d->format('Ymd\\THis'); |
|
| 224 | + continue; |
|
| 225 | + } |
|
| 226 | + if (is_null($tz)) { |
|
| 227 | + $tz = $d->getTimeZone(); |
|
| 228 | + $isUtc = in_array($tz->getName(), ['UTC', 'GMT', 'Z', '+00:00']); |
|
| 229 | + if (!$isUtc) { |
|
| 230 | + $this->offsetSet('TZID', $tz->getName()); |
|
| 231 | + } |
|
| 232 | + } else { |
|
| 233 | + $d = $d->setTimeZone($tz); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + if ($isUtc) { |
|
| 237 | + $values[] = $d->format('Ymd\\THis\\Z'); |
|
| 238 | + } else { |
|
| 239 | + $values[] = $d->format('Ymd\\THis'); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + } |
|
| 243 | + if ($isUtc || $isFloating) { |
|
| 244 | + $this->offsetUnset('TZID'); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + } else { |
|
| 248 | + |
|
| 249 | + foreach ($dt as $d) { |
|
| 250 | + |
|
| 251 | + $values[] = $d->format('Ymd'); |
|
| 252 | + |
|
| 253 | + } |
|
| 254 | + $this->offsetUnset('TZID'); |
|
| 255 | + |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + $this->value = $values; |
|
| 259 | + |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Returns the type of value. |
|
| 264 | + * |
|
| 265 | + * This corresponds to the VALUE= parameter. Every property also has a |
|
| 266 | + * 'default' valueType. |
|
| 267 | + * |
|
| 268 | + * @return string |
|
| 269 | + */ |
|
| 270 | + public function getValueType() { |
|
| 271 | + |
|
| 272 | + return $this->hasTime() ? 'DATE-TIME' : 'DATE'; |
|
| 273 | + |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Returns the value, in the format it should be encoded for JSON. |
|
| 278 | + * |
|
| 279 | + * This method must always return an array. |
|
| 280 | + * |
|
| 281 | + * @return array |
|
| 282 | + */ |
|
| 283 | + public function getJsonValue() { |
|
| 284 | + |
|
| 285 | + $dts = $this->getDateTimes(); |
|
| 286 | + $hasTime = $this->hasTime(); |
|
| 287 | + $isFloating = $this->isFloating(); |
|
| 288 | + |
|
| 289 | + $tz = $dts[0]->getTimeZone(); |
|
| 290 | + $isUtc = $isFloating ? false : in_array($tz->getName(), ['UTC', 'GMT', 'Z']); |
|
| 291 | + |
|
| 292 | + return array_map( |
|
| 293 | + function(DateTimeInterface $dt) use ($hasTime, $isUtc) { |
|
| 294 | + |
|
| 295 | + if ($hasTime) { |
|
| 296 | + return $dt->format('Y-m-d\\TH:i:s') . ($isUtc ? 'Z' : ''); |
|
| 297 | + } else { |
|
| 298 | + return $dt->format('Y-m-d'); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + }, |
|
| 302 | + $dts |
|
| 303 | + ); |
|
| 304 | + |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * Sets the json value, as it would appear in a jCard or jCal object. |
|
| 309 | + * |
|
| 310 | + * The value must always be an array. |
|
| 311 | + * |
|
| 312 | + * @param array $value |
|
| 313 | + * |
|
| 314 | + * @return void |
|
| 315 | + */ |
|
| 316 | + public function setJsonValue(array $value) { |
|
| 317 | + |
|
| 318 | + // dates and times in jCal have one difference to dates and times in |
|
| 319 | + // iCalendar. In jCal date-parts are separated by dashes, and |
|
| 320 | + // time-parts are separated by colons. It makes sense to just remove |
|
| 321 | + // those. |
|
| 322 | + $this->setValue( |
|
| 323 | + array_map( |
|
| 324 | + public function($item) { |
|
| 325 | + |
|
| 326 | + return strtr($item, [':' => '', '-' => '']); |
|
| 327 | + |
|
| 328 | + }, |
|
| 329 | + $value |
|
| 330 | + ) |
|
| 331 | + ); |
|
| 332 | + |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * We need to intercept offsetSet, because it may be used to alter the |
|
| 337 | + * VALUE from DATE-TIME to DATE or vice-versa. |
|
| 338 | + * |
|
| 339 | + * @param string $name |
|
| 340 | + * @param mixed $value |
|
| 341 | + * |
|
| 342 | + * @return void |
|
| 343 | + */ |
|
| 344 | + public function offsetSet($name, $value) { |
|
| 345 | + |
|
| 346 | + parent::offsetSet($name, $value); |
|
| 347 | + if (strtoupper($name) !== 'VALUE') { |
|
| 348 | + return; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + // This will ensure that dates are correctly encoded. |
|
| 352 | + $this->setDateTimes($this->getDateTimes()); |
|
| 353 | + |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * Validates the node for correctness. |
|
| 358 | + * |
|
| 359 | + * The following options are supported: |
|
| 360 | + * Node::REPAIR - May attempt to automatically repair the problem. |
|
| 361 | + * |
|
| 362 | + * This method returns an array with detected problems. |
|
| 363 | + * Every element has the following properties: |
|
| 364 | + * |
|
| 365 | + * * level - problem level. |
|
| 366 | + * * message - A human-readable string describing the issue. |
|
| 367 | + * * node - A reference to the problematic node. |
|
| 368 | + * |
|
| 369 | + * The level means: |
|
| 370 | + * 1 - The issue was repaired (only happens if REPAIR was turned on) |
|
| 371 | + * 2 - An inconsequential issue |
|
| 372 | + * 3 - A severe issue. |
|
| 373 | + * |
|
| 374 | + * @param int $options |
|
| 375 | + * |
|
| 376 | + * @return array |
|
| 377 | + */ |
|
| 378 | + public function validate($options = 0) { |
|
| 379 | + |
|
| 380 | + $messages = parent::validate($options); |
|
| 381 | + $valueType = $this->getValueType(); |
|
| 382 | + $values = $this->getParts(); |
|
| 383 | + try { |
|
| 384 | + foreach ($values as $value) { |
|
| 385 | + switch ($valueType) { |
|
| 386 | + case 'DATE' : |
|
| 387 | + DateTimeParser::parseDate($value); |
|
| 388 | + break; |
|
| 389 | + case 'DATE-TIME' : |
|
| 390 | + DateTimeParser::parseDateTime($value); |
|
| 391 | + break; |
|
| 392 | + } |
|
| 393 | + } |
|
| 394 | + } catch (InvalidDataException $e) { |
|
| 395 | + $messages[] = [ |
|
| 396 | + 'level' => 3, |
|
| 397 | + 'message' => 'The supplied value (' . $value . ') is not a correct ' . $valueType, |
|
| 398 | + 'node' => $this, |
|
| 399 | + ]; |
|
| 400 | + } |
|
| 401 | + return $messages; |
|
| 402 | + |
|
| 403 | + } |
|
| 404 | 404 | } |
@@ -19,137 +19,137 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Period extends Property { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * In case this is a multi-value property. This string will be used as a |
|
| 24 | - * delimiter. |
|
| 25 | - * |
|
| 26 | - * @var string|null |
|
| 27 | - */ |
|
| 28 | - public $delimiter = ','; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Sets a raw value coming from a mimedir (iCalendar/vCard) file. |
|
| 32 | - * |
|
| 33 | - * This has been 'unfolded', so only 1 line will be passed. Unescaping is |
|
| 34 | - * not yet done, but parameters are not included. |
|
| 35 | - * |
|
| 36 | - * @param string $val |
|
| 37 | - * |
|
| 38 | - * @return void |
|
| 39 | - */ |
|
| 40 | - public function setRawMimeDirValue($val) { |
|
| 41 | - |
|
| 42 | - $this->setValue(explode($this->delimiter, $val)); |
|
| 43 | - |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Returns a raw mime-dir representation of the value. |
|
| 48 | - * |
|
| 49 | - * @return string |
|
| 50 | - */ |
|
| 51 | - public function getRawMimeDirValue() { |
|
| 52 | - |
|
| 53 | - return implode($this->delimiter, $this->getParts()); |
|
| 54 | - |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Returns the type of value. |
|
| 59 | - * |
|
| 60 | - * This corresponds to the VALUE= parameter. Every property also has a |
|
| 61 | - * 'default' valueType. |
|
| 62 | - * |
|
| 63 | - * @return string |
|
| 64 | - */ |
|
| 65 | - public function getValueType() { |
|
| 66 | - |
|
| 67 | - return 'PERIOD'; |
|
| 68 | - |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Sets the json value, as it would appear in a jCard or jCal object. |
|
| 73 | - * |
|
| 74 | - * The value must always be an array. |
|
| 75 | - * |
|
| 76 | - * @param array $value |
|
| 77 | - * |
|
| 78 | - * @return void |
|
| 79 | - */ |
|
| 80 | - public function setJsonValue(array $value) { |
|
| 81 | - |
|
| 82 | - $value = array_map( |
|
| 83 | - function($item) { |
|
| 84 | - |
|
| 85 | - return strtr(implode('/', $item), [':' => '', '-' => '']); |
|
| 86 | - |
|
| 87 | - }, |
|
| 88 | - $value |
|
| 89 | - ); |
|
| 90 | - parent::setJsonValue($value); |
|
| 91 | - |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Returns the value, in the format it should be encoded for json. |
|
| 96 | - * |
|
| 97 | - * This method must always return an array. |
|
| 98 | - * |
|
| 99 | - * @return array |
|
| 100 | - */ |
|
| 101 | - public function getJsonValue() { |
|
| 102 | - |
|
| 103 | - $return = []; |
|
| 104 | - foreach ($this->getParts() as $item) { |
|
| 105 | - |
|
| 106 | - list($start, $end) = explode('/', $item, 2); |
|
| 107 | - |
|
| 108 | - $start = DateTimeParser::parseDateTime($start); |
|
| 109 | - |
|
| 110 | - // This is a duration value. |
|
| 111 | - if ($end[0] === 'P') { |
|
| 112 | - $return[] = [ |
|
| 113 | - $start->format('Y-m-d\\TH:i:s'), |
|
| 114 | - $end |
|
| 115 | - ]; |
|
| 116 | - } else { |
|
| 117 | - $end = DateTimeParser::parseDateTime($end); |
|
| 118 | - $return[] = [ |
|
| 119 | - $start->format('Y-m-d\\TH:i:s'), |
|
| 120 | - $end->format('Y-m-d\\TH:i:s'), |
|
| 121 | - ]; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return $return; |
|
| 127 | - |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * This method serializes only the value of a property. This is used to |
|
| 132 | - * create xCard or xCal documents. |
|
| 133 | - * |
|
| 134 | - * @param Xml\Writer $writer XML writer. |
|
| 135 | - * |
|
| 136 | - * @return void |
|
| 137 | - */ |
|
| 138 | - protected function xmlSerializeValue(Xml\Writer $writer) { |
|
| 139 | - |
|
| 140 | - $writer->startElement(strtolower($this->getValueType())); |
|
| 141 | - $value = $this->getJsonValue(); |
|
| 142 | - $writer->writeElement('start', $value[0][0]); |
|
| 143 | - |
|
| 144 | - if ($value[0][1][0] === 'P') { |
|
| 145 | - $writer->writeElement('duration', $value[0][1]); |
|
| 146 | - } |
|
| 147 | - else { |
|
| 148 | - $writer->writeElement('end', $value[0][1]); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - $writer->endElement(); |
|
| 152 | - |
|
| 153 | - } |
|
| 22 | + /** |
|
| 23 | + * In case this is a multi-value property. This string will be used as a |
|
| 24 | + * delimiter. |
|
| 25 | + * |
|
| 26 | + * @var string|null |
|
| 27 | + */ |
|
| 28 | + public $delimiter = ','; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Sets a raw value coming from a mimedir (iCalendar/vCard) file. |
|
| 32 | + * |
|
| 33 | + * This has been 'unfolded', so only 1 line will be passed. Unescaping is |
|
| 34 | + * not yet done, but parameters are not included. |
|
| 35 | + * |
|
| 36 | + * @param string $val |
|
| 37 | + * |
|
| 38 | + * @return void |
|
| 39 | + */ |
|
| 40 | + public function setRawMimeDirValue($val) { |
|
| 41 | + |
|
| 42 | + $this->setValue(explode($this->delimiter, $val)); |
|
| 43 | + |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Returns a raw mime-dir representation of the value. |
|
| 48 | + * |
|
| 49 | + * @return string |
|
| 50 | + */ |
|
| 51 | + public function getRawMimeDirValue() { |
|
| 52 | + |
|
| 53 | + return implode($this->delimiter, $this->getParts()); |
|
| 54 | + |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Returns the type of value. |
|
| 59 | + * |
|
| 60 | + * This corresponds to the VALUE= parameter. Every property also has a |
|
| 61 | + * 'default' valueType. |
|
| 62 | + * |
|
| 63 | + * @return string |
|
| 64 | + */ |
|
| 65 | + public function getValueType() { |
|
| 66 | + |
|
| 67 | + return 'PERIOD'; |
|
| 68 | + |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Sets the json value, as it would appear in a jCard or jCal object. |
|
| 73 | + * |
|
| 74 | + * The value must always be an array. |
|
| 75 | + * |
|
| 76 | + * @param array $value |
|
| 77 | + * |
|
| 78 | + * @return void |
|
| 79 | + */ |
|
| 80 | + public function setJsonValue(array $value) { |
|
| 81 | + |
|
| 82 | + $value = array_map( |
|
| 83 | + function($item) { |
|
| 84 | + |
|
| 85 | + return strtr(implode('/', $item), [':' => '', '-' => '']); |
|
| 86 | + |
|
| 87 | + }, |
|
| 88 | + $value |
|
| 89 | + ); |
|
| 90 | + parent::setJsonValue($value); |
|
| 91 | + |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Returns the value, in the format it should be encoded for json. |
|
| 96 | + * |
|
| 97 | + * This method must always return an array. |
|
| 98 | + * |
|
| 99 | + * @return array |
|
| 100 | + */ |
|
| 101 | + public function getJsonValue() { |
|
| 102 | + |
|
| 103 | + $return = []; |
|
| 104 | + foreach ($this->getParts() as $item) { |
|
| 105 | + |
|
| 106 | + list($start, $end) = explode('/', $item, 2); |
|
| 107 | + |
|
| 108 | + $start = DateTimeParser::parseDateTime($start); |
|
| 109 | + |
|
| 110 | + // This is a duration value. |
|
| 111 | + if ($end[0] === 'P') { |
|
| 112 | + $return[] = [ |
|
| 113 | + $start->format('Y-m-d\\TH:i:s'), |
|
| 114 | + $end |
|
| 115 | + ]; |
|
| 116 | + } else { |
|
| 117 | + $end = DateTimeParser::parseDateTime($end); |
|
| 118 | + $return[] = [ |
|
| 119 | + $start->format('Y-m-d\\TH:i:s'), |
|
| 120 | + $end->format('Y-m-d\\TH:i:s'), |
|
| 121 | + ]; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return $return; |
|
| 127 | + |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * This method serializes only the value of a property. This is used to |
|
| 132 | + * create xCard or xCal documents. |
|
| 133 | + * |
|
| 134 | + * @param Xml\Writer $writer XML writer. |
|
| 135 | + * |
|
| 136 | + * @return void |
|
| 137 | + */ |
|
| 138 | + protected function xmlSerializeValue(Xml\Writer $writer) { |
|
| 139 | + |
|
| 140 | + $writer->startElement(strtolower($this->getValueType())); |
|
| 141 | + $value = $this->getJsonValue(); |
|
| 142 | + $writer->writeElement('start', $value[0][0]); |
|
| 143 | + |
|
| 144 | + if ($value[0][1][0] === 'P') { |
|
| 145 | + $writer->writeElement('duration', $value[0][1]); |
|
| 146 | + } |
|
| 147 | + else { |
|
| 148 | + $writer->writeElement('end', $value[0][1]); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + $writer->endElement(); |
|
| 152 | + |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | 155 | } |
@@ -15,130 +15,130 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Time extends Text { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * In case this is a multi-value property. This string will be used as a |
|
| 20 | - * delimiter. |
|
| 21 | - * |
|
| 22 | - * @var string|null |
|
| 23 | - */ |
|
| 24 | - public $delimiter = null; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Returns the type of value. |
|
| 28 | - * |
|
| 29 | - * This corresponds to the VALUE= parameter. Every property also has a |
|
| 30 | - * 'default' valueType. |
|
| 31 | - * |
|
| 32 | - * @return string |
|
| 33 | - */ |
|
| 34 | - public function getValueType() { |
|
| 35 | - |
|
| 36 | - return 'TIME'; |
|
| 37 | - |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Sets the JSON value, as it would appear in a jCard or jCal object. |
|
| 42 | - * |
|
| 43 | - * The value must always be an array. |
|
| 44 | - * |
|
| 45 | - * @param array $value |
|
| 46 | - * |
|
| 47 | - * @return void |
|
| 48 | - */ |
|
| 49 | - public function setJsonValue(array $value) { |
|
| 50 | - |
|
| 51 | - // Removing colons from value. |
|
| 52 | - $value = str_replace( |
|
| 53 | - ':', |
|
| 54 | - '', |
|
| 55 | - $value |
|
| 56 | - ); |
|
| 57 | - |
|
| 58 | - if (count($value) === 1) { |
|
| 59 | - $this->setValue(reset($value)); |
|
| 60 | - } else { |
|
| 61 | - $this->setValue($value); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Returns the value, in the format it should be encoded for json. |
|
| 68 | - * |
|
| 69 | - * This method must always return an array. |
|
| 70 | - * |
|
| 71 | - * @return array |
|
| 72 | - */ |
|
| 73 | - public function getJsonValue() { |
|
| 74 | - |
|
| 75 | - $parts = DateTimeParser::parseVCardTime($this->getValue()); |
|
| 76 | - $timeStr = ''; |
|
| 77 | - |
|
| 78 | - // Hour |
|
| 79 | - if (!is_null($parts['hour'])) { |
|
| 80 | - $timeStr .= $parts['hour']; |
|
| 81 | - |
|
| 82 | - if (!is_null($parts['minute'])) { |
|
| 83 | - $timeStr .= ':'; |
|
| 84 | - } |
|
| 85 | - } else { |
|
| 86 | - // We know either minute or second _must_ be set, so we insert a |
|
| 87 | - // dash for an empty value. |
|
| 88 | - $timeStr .= '-'; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - // Minute |
|
| 92 | - if (!is_null($parts['minute'])) { |
|
| 93 | - $timeStr .= $parts['minute']; |
|
| 94 | - |
|
| 95 | - if (!is_null($parts['second'])) { |
|
| 96 | - $timeStr .= ':'; |
|
| 97 | - } |
|
| 98 | - } else { |
|
| 99 | - if (isset($parts['second'])) { |
|
| 100 | - // Dash for empty minute |
|
| 101 | - $timeStr .= '-'; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - // Second |
|
| 106 | - if (!is_null($parts['second'])) { |
|
| 107 | - $timeStr .= $parts['second']; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - // Timezone |
|
| 111 | - if (!is_null($parts['timezone'])) { |
|
| 112 | - if ($parts['timezone'] === 'Z') { |
|
| 113 | - $timeStr .= 'Z'; |
|
| 114 | - } else { |
|
| 115 | - $timeStr .= |
|
| 116 | - preg_replace('/([0-9]{2})([0-9]{2})$/', '$1:$2', $parts['timezone']); |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - return [$timeStr]; |
|
| 121 | - |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Hydrate data from a XML subtree, as it would appear in a xCard or xCal |
|
| 126 | - * object. |
|
| 127 | - * |
|
| 128 | - * @param array $value |
|
| 129 | - * |
|
| 130 | - * @return void |
|
| 131 | - */ |
|
| 132 | - public function setXmlValue(array $value) { |
|
| 133 | - |
|
| 134 | - $value = array_map( |
|
| 135 | - function($value) { |
|
| 136 | - return str_replace(':', '', $value); |
|
| 137 | - }, |
|
| 138 | - $value |
|
| 139 | - ); |
|
| 140 | - parent::setXmlValue($value); |
|
| 141 | - |
|
| 142 | - } |
|
| 18 | + /** |
|
| 19 | + * In case this is a multi-value property. This string will be used as a |
|
| 20 | + * delimiter. |
|
| 21 | + * |
|
| 22 | + * @var string|null |
|
| 23 | + */ |
|
| 24 | + public $delimiter = null; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Returns the type of value. |
|
| 28 | + * |
|
| 29 | + * This corresponds to the VALUE= parameter. Every property also has a |
|
| 30 | + * 'default' valueType. |
|
| 31 | + * |
|
| 32 | + * @return string |
|
| 33 | + */ |
|
| 34 | + public function getValueType() { |
|
| 35 | + |
|
| 36 | + return 'TIME'; |
|
| 37 | + |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Sets the JSON value, as it would appear in a jCard or jCal object. |
|
| 42 | + * |
|
| 43 | + * The value must always be an array. |
|
| 44 | + * |
|
| 45 | + * @param array $value |
|
| 46 | + * |
|
| 47 | + * @return void |
|
| 48 | + */ |
|
| 49 | + public function setJsonValue(array $value) { |
|
| 50 | + |
|
| 51 | + // Removing colons from value. |
|
| 52 | + $value = str_replace( |
|
| 53 | + ':', |
|
| 54 | + '', |
|
| 55 | + $value |
|
| 56 | + ); |
|
| 57 | + |
|
| 58 | + if (count($value) === 1) { |
|
| 59 | + $this->setValue(reset($value)); |
|
| 60 | + } else { |
|
| 61 | + $this->setValue($value); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Returns the value, in the format it should be encoded for json. |
|
| 68 | + * |
|
| 69 | + * This method must always return an array. |
|
| 70 | + * |
|
| 71 | + * @return array |
|
| 72 | + */ |
|
| 73 | + public function getJsonValue() { |
|
| 74 | + |
|
| 75 | + $parts = DateTimeParser::parseVCardTime($this->getValue()); |
|
| 76 | + $timeStr = ''; |
|
| 77 | + |
|
| 78 | + // Hour |
|
| 79 | + if (!is_null($parts['hour'])) { |
|
| 80 | + $timeStr .= $parts['hour']; |
|
| 81 | + |
|
| 82 | + if (!is_null($parts['minute'])) { |
|
| 83 | + $timeStr .= ':'; |
|
| 84 | + } |
|
| 85 | + } else { |
|
| 86 | + // We know either minute or second _must_ be set, so we insert a |
|
| 87 | + // dash for an empty value. |
|
| 88 | + $timeStr .= '-'; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + // Minute |
|
| 92 | + if (!is_null($parts['minute'])) { |
|
| 93 | + $timeStr .= $parts['minute']; |
|
| 94 | + |
|
| 95 | + if (!is_null($parts['second'])) { |
|
| 96 | + $timeStr .= ':'; |
|
| 97 | + } |
|
| 98 | + } else { |
|
| 99 | + if (isset($parts['second'])) { |
|
| 100 | + // Dash for empty minute |
|
| 101 | + $timeStr .= '-'; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + // Second |
|
| 106 | + if (!is_null($parts['second'])) { |
|
| 107 | + $timeStr .= $parts['second']; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + // Timezone |
|
| 111 | + if (!is_null($parts['timezone'])) { |
|
| 112 | + if ($parts['timezone'] === 'Z') { |
|
| 113 | + $timeStr .= 'Z'; |
|
| 114 | + } else { |
|
| 115 | + $timeStr .= |
|
| 116 | + preg_replace('/([0-9]{2})([0-9]{2})$/', '$1:$2', $parts['timezone']); |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + return [$timeStr]; |
|
| 121 | + |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Hydrate data from a XML subtree, as it would appear in a xCard or xCal |
|
| 126 | + * object. |
|
| 127 | + * |
|
| 128 | + * @param array $value |
|
| 129 | + * |
|
| 130 | + * @return void |
|
| 131 | + */ |
|
| 132 | + public function setXmlValue(array $value) { |
|
| 133 | + |
|
| 134 | + $value = array_map( |
|
| 135 | + function($value) { |
|
| 136 | + return str_replace(':', '', $value); |
|
| 137 | + }, |
|
| 138 | + $value |
|
| 139 | + ); |
|
| 140 | + parent::setXmlValue($value); |
|
| 141 | + |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | 144 | } |
@@ -13,65 +13,65 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class UtcOffset extends Text { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * In case this is a multi-value property. This string will be used as a |
|
| 18 | - * delimiter. |
|
| 19 | - * |
|
| 20 | - * @var string|null |
|
| 21 | - */ |
|
| 22 | - public $delimiter = null; |
|
| 16 | + /** |
|
| 17 | + * In case this is a multi-value property. This string will be used as a |
|
| 18 | + * delimiter. |
|
| 19 | + * |
|
| 20 | + * @var string|null |
|
| 21 | + */ |
|
| 22 | + public $delimiter = null; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Returns the type of value. |
|
| 26 | - * |
|
| 27 | - * This corresponds to the VALUE= parameter. Every property also has a |
|
| 28 | - * 'default' valueType. |
|
| 29 | - * |
|
| 30 | - * @return string |
|
| 31 | - */ |
|
| 32 | - public function getValueType() { |
|
| 24 | + /** |
|
| 25 | + * Returns the type of value. |
|
| 26 | + * |
|
| 27 | + * This corresponds to the VALUE= parameter. Every property also has a |
|
| 28 | + * 'default' valueType. |
|
| 29 | + * |
|
| 30 | + * @return string |
|
| 31 | + */ |
|
| 32 | + public function getValueType() { |
|
| 33 | 33 | |
| 34 | - return 'UTC-OFFSET'; |
|
| 34 | + return 'UTC-OFFSET'; |
|
| 35 | 35 | |
| 36 | - } |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Sets the JSON value, as it would appear in a jCard or jCal object. |
|
| 40 | - * |
|
| 41 | - * The value must always be an array. |
|
| 42 | - * |
|
| 43 | - * @param array $value |
|
| 44 | - * |
|
| 45 | - * @return void |
|
| 46 | - */ |
|
| 47 | - public function setJsonValue(array $value) { |
|
| 38 | + /** |
|
| 39 | + * Sets the JSON value, as it would appear in a jCard or jCal object. |
|
| 40 | + * |
|
| 41 | + * The value must always be an array. |
|
| 42 | + * |
|
| 43 | + * @param array $value |
|
| 44 | + * |
|
| 45 | + * @return void |
|
| 46 | + */ |
|
| 47 | + public function setJsonValue(array $value) { |
|
| 48 | 48 | |
| 49 | - $value = array_map( |
|
| 50 | - function($value) { |
|
| 51 | - return str_replace(':', '', $value); |
|
| 52 | - }, |
|
| 53 | - $value |
|
| 54 | - ); |
|
| 55 | - parent::setJsonValue($value); |
|
| 49 | + $value = array_map( |
|
| 50 | + function($value) { |
|
| 51 | + return str_replace(':', '', $value); |
|
| 52 | + }, |
|
| 53 | + $value |
|
| 54 | + ); |
|
| 55 | + parent::setJsonValue($value); |
|
| 56 | 56 | |
| 57 | - } |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Returns the value, in the format it should be encoded for JSON. |
|
| 61 | - * |
|
| 62 | - * This method must always return an array. |
|
| 63 | - * |
|
| 64 | - * @return array |
|
| 65 | - */ |
|
| 66 | - public function getJsonValue() { |
|
| 59 | + /** |
|
| 60 | + * Returns the value, in the format it should be encoded for JSON. |
|
| 61 | + * |
|
| 62 | + * This method must always return an array. |
|
| 63 | + * |
|
| 64 | + * @return array |
|
| 65 | + */ |
|
| 66 | + public function getJsonValue() { |
|
| 67 | 67 | |
| 68 | - return array_map( |
|
| 69 | - public function($value) { |
|
| 70 | - return substr($value, 0, -2) . ':' . |
|
| 71 | - substr($value, -2); |
|
| 72 | - }, |
|
| 73 | - parent::getJsonValue() |
|
| 74 | - ); |
|
| 68 | + return array_map( |
|
| 69 | + public function($value) { |
|
| 70 | + return substr($value, 0, -2) . ':' . |
|
| 71 | + substr($value, -2); |
|
| 72 | + }, |
|
| 73 | + parent::getJsonValue() |
|
| 74 | + ); |
|
| 75 | 75 | |
| 76 | - } |
|
| 76 | + } |
|
| 77 | 77 | } |
@@ -3,7 +3,7 @@ discard block |
||
| 3 | 3 | namespace Sabre\VObject\Property; |
| 4 | 4 | |
| 5 | 5 | use |
| 6 | - Sabre\VObject\Property; |
|
| 6 | + Sabre\VObject\Property; |
|
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | 9 | * Boolean property. |
@@ -19,66 +19,66 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | class Boolean extends Property { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Sets a raw value coming from a mimedir (iCalendar/vCard) file. |
|
| 24 | - * |
|
| 25 | - * This has been 'unfolded', so only 1 line will be passed. Unescaping is |
|
| 26 | - * not yet done, but parameters are not included. |
|
| 27 | - * |
|
| 28 | - * @param string $val |
|
| 29 | - * |
|
| 30 | - * @return void |
|
| 31 | - */ |
|
| 32 | - public function setRawMimeDirValue($val) { |
|
| 33 | - |
|
| 34 | - $val = strtoupper($val) === 'TRUE' ? true : false; |
|
| 35 | - $this->setValue($val); |
|
| 36 | - |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Returns a raw mime-dir representation of the value. |
|
| 41 | - * |
|
| 42 | - * @return string |
|
| 43 | - */ |
|
| 44 | - public function getRawMimeDirValue() { |
|
| 45 | - |
|
| 46 | - return $this->value ? 'TRUE' : 'false'; |
|
| 47 | - |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Returns the type of value. |
|
| 52 | - * |
|
| 53 | - * This corresponds to the VALUE= parameter. Every property also has a |
|
| 54 | - * 'default' valueType. |
|
| 55 | - * |
|
| 56 | - * @return string |
|
| 57 | - */ |
|
| 58 | - public function getValueType() { |
|
| 59 | - |
|
| 60 | - return 'BOOLEAN'; |
|
| 61 | - |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Hydrate data from a XML subtree, as it would appear in a xCard or xCal |
|
| 66 | - * object. |
|
| 67 | - * |
|
| 68 | - * @param array $value |
|
| 69 | - * |
|
| 70 | - * @return void |
|
| 71 | - */ |
|
| 72 | - public function setXmlValue(array $value) { |
|
| 73 | - |
|
| 74 | - $value = array_map( |
|
| 75 | - function($value) { |
|
| 76 | - return 'true' === $value; |
|
| 77 | - }, |
|
| 78 | - $value |
|
| 79 | - ); |
|
| 80 | - parent::setXmlValue($value); |
|
| 81 | - |
|
| 82 | - } |
|
| 22 | + /** |
|
| 23 | + * Sets a raw value coming from a mimedir (iCalendar/vCard) file. |
|
| 24 | + * |
|
| 25 | + * This has been 'unfolded', so only 1 line will be passed. Unescaping is |
|
| 26 | + * not yet done, but parameters are not included. |
|
| 27 | + * |
|
| 28 | + * @param string $val |
|
| 29 | + * |
|
| 30 | + * @return void |
|
| 31 | + */ |
|
| 32 | + public function setRawMimeDirValue($val) { |
|
| 33 | + |
|
| 34 | + $val = strtoupper($val) === 'TRUE' ? true : false; |
|
| 35 | + $this->setValue($val); |
|
| 36 | + |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Returns a raw mime-dir representation of the value. |
|
| 41 | + * |
|
| 42 | + * @return string |
|
| 43 | + */ |
|
| 44 | + public function getRawMimeDirValue() { |
|
| 45 | + |
|
| 46 | + return $this->value ? 'TRUE' : 'false'; |
|
| 47 | + |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Returns the type of value. |
|
| 52 | + * |
|
| 53 | + * This corresponds to the VALUE= parameter. Every property also has a |
|
| 54 | + * 'default' valueType. |
|
| 55 | + * |
|
| 56 | + * @return string |
|
| 57 | + */ |
|
| 58 | + public function getValueType() { |
|
| 59 | + |
|
| 60 | + return 'BOOLEAN'; |
|
| 61 | + |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Hydrate data from a XML subtree, as it would appear in a xCard or xCal |
|
| 66 | + * object. |
|
| 67 | + * |
|
| 68 | + * @param array $value |
|
| 69 | + * |
|
| 70 | + * @return void |
|
| 71 | + */ |
|
| 72 | + public function setXmlValue(array $value) { |
|
| 73 | + |
|
| 74 | + $value = array_map( |
|
| 75 | + function($value) { |
|
| 76 | + return 'true' === $value; |
|
| 77 | + }, |
|
| 78 | + $value |
|
| 79 | + ); |
|
| 80 | + parent::setXmlValue($value); |
|
| 81 | + |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | 84 | } |
@@ -18,180 +18,180 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Json extends Parser { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The input data. |
|
| 23 | - * |
|
| 24 | - * @var array |
|
| 25 | - */ |
|
| 26 | - protected $input; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Root component. |
|
| 30 | - * |
|
| 31 | - * @var Document |
|
| 32 | - */ |
|
| 33 | - protected $root; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * This method starts the parsing process. |
|
| 37 | - * |
|
| 38 | - * If the input was not supplied during construction, it's possible to pass |
|
| 39 | - * it here instead. |
|
| 40 | - * |
|
| 41 | - * If either input or options are not supplied, the defaults will be used. |
|
| 42 | - * |
|
| 43 | - * @param resource|string|array|null $input |
|
| 44 | - * @param int $options |
|
| 45 | - * |
|
| 46 | - * @return Sabre\VObject\Document |
|
| 47 | - */ |
|
| 48 | - public function parse($input = null, $options = 0) { |
|
| 49 | - |
|
| 50 | - if (!is_null($input)) { |
|
| 51 | - $this->setInput($input); |
|
| 52 | - } |
|
| 53 | - if (is_null($this->input)) { |
|
| 54 | - throw new EofException('End of input stream, or no input supplied'); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - if (0 !== $options) { |
|
| 58 | - $this->options = $options; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - switch ($this->input[0]) { |
|
| 62 | - case 'vcalendar' : |
|
| 63 | - $this->root = new VCalendar([], false); |
|
| 64 | - break; |
|
| 65 | - case 'vcard' : |
|
| 66 | - $this->root = new VCard([], false); |
|
| 67 | - break; |
|
| 68 | - default : |
|
| 69 | - throw new ParseException('The root component must either be a vcalendar, or a vcard'); |
|
| 70 | - |
|
| 71 | - } |
|
| 72 | - foreach ($this->input[1] as $prop) { |
|
| 73 | - $this->root->add($this->parseProperty($prop)); |
|
| 74 | - } |
|
| 75 | - if (isset($this->input[2])) foreach ($this->input[2] as $comp) { |
|
| 76 | - $this->root->add($this->parseComponent($comp)); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - // Resetting the input so we can throw an feof exception the next time. |
|
| 80 | - $this->input = null; |
|
| 81 | - |
|
| 82 | - return $this->root; |
|
| 83 | - |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Parses a component. |
|
| 88 | - * |
|
| 89 | - * @param array $jComp |
|
| 90 | - * |
|
| 91 | - * @return \Sabre\VObject\Component |
|
| 92 | - */ |
|
| 93 | - public function parseComponent(array $jComp) { |
|
| 94 | - |
|
| 95 | - // We can remove $self from PHP 5.4 onward. |
|
| 96 | - $self = $this; |
|
| 97 | - |
|
| 98 | - $properties = array_map( |
|
| 99 | - function($jProp) use ($self) { |
|
| 100 | - return $self->parseProperty($jProp); |
|
| 101 | - }, |
|
| 102 | - $jComp[1] |
|
| 103 | - ); |
|
| 104 | - |
|
| 105 | - if (isset($jComp[2])) { |
|
| 106 | - |
|
| 107 | - $components = array_map( |
|
| 108 | - public function($jComp) use ($self) { |
|
| 109 | - return $self->parseComponent($jComp); |
|
| 110 | - }, |
|
| 111 | - $jComp[2] |
|
| 112 | - ); |
|
| 113 | - |
|
| 114 | - } else $components = []; |
|
| 115 | - |
|
| 116 | - return $this->root->createComponent( |
|
| 117 | - $jComp[0], |
|
| 118 | - array_merge($properties, $components), |
|
| 119 | - $defaults = false |
|
| 120 | - ); |
|
| 121 | - |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Parses properties. |
|
| 126 | - * |
|
| 127 | - * @param array $jProp |
|
| 128 | - * |
|
| 129 | - * @return \Sabre\VObject\Property |
|
| 130 | - */ |
|
| 131 | - public function parseProperty(array $jProp) { |
|
| 132 | - |
|
| 133 | - list( |
|
| 134 | - $propertyName, |
|
| 135 | - $parameters, |
|
| 136 | - $valueType |
|
| 137 | - ) = $jProp; |
|
| 138 | - |
|
| 139 | - $propertyName = strtoupper($propertyName); |
|
| 140 | - |
|
| 141 | - // This is the default class we would be using if we didn't know the |
|
| 142 | - // value type. We're using this value later in this function. |
|
| 143 | - $defaultPropertyClass = $this->root->getClassNameForPropertyName($propertyName); |
|
| 144 | - |
|
| 145 | - $parameters = (array)$parameters; |
|
| 146 | - |
|
| 147 | - $value = array_slice($jProp, 3); |
|
| 148 | - |
|
| 149 | - $valueType = strtoupper($valueType); |
|
| 150 | - |
|
| 151 | - if (isset($parameters['group'])) { |
|
| 152 | - $propertyName = $parameters['group'] . '.' . $propertyName; |
|
| 153 | - unset($parameters['group']); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - $prop = $this->root->createProperty($propertyName, null, $parameters, $valueType); |
|
| 157 | - $prop->setJsonValue($value); |
|
| 158 | - |
|
| 159 | - // We have to do something awkward here. FlatText as well as Text |
|
| 160 | - // represents TEXT values. We have to normalize these here. In the |
|
| 161 | - // future we can get rid of FlatText once we're allowed to break BC |
|
| 162 | - // again. |
|
| 163 | - if ($defaultPropertyClass === 'Sabre\VObject\Property\FlatText') { |
|
| 164 | - $defaultPropertyClass = 'Sabre\VObject\Property\Text'; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - // If the value type we received (e.g.: TEXT) was not the default value |
|
| 168 | - // type for the given property (e.g.: BDAY), we need to add a VALUE= |
|
| 169 | - // parameter. |
|
| 170 | - if ($defaultPropertyClass !== get_class($prop)) { |
|
| 171 | - $prop["VALUE"] = $valueType; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - return $prop; |
|
| 175 | - |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Sets the input data. |
|
| 180 | - * |
|
| 181 | - * @param resource|string|array $input |
|
| 182 | - * |
|
| 183 | - * @return void |
|
| 184 | - */ |
|
| 185 | - public function setInput($input) { |
|
| 186 | - |
|
| 187 | - if (is_resource($input)) { |
|
| 188 | - $input = stream_get_contents($input); |
|
| 189 | - } |
|
| 190 | - if (is_string($input)) { |
|
| 191 | - $input = json_decode($input); |
|
| 192 | - } |
|
| 193 | - $this->input = $input; |
|
| 194 | - |
|
| 195 | - } |
|
| 21 | + /** |
|
| 22 | + * The input data. |
|
| 23 | + * |
|
| 24 | + * @var array |
|
| 25 | + */ |
|
| 26 | + protected $input; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Root component. |
|
| 30 | + * |
|
| 31 | + * @var Document |
|
| 32 | + */ |
|
| 33 | + protected $root; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * This method starts the parsing process. |
|
| 37 | + * |
|
| 38 | + * If the input was not supplied during construction, it's possible to pass |
|
| 39 | + * it here instead. |
|
| 40 | + * |
|
| 41 | + * If either input or options are not supplied, the defaults will be used. |
|
| 42 | + * |
|
| 43 | + * @param resource|string|array|null $input |
|
| 44 | + * @param int $options |
|
| 45 | + * |
|
| 46 | + * @return Sabre\VObject\Document |
|
| 47 | + */ |
|
| 48 | + public function parse($input = null, $options = 0) { |
|
| 49 | + |
|
| 50 | + if (!is_null($input)) { |
|
| 51 | + $this->setInput($input); |
|
| 52 | + } |
|
| 53 | + if (is_null($this->input)) { |
|
| 54 | + throw new EofException('End of input stream, or no input supplied'); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + if (0 !== $options) { |
|
| 58 | + $this->options = $options; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + switch ($this->input[0]) { |
|
| 62 | + case 'vcalendar' : |
|
| 63 | + $this->root = new VCalendar([], false); |
|
| 64 | + break; |
|
| 65 | + case 'vcard' : |
|
| 66 | + $this->root = new VCard([], false); |
|
| 67 | + break; |
|
| 68 | + default : |
|
| 69 | + throw new ParseException('The root component must either be a vcalendar, or a vcard'); |
|
| 70 | + |
|
| 71 | + } |
|
| 72 | + foreach ($this->input[1] as $prop) { |
|
| 73 | + $this->root->add($this->parseProperty($prop)); |
|
| 74 | + } |
|
| 75 | + if (isset($this->input[2])) foreach ($this->input[2] as $comp) { |
|
| 76 | + $this->root->add($this->parseComponent($comp)); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + // Resetting the input so we can throw an feof exception the next time. |
|
| 80 | + $this->input = null; |
|
| 81 | + |
|
| 82 | + return $this->root; |
|
| 83 | + |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Parses a component. |
|
| 88 | + * |
|
| 89 | + * @param array $jComp |
|
| 90 | + * |
|
| 91 | + * @return \Sabre\VObject\Component |
|
| 92 | + */ |
|
| 93 | + public function parseComponent(array $jComp) { |
|
| 94 | + |
|
| 95 | + // We can remove $self from PHP 5.4 onward. |
|
| 96 | + $self = $this; |
|
| 97 | + |
|
| 98 | + $properties = array_map( |
|
| 99 | + function($jProp) use ($self) { |
|
| 100 | + return $self->parseProperty($jProp); |
|
| 101 | + }, |
|
| 102 | + $jComp[1] |
|
| 103 | + ); |
|
| 104 | + |
|
| 105 | + if (isset($jComp[2])) { |
|
| 106 | + |
|
| 107 | + $components = array_map( |
|
| 108 | + public function($jComp) use ($self) { |
|
| 109 | + return $self->parseComponent($jComp); |
|
| 110 | + }, |
|
| 111 | + $jComp[2] |
|
| 112 | + ); |
|
| 113 | + |
|
| 114 | + } else $components = []; |
|
| 115 | + |
|
| 116 | + return $this->root->createComponent( |
|
| 117 | + $jComp[0], |
|
| 118 | + array_merge($properties, $components), |
|
| 119 | + $defaults = false |
|
| 120 | + ); |
|
| 121 | + |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Parses properties. |
|
| 126 | + * |
|
| 127 | + * @param array $jProp |
|
| 128 | + * |
|
| 129 | + * @return \Sabre\VObject\Property |
|
| 130 | + */ |
|
| 131 | + public function parseProperty(array $jProp) { |
|
| 132 | + |
|
| 133 | + list( |
|
| 134 | + $propertyName, |
|
| 135 | + $parameters, |
|
| 136 | + $valueType |
|
| 137 | + ) = $jProp; |
|
| 138 | + |
|
| 139 | + $propertyName = strtoupper($propertyName); |
|
| 140 | + |
|
| 141 | + // This is the default class we would be using if we didn't know the |
|
| 142 | + // value type. We're using this value later in this function. |
|
| 143 | + $defaultPropertyClass = $this->root->getClassNameForPropertyName($propertyName); |
|
| 144 | + |
|
| 145 | + $parameters = (array)$parameters; |
|
| 146 | + |
|
| 147 | + $value = array_slice($jProp, 3); |
|
| 148 | + |
|
| 149 | + $valueType = strtoupper($valueType); |
|
| 150 | + |
|
| 151 | + if (isset($parameters['group'])) { |
|
| 152 | + $propertyName = $parameters['group'] . '.' . $propertyName; |
|
| 153 | + unset($parameters['group']); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + $prop = $this->root->createProperty($propertyName, null, $parameters, $valueType); |
|
| 157 | + $prop->setJsonValue($value); |
|
| 158 | + |
|
| 159 | + // We have to do something awkward here. FlatText as well as Text |
|
| 160 | + // represents TEXT values. We have to normalize these here. In the |
|
| 161 | + // future we can get rid of FlatText once we're allowed to break BC |
|
| 162 | + // again. |
|
| 163 | + if ($defaultPropertyClass === 'Sabre\VObject\Property\FlatText') { |
|
| 164 | + $defaultPropertyClass = 'Sabre\VObject\Property\Text'; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + // If the value type we received (e.g.: TEXT) was not the default value |
|
| 168 | + // type for the given property (e.g.: BDAY), we need to add a VALUE= |
|
| 169 | + // parameter. |
|
| 170 | + if ($defaultPropertyClass !== get_class($prop)) { |
|
| 171 | + $prop["VALUE"] = $valueType; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + return $prop; |
|
| 175 | + |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Sets the input data. |
|
| 180 | + * |
|
| 181 | + * @param resource|string|array $input |
|
| 182 | + * |
|
| 183 | + * @return void |
|
| 184 | + */ |
|
| 185 | + public function setInput($input) { |
|
| 186 | + |
|
| 187 | + if (is_resource($input)) { |
|
| 188 | + $input = stream_get_contents($input); |
|
| 189 | + } |
|
| 190 | + if (is_string($input)) { |
|
| 191 | + $input = json_decode($input); |
|
| 192 | + } |
|
| 193 | + $this->input = $input; |
|
| 194 | + |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | 197 | } |