Skip to content

Commit 2b0bbf4

Browse files
authored
Merge pull request #1927 from nowackipawel/patch-56
Entity exception for non existed props.
2 parents bb670cc + 5a4d017 commit 2b0bbf4

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

system/Entity.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,14 @@ public function __get(string $key)
294294
{
295295
$result = $this->castAs($result, $this->_options['casts'][$key]);
296296
}
297+
298+
if(! isset($result) && ! property_exists($this, $key))
299+
{
300+
throw EntityException::forTryingToAccessNonExistentProperty($key, get_called_class());
301+
}
297302

298303
return $result;
304+
299305
}
300306

301307
//--------------------------------------------------------------------
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php namespace CodeIgniter\Exceptions;
2+
3+
/**
4+
* Entity Exceptions.
5+
*/
6+
7+
class EntityException extends AlertError
8+
{
9+
10+
/**
11+
* Error code
12+
*
13+
* @var integer
14+
*/
15+
protected $code = 3;
16+
17+
public static function forTryingToAccessNonExistentProperty(string $property, string $on)
18+
{
19+
throw new static(lang('Entity.tryingToAccessNonExistentProperty', [$property, $on]));
20+
}
21+
22+
}

system/Language/en/Entity.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* CLI language strings.
5+
*
6+
* @package CodeIgniter
7+
* @author CodeIgniter Dev Team
8+
* @copyright 2014-2019 British Columbia Institute of Technology (https://bcit.ca/)
9+
* @license https://opensource.org/licenses/MIT MIT License
10+
* @link https://codeigniter.com
11+
* @filesource
12+
*
13+
* @codeCoverageIgnore
14+
*/
15+
16+
return
17+
[
18+
'tryingToAccessNonExistentProperty' => 'Trying to access non existent property {0} of {1}'
19+
];

0 commit comments

Comments
 (0)