Skip to content

Commit 086ab9a

Browse files
authored
Rename Field negated mandatory to nullable (#1041)
1 parent 6657685 commit 086ab9a

File tree

11 files changed

+35
-60
lines changed

11 files changed

+35
-60
lines changed

docs/fields.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ Similar with $enum, but difference is that this array is a hash array so
135135
array keys will be used as values and array values will be used as titles
136136
for these values.
137137

138-
.. php:attr:: mandatory
138+
.. php:attr:: nullable
139139
140-
Set this to true if field value must not be NULL. Attempting to set field
140+
Set this to false if field value must NOT be NULL. Attempting to set field
141141
value to "NULL" will result in exception.
142142
Example::
143143

@@ -211,11 +211,11 @@ Set the value of the field. Same as $model->set($fieldName, $value);
211211

212212
.. php:method:: setNull
213213
214-
Set field value to NULL. This will bypass "mandatory" and "required" checks and
214+
Set field value to NULL. This will bypass "nullable" and "required" checks and
215215
should only be used if you are planning to set a different value to the field
216216
before executing save().
217217

218-
If you do not set non-null value to a mandatory field, save() will fail with
218+
If you do not set non-null value to a not-nullable field, save() will fail with
219219
exception.
220220

221221
Example::

docs/joins.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Strong and Weak joins
5757
---------------------
5858

5959
When you are joining tables, then by default a strong join is used. That means
60-
that both records are mandatory and when adding records, they will both be added
60+
that both records are not-nullable and when adding records, they will both be added
6161
and linked.
6262

6363
Weak join is used if you do not really want to modify the other table.

src/Field.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public function normalize($value)
202202
$value = $this->normalizeUsingTypecast($value);
203203

204204
if ($value === null) {
205-
if ($this->required || $this->mandatory) {
205+
if (!$this->nullable || $this->required) {
206206
throw new Exception('Must not be null');
207207
}
208208

src/Model/FieldPropertiesTrait.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ trait FieldPropertiesTrait
1515

1616
/** @var string|null DBAL type registered in \Doctrine\DBAL\Types\Type. */
1717
public ?string $type = null;
18-
/** @var bool Mandatory field must not be null. The value must be set, even if it's an empty value. */
19-
public bool $mandatory = false;
18+
/** @var bool Nullable field can be null, otherwise the value must be set, even if it is an empty value. */
19+
public bool $nullable = true;
2020
/** @var bool Required field must have non-empty value. A null value is considered empty too. */
2121
public bool $required = false;
2222

@@ -63,8 +63,6 @@ trait FieldPropertiesTrait
6363
*
6464
* By default hasOne relation ID field should be editable in forms,
6565
* but not visible in grids. UI should respect these flags.
66-
*
67-
* @var array
6866
*/
69-
public $ui = [];
67+
public array $ui = [];
7068
}

src/Persistence/Sql/Query.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,8 @@ public function table($table, $alias = null)
157157
throw new \TypeError('Array input is no longer accepted');
158158
}
159159

160-
// if table is set as sub-Query, then alias is mandatory
161160
if ($table instanceof self && $alias === null) {
162-
throw new Exception('If table is set as Query, then table alias is mandatory');
161+
throw new Exception('If table is set as subquery, then table alias is required');
163162
}
164163

165164
if (is_string($table) && $alias === null) {

src/Reference/ContainsBase.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ abstract class ContainsBase extends Reference
1414
use ContainsSeedHackTrait;
1515

1616
/** @var string Field type. */
17-
public $type = 'json';
17+
public string $type = 'json';
1818

1919
/** @var bool Is it system field? */
2020
public $system = true;
@@ -24,10 +24,8 @@ abstract class ContainsBase extends Reference
2424
*
2525
* By default hasOne relation ID field should be editable in forms,
2626
* but not visible in grids. UI should respect these flags.
27-
*
28-
* @var array
2927
*/
30-
public $ui = [];
28+
public array $ui = [];
3129

3230
/** @var string Required! We need table alias for internal use only. */
3331
protected $tableAlias = 'tbl';

src/Schema/Migrator.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public function field(string $fieldName, array $options = []): self
235235

236236
$column = $this->table->addColumn($this->getDatabasePlatform()->quoteSingleIdentifier($fieldName), $options['type']);
237237

238-
if (!($options['mandatory'] ?? false) && $refType !== self::REF_TYPE_PRIMARY) {
238+
if (($options['nullable'] ?? true) && $refType !== self::REF_TYPE_PRIMARY) {
239239
$column->setNotnull(false);
240240
}
241241

@@ -267,7 +267,7 @@ public function id(string $name = 'id'): self
267267
$options = [
268268
'type' => 'integer',
269269
'ref_type' => self::REF_TYPE_PRIMARY,
270-
'mandatory' => true,
270+
'nullable' => false,
271271
];
272272

273273
$this->field($name, $options);
@@ -299,9 +299,9 @@ public function setModel(Model $model): Model
299299
}
300300

301301
$options = [
302-
'type' => $refype !== self::REF_TYPE_NONE && empty($persistField->type) ? 'integer' : $persistField->type,
302+
'type' => $refype !== self::REF_TYPE_NONE && $persistField->type === null ? 'integer' : $persistField->type,
303303
'ref_type' => $refype,
304-
'mandatory' => ($field->mandatory || $field->required) && ($persistField->mandatory || $persistField->required),
304+
'nullable' => ($field->nullable && !$field->required) || ($persistField->nullable && !$persistField->required),
305305
];
306306

307307
$this->field($field->getPersistenceName(), $options);

tests/FieldTest.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ public function testCompare(): void
6767
$this->assertTrue($m->compare('foo', 'zzz'));
6868
}
6969

70-
public function testMandatory1(): void
70+
public function testNotNullable1(): void
7171
{
7272
$m = new Model();
73-
$m->addField('foo', ['mandatory' => true]);
73+
$m->addField('foo', ['nullable' => false]);
7474
$m = $m->createEntity();
7575
$m->set('foo', 'abc');
7676
$m->set('foo', '');
@@ -99,7 +99,7 @@ public function testRequired11(): void
9999
$m->set('foo', null);
100100
}
101101

102-
public function testMandatory2(): void
102+
public function testNotNullable2(): void
103103
{
104104
$this->setDb([
105105
'user' => [
@@ -108,7 +108,7 @@ public function testMandatory2(): void
108108
]);
109109

110110
$m = new Model($this->db, ['table' => 'user']);
111-
$m->addField('name', ['mandatory' => true]);
111+
$m->addField('name', ['nullable' => false]);
112112
$m->addField('surname');
113113
$this->expectException(Exception::class);
114114
$m->insert(['surname' => 'qq']);
@@ -129,7 +129,7 @@ public function testRequired2(): void
129129
$m->insert(['surname' => 'qq', 'name' => '']);
130130
}
131131

132-
public function testMandatory3(): void
132+
public function testNotNullable3(): void
133133
{
134134
$this->setDb([
135135
'user' => [
@@ -138,14 +138,14 @@ public function testMandatory3(): void
138138
]);
139139

140140
$m = new Model($this->db, ['table' => 'user']);
141-
$m->addField('name', ['mandatory' => true]);
141+
$m->addField('name', ['nullable' => false]);
142142
$m->addField('surname');
143143
$m = $m->load(1);
144144
$this->expectException(Exception::class);
145145
$m->save(['name' => null]);
146146
}
147147

148-
public function testMandatory4(): void
148+
public function testNotNullable4(): void
149149
{
150150
$this->setDb([
151151
'user' => [
@@ -154,7 +154,7 @@ public function testMandatory4(): void
154154
]);
155155

156156
$m = new Model($this->db, ['table' => 'user']);
157-
$m->addField('name', ['mandatory' => true, 'default' => 'NoName']);
157+
$m->addField('name', ['nullable' => false, 'default' => 'NoName']);
158158
$m->addField('surname');
159159
$m->insert(['surname' => 'qq']);
160160
$this->assertEquals([
@@ -779,7 +779,7 @@ public function testSetNull(): void
779779
{
780780
$m = new Model();
781781
$m->addField('a');
782-
$m->addField('b', ['mandatory' => true]);
782+
$m->addField('b', ['nullable' => false]);
783783
$m->addField('c', ['required' => true]);
784784
$m = $m->createEntity();
785785

@@ -814,7 +814,7 @@ public function testEntityFieldPair(): void
814814
{
815815
$m = new Model();
816816
$m->addField('foo');
817-
$m->addField('bar', ['mandatory' => true]);
817+
$m->addField('bar', ['nullable' => false]);
818818

819819
$entity = $m->createEntity();
820820
$entityFooField = new Model\EntityFieldPair($entity, 'foo');

tests/Persistence/Sql/QueryTest.php

+7-27
Original file line numberDiff line numberDiff line change
@@ -147,73 +147,53 @@ public function testFieldDuplicateAliasException(): void
147147
}
148148

149149
/**
150-
* Alias is NOT mandatory when pass table as Expression.
151-
*
152150
* @doesNotPerformAssertions
153151
*/
154-
public function testTableException3(): void
152+
public function testTableNoAliasExpressionException(): void
155153
{
156154
// $this->expectException(Exception::class); // no more
157155
$this->q()->table($this->q()->expr('test'));
158156
}
159157

160-
/**
161-
* Alias is IS mandatory when pass table as Query.
162-
*/
163-
public function testTableException4(): void
158+
public function testTableNoAliasQueryException(): void
164159
{
165160
$this->expectException(Exception::class);
166161
$this->q()->table($this->q()->table('test'));
167162
}
168163

169-
/**
170-
* Table aliases should be unique.
171-
*/
172-
public function testTableException5(): void
164+
public function testTableAliasNotUniqueException(): void
173165
{
174166
$this->expectException(Exception::class);
175167
$this->q()
176168
->table('foo', 'a')
177169
->table('bar', 'a');
178170
}
179171

180-
/**
181-
* Table aliases should be unique.
182-
*/
183-
public function testTableException6(): void
172+
public function testTableAliasNotUniqueException2(): void
184173
{
185174
$this->expectException(Exception::class);
186175
$this->q()
187176
->table('foo', 'bar')
188177
->table('bar');
189178
}
190179

191-
/**
192-
* Table aliases should be unique.
193-
*/
194-
public function testTableException7(): void
180+
public function testTableAliasNotUniqueException3(): void
195181
{
196182
$this->expectException(Exception::class);
197183
$this->q()
198184
->table('foo')
199185
->table('foo');
200186
}
201187

202-
/**
203-
* Table aliases should be unique.
204-
*/
205-
public function testTableException8(): void
188+
public function testTableAliasNotUniqueException4(): void
206189
{
207190
$this->expectException(Exception::class);
208191
$this->q()
209192
->table($this->q()->table('test'), 'foo')
210193
->table('foo');
211194
}
212195

213-
/**
214-
* Table aliases should be unique.
215-
*/
216-
public function testTableException9(): void
196+
public function testTableAliasNotUniqueException5(): void
217197
{
218198
$this->expectException(Exception::class);
219199
$this->q()

tests/Persistence/StaticTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ protected function init(): void
7575
{
7676
parent::init();
7777

78+
$this->getField('id')->nullable = false;
7879
$this->getField('id')->required = false;
79-
$this->getField('id')->mandatory = true;
8080
}
8181
};
8282

tests/Util/DeepCopyTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected function init(): void
8888
$this->addField('type', ['enum' => ['invoice', 'quote']]);
8989
$this->addCondition('type', '=', 'invoice');
9090

91-
$this->addField('qty', ['type' => 'integer', 'mandatory' => true]);
91+
$this->addField('qty', ['type' => 'integer', 'nullable' => false]);
9292
$this->addField('price', ['type' => 'atk4_money']);
9393
$this->addField('vat', ['type' => 'float', 'default' => 0.21]);
9494

0 commit comments

Comments
 (0)