Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entity exception for non existed props. #1927

Merged
merged 5 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions system/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,14 @@ public function __get(string $key)
{
$result = $this->castAs($result, $this->_options['casts'][$key]);
}

if(! isset($result) && ! property_exists($this, $key))
{
throw EntityException::forTryingToAccessNonExistentProperty($key, get_called_class());
}

return $result;

}

//--------------------------------------------------------------------
Expand Down
22 changes: 22 additions & 0 deletions system/Exceptions/EntityException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php namespace CodeIgniter\Exceptions;

/**
* Entity Exceptions.
*/

class EntityException extends AlertError
{

/**
* Error code
*
* @var integer
*/
protected $code = 3;

public static function forTryingToAccessNonExistentProperty(string $property, string $on)
{
throw new static(lang('Entity.tryingToAccessNonExistentProperty', [$property, $on]));
}

}
19 changes: 19 additions & 0 deletions system/Language/en/Entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* CLI language strings.
*
* @package CodeIgniter
* @author CodeIgniter Dev Team
* @copyright 2014-2019 British Columbia Institute of Technology (https://bcit.ca/)
* @license https://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com
* @filesource
*
* @codeCoverageIgnore
*/

return
[
'tryingToAccessNonExistentProperty' => 'Trying to access non existent property {0} of {1}'
];