1+ <?php
2+
3+ namespace EcomDev \MySQL2JSONL ;
4+
5+ use Amp \Mysql \MysqlConfig ;
6+ use JsonSchema \Constraints \Constraint ;
7+ use JsonSchema \Validator ;
8+ use JsonException ;
9+
10+ final readonly class Configuration
11+ {
12+ private function __construct (
13+ public MysqlConfig $ connection ,
14+ public TableCondition $ includeCondition ,
15+ public TableCondition $ excludeCondition ,
16+ )
17+ {
18+
19+ }
20+
21+ /**
22+ * Creates configuration based on JSON value supported by schema.json
23+ *
24+ * @throws ConfigurationException|JsonException
25+ */
26+ public static function fromJSON (string $ json ): Configuration
27+ {
28+ $ validator = new Validator ();
29+ $ schema = json_decode (
30+ file_get_contents (dirname (__DIR__ ) . DIRECTORY_SEPARATOR . 'schema.json ' ),
31+ flags: JSON_THROW_ON_ERROR
32+ );
33+ $ data = json_decode (
34+ $ json ,
35+ flags: JSON_THROW_ON_ERROR
36+ );
37+ $ result = $ validator ->validate ($ data , $ schema , Constraint::CHECK_MODE_APPLY_DEFAULTS );
38+
39+ if (($ result & Validator::ERROR_DOCUMENT_VALIDATION ) === Validator::ERROR_DOCUMENT_VALIDATION ) {
40+ $ errors = array_reduce (
41+ $ validator ->getErrors (),
42+ function ($ errors , $ error ) {
43+ $ errors [$ error ['property ' ]] = $ error ['message ' ];
44+ return $ errors ;
45+ }
46+ );
47+
48+ throw ConfigurationException::fromErrors ($ errors );
49+ }
50+
51+
52+ return new self (
53+ new MysqlConfig (
54+ host: $ data ->connection ->host ,
55+ port: (int ) $ data ->connection ->port ,
56+ user: $ data ->connection ->user ,
57+ password: $ data ->connection ->password ,
58+ database: $ data ->connection ->database ,
59+ ),
60+ StaticTableCondition::alwaysTrue (),
61+ StaticTableCondition::alwaysFalse (),
62+ );
63+ }
64+
65+ public static function fromMySQLConfig (MysqlConfig $ config ): self
66+ {
67+ return new self ($ config , StaticTableCondition::alwaysTrue (), StaticTableCondition::alwaysFalse ());
68+ }
69+
70+ public function withIncludeCondition (TableCondition $ condition ): self
71+ {
72+
73+ }
74+
75+ public function withExcludeCondition (TableCondition $ condition ): self
76+ {
77+
78+ }
79+ }
0 commit comments