Passed
Push — master ( c327c2...0adcf0 )
by Ondřej
02:45
created

StdCoreFactory.php$0 ➔ compareValues()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
namespace Ivory;
4
5
use Ivory\Cache\CacheControl;
6
use Ivory\Cache\ICacheControl;
7
use Ivory\Connection\Connection;
8
use Ivory\Connection\ConnectionParameters;
9
use Ivory\Connection\IConnection;
10
use Ivory\Connection\IObservableTransactionControl;
11
use Ivory\Connection\ISessionControl;
12
use Ivory\Connection\IStatementExecution;
13
use Ivory\Connection\ITxHandle;
14
use Ivory\Connection\TxHandle;
15
use Ivory\Exception\StatementExceptionFactory;
16
use Ivory\Lang;
17
use Ivory\Lang\SqlPattern\CachingSqlPatternParser;
18
use Ivory\Lang\SqlPattern\ISqlPatternParser;
19
use Ivory\Lang\SqlPattern\SqlPatternParser;
20
use Ivory\Type\Ivory\CommandSerializer;
21
use Ivory\Type\Ivory\IdentifierSerializer;
22
use Ivory\Type\Ivory\LikeExpressionSerializer;
23
use Ivory\Type\Ivory\QuotedIdentifierSerializer;
24
use Ivory\Type\Ivory\RelationSerializer;
25
use Ivory\Type\Ivory\SqlSerializer;
26
use Ivory\Type\Std\StdTypeLoader;
27
use Ivory\Type\TypeRegister;
28
use Ivory\Value\Alg\ComparisonUtils;
29
use Ivory\Value\Alg\IValueComparator;
30
31
/**
32
 * The standard factory for the core Ivory objects.
33
 *
34
 * The factory may either be inherited or replaced altogether to change any default behaviour of Ivory. The user
35
 * supplied factory may be activated with {@link Ivory::setCoreFactory()}. Note, however, that {@link Ivory} caches the
36
 * objects so setting another core factory is only effective at the very beginning of working with Ivory.
37
 */
38
class StdCoreFactory implements ICoreFactory
39
{
40
    public function createGlobalTypeRegister(): TypeRegister
41
    {
42
        $reg = new TypeRegister();
43
        $reg->registerTypeLoader(new StdTypeLoader());
44
        $reg->registerTypeAbbreviations(Lang\Sql\Types::getReservedTypes());
45
46
        // standard value serializers
47
        $reg->registerValueSerializers([
48
            'sql' => new SqlSerializer(),
49
            'ident' => new IdentifierSerializer(),
50
            'qident' => new QuotedIdentifierSerializer(),
51
            'rel' => new RelationSerializer(),
52
            'cmd' => new CommandSerializer(),
53
            'like' => new LikeExpressionSerializer(LikeExpressionSerializer::WILDCARD_NONE),
54
            'like_' => new LikeExpressionSerializer(LikeExpressionSerializer::WILDCARD_APPEND),
55
            '_like' => new LikeExpressionSerializer(LikeExpressionSerializer::WILDCARD_PREPEND),
56
            '_like_' => new LikeExpressionSerializer(LikeExpressionSerializer::WILDCARD_BOTH),
57
        ]);
58
59
        // standard type abbreviations
60
        $reg->registerTypeAbbreviations([
61
            's' => ['pg_catalog', 'text'],
62
            'i' => ['pg_catalog', 'int8'],
63
            'num' => ['pg_catalog', 'numeric'],
64
            'f' => ['pg_catalog', 'float8'],
65
            'ts' => ['pg_catalog', 'timestamp'],
66
            'tstz' => ['pg_catalog', 'timestamptz'],
67
        ]);
68
69
        // standard type inference rules
70
        $reg->registerTypeInferenceRules([
71
            'int' => ['pg_catalog', 'int8'],
72
            'string' => ['pg_catalog', 'text'],
73
            'bool' => ['pg_catalog', 'bool'],
74
            'float' => ['pg_catalog', 'float8'],
75
            'null' => ['pg_catalog', 'text'],
76
            'array' => ['pg_catalog', 'text[]'],
77
            Value\Decimal::class => ['pg_catalog', 'numeric'],
78
            Value\Date::class => ['pg_catalog', 'date'],
79
            Value\Time::class => ['pg_catalog', 'time'],
80
            Value\TimeTz::class => ['pg_catalog', 'timetz'],
81
            Value\Timestamp::class => ['pg_catalog', 'timestamp'],
82
            Value\TimestampTz::class => ['pg_catalog', 'timestamptz'],
83
            Value\TimeInterval::class => ['pg_catalog', 'interval'],
84
            Value\FixedBitString::class => ['pg_catalog', 'bit'],
85
            Value\VarBitString::class => ['pg_catalog', 'varbit'],
86
            Value\Json::class => ['pg_catalog', 'json'],
87
            Value\XmlContent::class => ['pg_catalog', 'xml'],
88
            Value\XmlDocument::class => ['pg_catalog', 'xml'],
89
            \DOMDocument::class => ['pg_catalog', 'xml'],
90
            \DOMNode::class => ['pg_catalog', 'xml'],
91
            \DOMNodeList::class => ['pg_catalog', 'xml'],
92
            \SimpleXMLElement::class => ['pg_catalog', 'xml'],
93
            Value\Point::class => ['pg_catalog', 'point'],
94
            Value\Line::class => ['pg_catalog', 'line'],
95
            Value\LineSegment::class => ['pg_catalog', 'lseg'],
96
            Value\Box::class => ['pg_catalog', 'box'],
97
            Value\Path::class => ['pg_catalog', 'path'],
98
            Value\Polygon::class => ['pg_catalog', 'polygon'],
99
            Value\Circle::class => ['pg_catalog', 'circle'],
100
            Value\NetAddress::class => ['pg_catalog', 'inet'],
101
            Value\MacAddr::class => ['pg_catalog', 'macaddr'],
102
            Value\Money::class => ['pg_catalog', 'money'],
103
            Value\PgLogSequenceNumber::class => ['pg_catalog', 'pg_lsn'],
104
            Value\TxIdSnapshot::class => ['pg_catalog', 'txid_snapshot'],
105
            Value\TextSearchVector::class => ['pg_catalog', 'tsvector'],
106
            Value\TextSearchQuery::class => ['pg_catalog', 'tsquery'],
107
        ]);
108
109
        return $reg;
110
    }
111
112
    public function createSqlPatternParser(?ICacheControl $cacheControl = null): ISqlPatternParser
113
    {
114
        if ($cacheControl !== null && $cacheControl->isCacheEnabled()) {
115
            return new CachingSqlPatternParser($cacheControl);
116
        } else {
117
            return new SqlPatternParser();
118
        }
119
    }
120
121
    public function createStatementExceptionFactory(?IStatementExecution $stmtExecution = null): StatementExceptionFactory
122
    {
123
        return new StatementExceptionFactory();
124
    }
125
126
    public function createConnection(string $connName, ConnectionParameters $params): IConnection
127
    {
128
        return new Connection($connName, $params);
129
    }
130
131
    public function createCacheControl(?IConnection $connection = null): ICacheControl
132
    {
133
        $prefix = 'Ivory' . Ivory::VERSION . '.';
134
135
        if ($connection === null) {
136
            $postfix = '';
137
        } else {
138
            $params = $connection->getParameters();
139
            $postfix = sprintf(
140
                '.%s.%d.%s',
141
                $params->getHost(),
142
                $params->getPort(),
143
                $params->getDbName()
144
            );
145
        }
146
147
        return new CacheControl(Ivory::getDefaultCacheImpl(), $prefix, $postfix);
148
    }
149
150
    public function createTransactionHandle(
151
        IStatementExecution $stmtExec,
152
        IObservableTransactionControl $observableTxCtl,
153
        ISessionControl $sessionCtl
154
    ): ITxHandle {
155
        return new TxHandle($stmtExec, $observableTxCtl, $sessionCtl);
156
    }
157
158
    public function createDefaultValueComparator(): IValueComparator
159
    {
160
        return new class implements IValueComparator
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
161
        {
162
            public function compareValues($a, $b): int
163
            {
164
                return ComparisonUtils::compareValues($a, $b);
165
            }
166
        };
167
    }
168
}
169