• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ICanBoogie / bind-activerecord / 4325699063

pending completion
4325699063

push

github

Olivier Laviale
Use SchemaBuilder

3 of 3 new or added lines in 1 file covered. (100.0%)

57 of 134 relevant lines covered (42.54%)

0.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

43.59
/lib/ConfigBuilder.php
1
<?php
2

3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace ICanBoogie\Binding\ActiveRecord;
13

14
use Closure;
15
use ICanBoogie\ActiveRecord;
16
use ICanBoogie\ActiveRecord\ConnectionOptions;
17
use ICanBoogie\ActiveRecord\Model;
18
use ICanBoogie\ActiveRecord\SchemaBuilder;
19
use ICanBoogie\ActiveRecord\Table;
20
use ICanBoogie\Config\Builder;
21
use InvalidArgumentException;
22
use LogicException;
23

24
use function array_filter;
25
use function preg_match;
26

27
/**
28
 * @implements Builder<Config>
29
 */
30
final class ConfigBuilder implements Builder
31
{
32
    private const REGEXP_TIMEZONE = '/^[-+]\d{2}:\d{2}$/';
33

34
    public static function get_fragment_filename(): string
35
    {
36
        return 'activerecord';
×
37
    }
38

39
    /**
40
     * @param array<int|string, mixed> $array
41
     *
42
     * @return array<int|string, mixed>
43
     */
44
    private static function filter_non_null(array $array): array
45
    {
46
        return array_filter($array, fn(mixed $v): bool => $v !== null);
1✔
47
    }
48

49
    /**
50
     * @var array<string, array<ConnectionOptions::*, mixed>>
51
     */
52
    private array $connections = [];
53

54
    /**
55
     * @var array<string, array<Model::*, mixed>>
56
     */
57
    private array $models = [];
58

59
    public function build(): Config
60
    {
61
        return new Config($this->connections, $this->models);
1✔
62
    }
63

64
    /**
65
     * @return $this
66
     */
67
    public function add_connection(
68
        string $id,
69
        string $dsn,
70
        string|null $username = null,
71
        string|null $password = null,
72
        string|null $table_name_prefix = null,
73
        string $charset_and_collate = ConnectionOptions::DEFAULT_CHARSET_AND_COLLATE,
74
        string $time_zone = ConnectionOptions::DEFAULT_TIMEZONE,
75
    ): self {
76
        $this->assert_time_zone($time_zone);
1✔
77

78
        $this->connections[$id] = self::filter_non_null([ // @phpstan-ignore-line
1✔
79
            'dsn' => $dsn,
1✔
80
            'username' => $username,
1✔
81
            'password' => $password,
1✔
82
            'options' => self::filter_non_null([
1✔
83
                ConnectionOptions::ID => $id,
1✔
84
                ConnectionOptions::TABLE_NAME_PREFIX => $table_name_prefix,
1✔
85
                ConnectionOptions::CHARSET_AND_COLLATE => $charset_and_collate,
1✔
86
                ConnectionOptions::TIMEZONE => $time_zone,
1✔
87
            ])
1✔
88
        ]);
1✔
89

90
        return $this;
1✔
91
    }
92

93
    private function assert_time_zone(string $time_zone): void
94
    {
95
        $pattern = self::REGEXP_TIMEZONE;
1✔
96

97
        if (!preg_match($pattern, $time_zone)) {
1✔
98
            throw new InvalidArgumentException("Time zone doesn't match pattern '$pattern': $time_zone");
×
99
        }
100
    }
101

102
    /**
103
     * @param (Closure(SchemaBuilder $schema_builder): void) $schema_fn
104
     */
105
    public function add_model(
106
        string $id,
107
        Closure $schema_builder,
108
        string $activerecord_class,
109
        string $connection = 'primary',
110
        string|null $name = null,
111
        string|null $alias = null,
112
        string|null $extends = null,
113
        string|null $implements = null,
114
        string|null $model_class = null,
115
        string|null $query_class = null,
116
        mixed $belongs_to = null,
117
        mixed $has_many = null,
118
    ): self {
119
        if ($activerecord_class === ActiveRecord::class) {
×
120
            throw new LogicException("\$activerecord_class must be an extension of ICanBoogie\ActiveRecord");
×
121
        }
122

123
        $inner = new SchemaBuilder();
×
124
        $schema_builder($inner);
×
125
        $schema = $inner->build();
×
126

127
        $this->models[$id] = self::filter_non_null([ // @phpstan-ignore-line
×
128
            Table::SCHEMA => $schema,
×
129
            Table::CONNECTION => $connection,
×
130
            Table::NAME => $name,
×
131
            Table::ALIAS => $alias,
×
132
            Table::EXTENDING => $extends,
×
133
            Table::IMPLEMENTING => $implements,
×
134
            Model::ID => $id,
×
135
            Model::ACTIVERECORD_CLASS => $activerecord_class,
×
136
            Model::CLASSNAME => $model_class,
×
137
            Model::QUERY_CLASS => $query_class,
×
138
            Model::BELONGS_TO => $belongs_to,
×
139
            Model::HAS_MANY => $has_many,
×
140
        ]);
×
141

142
        return $this;
×
143
    }
144
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc