1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* ReportingCloud PHP SDK |
6
|
|
|
* |
7
|
|
|
* PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH. |
8
|
|
|
* |
9
|
|
|
* @link https://www.reporting.cloud to learn more about ReportingCloud |
10
|
|
|
* @link https://git.io/Jejj2 for the canonical source repository |
11
|
|
|
* @license https://git.io/Jejjr |
12
|
|
|
* @copyright © 2022 Text Control GmbH |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace TxTextControl\ReportingCloud\Assert; |
16
|
|
|
|
17
|
|
|
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Trait AssertTemplateNameTrait |
21
|
|
|
* |
22
|
|
|
* @package TxTextControl\ReportingCloud |
23
|
|
|
* @author Jonathan Maron (@JonathanMaron) |
24
|
|
|
*/ |
25
|
|
|
trait AssertTemplateNameTrait |
26
|
|
|
{ |
27
|
|
|
use ValueToStringTrait; |
28
|
|
|
use AssertTemplateFormatTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Check value is a valid template name |
32
|
|
|
* |
33
|
|
|
* @param string $value |
34
|
|
|
* @param string $message |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
* @throws InvalidArgumentException |
38
|
|
|
*/ |
39
|
62 |
|
public static function assertTemplateName(string $value, string $message = ''): void |
40
|
|
|
{ |
41
|
62 |
|
if (basename($value) != $value) { |
42
|
16 |
|
$format = 0 === strlen($message) ? '%1$s contains path information (\'/\', \'.\', or \'..\')' : $message; |
43
|
16 |
|
$message = sprintf($format, self::valueToString($value)); |
44
|
16 |
|
throw new InvalidArgumentException($message); |
45
|
|
|
} |
46
|
|
|
|
47
|
46 |
|
$extension = pathinfo($value, PATHINFO_EXTENSION); |
48
|
|
|
|
49
|
|
|
try { |
50
|
46 |
|
self::assertTemplateFormat(/** @scrutinizer ignore-type */ $extension); |
51
|
10 |
|
} catch (InvalidArgumentException $e) { |
52
|
10 |
|
$format = 0 === strlen($message) ? '%1$s contains an unsupported file extension' : $message; |
53
|
10 |
|
$message = sprintf($format, self::valueToString($value)); |
54
|
10 |
|
throw new InvalidArgumentException($message); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|