Skip to content

Commit 7808f18

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into 4.3
2 parents e551d33 + f781dc2 commit 7808f18

File tree

14 files changed

+69
-28
lines changed

14 files changed

+69
-28
lines changed

system/Database/Exceptions/DatabaseException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ class DatabaseException extends Error implements ExceptionInterface
2020
*
2121
* @var int
2222
*/
23-
protected $code = 8;
23+
protected $code = EXIT_DATABASE;
2424
}

system/Entity/Cast/BaseCast.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ abstract class BaseCast implements CastInterface
1919
/**
2020
* Get
2121
*
22-
* @param mixed $value Data
23-
* @param array $params Additional param
22+
* @param array|bool|float|int|object|string|null $value Data
23+
* @param array $params Additional param
2424
*
25-
* @return mixed
25+
* @return array|bool|float|int|object|string|null
2626
*/
2727
public static function get($value, array $params = [])
2828
{
@@ -32,10 +32,10 @@ public static function get($value, array $params = [])
3232
/**
3333
* Set
3434
*
35-
* @param mixed $value Data
36-
* @param array $params Additional param
35+
* @param array|bool|float|int|object|string|null $value Data
36+
* @param array $params Additional param
3737
*
38-
* @return mixed
38+
* @return array|bool|float|int|object|string|null
3939
*/
4040
public static function set($value, array $params = [])
4141
{

system/Entity/Cast/CastInterface.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ interface CastInterface
1919
/**
2020
* Get
2121
*
22-
* @param mixed $value Data
23-
* @param array $params Additional param
22+
* @param array|bool|float|int|object|string|null $value Data
23+
* @param array $params Additional param
2424
*
25-
* @return mixed
25+
* @return array|bool|float|int|object|string|null
2626
*/
2727
public static function get($value, array $params = []);
2828

2929
/**
3030
* Set
3131
*
32-
* @param mixed $value Data
33-
* @param array $params Additional param
32+
* @param array|bool|float|int|object|string|null $value Data
33+
* @param array $params Additional param
3434
*
35-
* @return mixed
35+
* @return array|bool|float|int|object|string|null
3636
*/
3737
public static function set($value, array $params = []);
3838
}

system/Entity/Cast/DatetimeCast.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class DatetimeCast extends BaseCast
2424
* {@inheritDoc}
2525
*
2626
* @throws Exception
27+
*
28+
* @return Time
2729
*/
2830
public static function get($value, array $params = [])
2931
{

system/Entity/Entity.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function fill(?array $data = null)
146146
* __get() magic method so will have any casts, etc applied to them.
147147
*
148148
* @param bool $onlyChanged If true, only return values that have changed since object creation
149-
* @param bool $cast If true, properties will be casted.
149+
* @param bool $cast If true, properties will be cast.
150150
* @param bool $recursive If true, inner entities will be casted as array as well.
151151
*/
152152
public function toArray(bool $onlyChanged = false, bool $cast = true, bool $recursive = false): array
@@ -256,6 +256,8 @@ public function hasChanged(?string $key = null): bool
256256
return $this->original !== $this->attributes;
257257
}
258258

259+
$key = $this->mapProperty($key);
260+
259261
// Key doesn't exist in either
260262
if (! array_key_exists($key, $this->original) && ! array_key_exists($key, $this->attributes)) {
261263
return false;
@@ -287,7 +289,7 @@ public function setAttributes(array $data)
287289
* Checks the datamap to see if this property name is being mapped,
288290
* and returns the db column name, if any, or the original property name.
289291
*
290-
* @return string
292+
* @return string db column name
291293
*/
292294
protected function mapProperty(string $key)
293295
{
@@ -480,7 +482,7 @@ public function __get(string $key)
480482
// Convert to CamelCase for the method
481483
$method = 'get' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key)));
482484

483-
// if a set* method exists for this key,
485+
// if a get* method exists for this key,
484486
// use that method to insert this value.
485487
if (method_exists($this, $method)) {
486488
$result = $this->{$method}();

system/Entity/Exceptions/CastException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
/**
1717
* CastException is thrown for invalid cast initialization and management.
18+
*
19+
* @TODO CodeIgniter\Exceptions\CastException is deprecated and this class is used.
20+
* CodeIgniter\Exceptions\CastException has the property $code = EXIT_CONFIG,
21+
* but this class does not have the code.
1822
*/
1923
class CastException extends FrameworkException
2024
{

system/Exceptions/CastException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ class CastException extends CriticalError
2323
use DebugTraceableTrait;
2424

2525
/**
26-
* Error code
26+
* Exit status code
2727
*
2828
* @var int
2929
*/
30-
protected $code = 3;
30+
protected $code = EXIT_CONFIG;
3131

3232
public static function forInvalidJsonFormatException(int $error)
3333
{

system/Exceptions/ConfigException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ class ConfigException extends CriticalError
1919
use DebugTraceableTrait;
2020

2121
/**
22-
* Error code
22+
* Exit status code
2323
*
2424
* @var int
2525
*/
26-
protected $code = 3;
26+
protected $code = EXIT_CONFIG;
2727

2828
public static function forDisabledMigrations()
2929
{

system/Exceptions/PageNotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class PageNotFoundException extends OutOfBoundsException implements ExceptionInt
1919
use DebugTraceableTrait;
2020

2121
/**
22-
* Error code
22+
* HTTP status code
2323
*
2424
* @var int
2525
*/

system/Router/Exceptions/RedirectException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class RedirectException extends Exception
2020
{
2121
/**
22-
* Status code for redirects
22+
* HTTP status code for redirects
2323
*
2424
* @var int
2525
*/

tests/system/Debug/ExceptionsTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace CodeIgniter\Debug;
1313

14+
use CodeIgniter\Database\Exceptions\DatabaseException;
15+
use CodeIgniter\Entity\Exceptions\CastException;
16+
use CodeIgniter\Exceptions\ConfigException;
1417
use CodeIgniter\Exceptions\PageNotFoundException;
1518
use CodeIgniter\Test\CIUnitTestCase;
1619
use CodeIgniter\Test\ReflectionHelper;
@@ -57,9 +60,16 @@ public function testDetermineCodes(): void
5760
{
5861
$determineCodes = $this->getPrivateMethodInvoker($this->exception, 'determineCodes');
5962

60-
$this->assertSame([500, 9], $determineCodes(new RuntimeException('This.')));
61-
$this->assertSame([500, 1], $determineCodes(new RuntimeException('That.', 600)));
62-
$this->assertSame([404, 1], $determineCodes(new RuntimeException('There.', 404)));
63+
$this->assertSame([500, EXIT__AUTO_MIN], $determineCodes(new RuntimeException('This.')));
64+
$this->assertSame([500, EXIT_ERROR], $determineCodes(new RuntimeException('That.', 600)));
65+
$this->assertSame([404, EXIT_ERROR], $determineCodes(new RuntimeException('There.', 404)));
66+
$this->assertSame([167, EXIT_ERROR], $determineCodes(new RuntimeException('This.', 167)));
67+
// @TODO This exit code should be EXIT_CONFIG.
68+
$this->assertSame([500, 12], $determineCodes(new ConfigException('This.')));
69+
// @TODO This exit code should be EXIT_CONFIG.
70+
$this->assertSame([500, 9], $determineCodes(new CastException('This.')));
71+
// @TODO This exit code should be EXIT_DATABASE.
72+
$this->assertSame([500, 17], $determineCodes(new DatabaseException('This.')));
6373
}
6474

6575
public function testRenderBacktrace(): void

tests/system/Entity/EntityTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,24 @@ public function testHasChangedNoChange()
929929
$this->assertFalse($entity->hasChanged('default'));
930930
}
931931

932+
public function testHasChangedMappedNoChange()
933+
{
934+
$entity = $this->getEntity();
935+
936+
$entity->createdAt = null;
937+
938+
$this->assertFalse($entity->hasChanged('createdAt'));
939+
}
940+
941+
public function testHasChangedMappedChanged()
942+
{
943+
$entity = $this->getEntity();
944+
945+
$entity->createdAt = '2022-11-11 11:11:11';
946+
947+
$this->assertTrue($entity->hasChanged('createdAt'));
948+
}
949+
932950
public function testHasChangedWholeEntity()
933951
{
934952
$entity = $this->getEntity();

user_guide_src/source/models/entities.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ As an example, imagine you have the simplified User Entity that is used througho
144144
.. literalinclude:: entities/008.php
145145

146146
Your boss comes to you and says that no one uses usernames anymore, so you're switching to just use emails for login.
147-
But they do want to personalize the application a bit, so they want you to change the name field to represent a user's
147+
But they do want to personalize the application a bit, so they want you to change the ``name`` field to represent a user's
148148
full name now, not their username like it does currently. To keep things tidy and ensure things continue making sense
149-
in the database you whip up a migration to rename the `name` field to `full_name` for clarity.
149+
in the database you whip up a migration to rename the ``name`` field to ``full_name`` for clarity.
150150

151151
Ignoring how contrived this example is, we now have two choices on how to fix the User class. We could modify the class
152152
property from ``$name`` to ``$full_name``, but that would require changes throughout the application. Instead, we can
@@ -164,6 +164,10 @@ through the original ``$user->full_name``, also, as this is needed for the model
164164
to the database. However, ``unset()`` and ``isset()`` only work on the mapped property, ``$user->name``, not on the database column name,
165165
``$user->full_name``.
166166

167+
.. note:: When you use Data Mapping, you must define ``set*()`` and ``get*()`` method
168+
for the database column name. In this example, you must define ``setFullName()`` and
169+
``getFullName()``.
170+
167171
Mutators
168172
========
169173

user_guide_src/source/models/entities/009.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ class User extends Entity
88
{
99
protected $attributes = [
1010
'id' => null,
11-
'full_name' => null, // In the $attributes, the key is the column name
11+
'full_name' => null, // In the $attributes, the key is the db column name
1212
'email' => null,
1313
'password' => null,
1414
'created_at' => null,
1515
'updated_at' => null,
1616
];
1717

1818
protected $datamap = [
19+
// property_name => db_column_name
1920
'name' => 'full_name',
2021
];
2122
}

0 commit comments

Comments
 (0)