-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathValidator.php
More file actions
90 lines (80 loc) · 3.02 KB
/
Validator.php
File metadata and controls
90 lines (80 loc) · 3.02 KB
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
<?php
namespace Codesleeve\Stapler;
use Codesleeve\Stapler\Interfaces\Validator as ValidatorInterface;
class Validator implements ValidatorInterface
{
/**
* Validate the attachment options for an attachment type.
* A url is required to have either an :id or an :id_partition interpolation.
*
* @param array $options
*/
public function validateOptions(array $options)
{
switch ($options['storage']) {
case 's3':
$this->validateS3Options($options);
break;
case 'gcs':
$this->validateGCSOptions($options);
break;
case 'filesystem':
default:
$this->validateFilesystemOptions($options);
}
}
/**
* Validate the attachment options for an attachment type when the storage
* driver is set to 'filesystem'.
*
* @throws Exceptions\InvalidUrlOptionException
*
* @param array $options
*/
protected function validateFilesystemOptions(array $options)
{
if (preg_match("/:id\b/", $options['url']) !== 1 && preg_match("/:id_partition\b/", $options['url']) !== 1 && preg_match("/:(secure_)?hash\b/", $options['url']) !== 1) {
throw new Exceptions\InvalidUrlOptionException('Invalid Url: an id, id_partition, hash, or secure_hash interpolation is required.', 1);
}
}
/**
* Validate the attachment options for an attachment type when the storage
* driver is set to 's3'.
*
* @throws Exceptions\InvalidUrlOptionException
*
* @param array $options
*/
protected function validateS3Options(array $options)
{
if (!$options['s3_object_config']['Bucket']) {
throw new Exceptions\InvalidUrlOptionException('Invalid Path: a bucket is required for s3 storage.', 1);
}
if (!$options['s3_client_config']['secret']) {
throw new Exceptions\InvalidUrlOptionException('Invalid Path: a secret is required for s3 storage.', 1);
}
if (!$options['s3_client_config']['key']) {
throw new Exceptions\InvalidUrlOptionException('Invalid Path: a key is required for s3 storage.', 1);
}
}
/**
* Validate the attachment options for an attachment type when the storage
* driver is set to 'gcs'.
*
* @throws InvalidUrlOptionException
*
* @param array $options
*/
protected function validateGCSOptions(array $options)
{
if (!$options['google_cloud_project_id']) {
throw new Exceptions\InvalidUrlOptionException('Invalid Path: a google project id is required for gcs storage.', 1);
}
if (!$options['google_cloud_key_file']) {
throw new Exceptions\InvalidUrlOptionException('Invalid Path: a google key file is required for gcs storage.', 1);
}
if (!$options['google_cloud_storage_bucket']) {
throw new Exceptions\InvalidUrlOptionException('Invalid Path: a bucket is required for gcs storage.', 1);
}
}
}