|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of graze/sprout. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright © 2018 Nature Delivered Ltd. <https://www.graze.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license https://github.com/graze/sprout/blob/master/LICENSE.md |
|
11
|
|
|
* @link https://github.com/graze/sprout |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Graze\Sprout\Config; |
|
15
|
|
|
|
|
16
|
|
|
use Graze\ConfigValidation\ConfigValidatorInterface; |
|
17
|
|
|
use Graze\ConfigValidation\Validate; |
|
18
|
|
|
use Respect\Validation\Validator as v; |
|
19
|
|
|
|
|
20
|
|
|
class SchemaConfig implements SchemaConfigInterface |
|
21
|
|
|
{ |
|
22
|
|
|
const CONFIG_SCHEMA = 'schema'; |
|
23
|
|
|
const CONFIG_EXCLUDE = 'exclude'; |
|
24
|
|
|
const CONFIG_CONNECTION = 'connection'; |
|
25
|
|
|
const CONFIG_DIR_NAME = 'dirName'; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
private $schema; |
|
29
|
|
|
/** @var string[] */ |
|
30
|
|
|
private $exclude; |
|
31
|
|
|
/** @var ConnectionConfigInterface */ |
|
32
|
|
|
private $connection; |
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
private $dirName; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* SchemaConfig constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $options Array of options |
|
40
|
|
|
* |
|
41
|
|
|
* @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(array $options = []) |
|
44
|
|
|
{ |
|
45
|
|
|
$options = static::getValidator()->validate($options); |
|
46
|
|
|
$this->schema = $options[static::CONFIG_SCHEMA]; |
|
47
|
|
|
$this->exclude = $options[static::CONFIG_EXCLUDE]; |
|
48
|
|
|
$this->connection = new ConnectionConfig($options[static::CONFIG_CONNECTION]); |
|
49
|
|
|
$this->dirName = $options[static::CONFIG_DIR_NAME] ?? $this->schema; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getSchema(): string |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->schema; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return ConnectionConfigInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getConnection(): ConnectionConfigInterface |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->connection; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return ConfigValidatorInterface |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function getValidator(): ConfigValidatorInterface |
|
72
|
|
|
{ |
|
73
|
|
|
return Validate::arr(false) |
|
74
|
|
|
->addChild(static::CONFIG_CONNECTION, ConnectionConfig::getValidator()) |
|
75
|
|
|
->optional(static::CONFIG_SCHEMA, v::stringType()) |
|
76
|
|
|
->optional(static::CONFIG_EXCLUDE, v::arrayVal()->each(v::stringType()), []) |
|
77
|
|
|
->optional(static::CONFIG_DIR_NAME, v::stringType()); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return string[] |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getExcludes(): array |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->exclude; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getDirName(): string |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->dirName; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|