11<?php
22
3+ declare (strict_types=1 );
4+
35namespace Keboola \Datatype \Definition ;
46
57abstract class Common implements DefinitionInterface
68{
7- const KBC_METADATA_KEY_TYPE = " KBC.datatype.type " ;
8- const KBC_METADATA_KEY_NULLABLE = " KBC.datatype.nullable " ;
9- const KBC_METADATA_KEY_BASETYPE = " KBC.datatype.basetype " ;
10- const KBC_METADATA_KEY_LENGTH = " KBC.datatype.length " ;
11- const KBC_METADATA_KEY_DEFAULT = " KBC.datatype.default " ;
9+ public const KBC_METADATA_KEY_TYPE = ' KBC.datatype.type ' ;
10+ public const KBC_METADATA_KEY_NULLABLE = ' KBC.datatype.nullable ' ;
11+ public const KBC_METADATA_KEY_BASETYPE = ' KBC.datatype.basetype ' ;
12+ public const KBC_METADATA_KEY_LENGTH = ' KBC.datatype.length ' ;
13+ public const KBC_METADATA_KEY_DEFAULT = ' KBC.datatype.default ' ;
1214
13- const KBC_METADATA_KEY_COMPRESSION = " KBC.datatype.compression " ;
14- const KBC_METADATA_KEY_FORMAT = " KBC.datatype.format " ;
15+ public const KBC_METADATA_KEY_COMPRESSION = ' KBC.datatype.compression ' ;
16+ public const KBC_METADATA_KEY_FORMAT = ' KBC.datatype.format ' ;
1517
16- /**
17- * @var string
18- */
19- protected $ type ;
18+ protected string $ type ;
2019
21- /**
22- * @var string
23- */
24- protected $ length = null ;
20+ protected ?string $ length = null ;
2521
26- /**
27- * @var bool
28- */
29- protected $ nullable = true ;
22+ protected bool $ nullable = true ;
3023
31- /**
32- * @var string
33- */
34- protected $ default = null ;
24+ protected ?string $ default = null ;
3525
3626 /**
3727 * Common constructor.
3828 *
39- * @param string $type
40- * @param array $options -- length, nullable, default
29+ * @param array{length?:string|null, nullable?:bool, default?:string|null} $options
4130 */
42- public function __construct ($ type , $ options = [])
31+ public function __construct (string $ type , array $ options = [])
4332 {
4433 $ this ->type = $ type ;
4534 if (isset ($ options ['length ' ])) {
@@ -53,103 +42,86 @@ public function __construct($type, $options = [])
5342 }
5443 }
5544
56- /**
57- * @return string
58- */
59- public function getType ()
45+ public function getType (): string
6046 {
6147 return $ this ->type ;
6248 }
6349
64- /**
65- * @return string|null
66- */
67- public function getLength ()
50+ public function getLength (): ?string
6851 {
6952 return $ this ->length ;
7053 }
7154
72- /**
73- * @return boolean
74- */
75- public function isNullable ()
55+ public function isNullable (): bool
7656 {
7757 return $ this ->nullable ;
7858 }
7959
80- /**
81- * @return string|null
82- */
83- public function getDefault ()
60+ public function getDefault (): ?string
8461 {
8562 return $ this ->default ;
8663 }
8764
88- /**
89- * @return string
90- */
91- abstract public function getSQLDefinition ();
65+ abstract public function getSQLDefinition (): string ;
9266
93- /**
94- * @return string
95- */
96- abstract public function getBasetype ();
67+ abstract public function getBasetype (): string ;
9768
9869 /**
99- * @return array
70+ * @return array<mixed>
10071 */
101- abstract public function toArray ();
72+ abstract public function toArray (): array ;
10273
10374 /**
10475 * @param string|int|null $length
105- * @return bool
10676 */
107- protected function isEmpty ($ length )
77+ //phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
78+ protected function isEmpty ($ length ): bool
10879 {
10980 return $ length === null || $ length === '' ;
11081 }
11182
11283 /**
113- * @return array
84+ * @return array<int, array{key:string,value:mixed}>
11485 */
115- public function toMetadata ()
86+ public function toMetadata (): array
11687 {
11788 $ metadata = [
11889 [
119- " key " => self ::KBC_METADATA_KEY_TYPE ,
120- " value " => $ this ->getType (),
90+ ' key ' => self ::KBC_METADATA_KEY_TYPE ,
91+ ' value ' => $ this ->getType (),
12192 ],[
122- " key " => self ::KBC_METADATA_KEY_NULLABLE ,
123- " value " => $ this ->isNullable ()
93+ ' key ' => self ::KBC_METADATA_KEY_NULLABLE ,
94+ ' value ' => $ this ->isNullable (),
12495 ],[
125- " key " => self ::KBC_METADATA_KEY_BASETYPE ,
126- " value " => $ this ->getBasetype ()
127- ]
96+ ' key ' => self ::KBC_METADATA_KEY_BASETYPE ,
97+ ' value ' => $ this ->getBasetype (),
98+ ],
12899 ];
129100 if ($ this ->getLength ()) {
130101 $ metadata [] = [
131- " key " => self ::KBC_METADATA_KEY_LENGTH ,
132- " value " => $ this ->getLength ()
102+ ' key ' => self ::KBC_METADATA_KEY_LENGTH ,
103+ ' value ' => $ this ->getLength (),
133104 ];
134105 }
135106 if (!is_null ($ this ->getDefault ())) {
136107 $ metadata [] = [
137- " key " => self ::KBC_METADATA_KEY_DEFAULT ,
138- " value " => $ this ->getDefault (),
108+ ' key ' => self ::KBC_METADATA_KEY_DEFAULT ,
109+ ' value ' => $ this ->getDefault (),
139110 ];
140111 }
141112 return $ metadata ;
142113 }
143114
144115 /**
145- * @param string|null $length
146- * @param int $firstMax
147- * @param int $secondMax
148- * @param bool $firstMustBeBigger
149- * @return bool
116+ * @param null|int|string $length
150117 */
151- protected function validateNumericLength ($ length , $ firstMax , $ secondMax , $ firstMustBeBigger = true )
152- {
118+ //phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
119+ protected function validateNumericLength (
120+ $ length ,
121+ int $ firstMax ,
122+ int $ secondMax ,
123+ bool $ firstMustBeBigger = true
124+ ): bool {
153125 if ($ this ->isEmpty ($ length )) {
154126 return true ;
155127 }
@@ -169,20 +141,14 @@ protected function validateNumericLength($length, $firstMax, $secondMax, $firstM
169141 if (isset ($ parts [1 ]) && ((int ) $ parts [1 ] > $ secondMax )) {
170142 return false ;
171143 }
172-
173- if ($ firstMustBeBigger && isset ($ parts [1 ]) && (int ) $ parts [1 ] > (int ) $ parts [0 ]) {
174- return false ;
175- }
176- return true ;
144+ return !($ firstMustBeBigger && isset ($ parts [1 ]) && (int ) $ parts [1 ] > (int ) $ parts [0 ]);
177145 }
178146
179147 /**
180148 * @param string|int|null $length
181- * @param int $max
182- * @param int $min
183- * @return bool
184149 */
185- protected function validateMaxLength ($ length , $ max , $ min = 1 )
150+ //phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
151+ protected function validateMaxLength ($ length , int $ max , int $ min = 1 ): bool
186152 {
187153 if ($ this ->isEmpty ($ length )) {
188154 return true ;
@@ -191,9 +157,9 @@ protected function validateMaxLength($length, $max, $min = 1)
191157 if (!is_numeric ($ length )) {
192158 return false ;
193159 }
194- if (( int ) $ length < $ min || ( int ) $ length > $ max ) {
160+ if (filter_var ( $ length, FILTER_VALIDATE_INT ) === false ) {
195161 return false ;
196162 }
197- return true ;
163+ return ( int ) $ length >= $ min && ( int ) $ length <= $ max ;
198164 }
199165}
0 commit comments