Last active
June 4, 2023 10:08
-
-
Save stefro/be3dc0ab8c458985c53194c61b5fa531 to your computer and use it in GitHub Desktop.
WithLockedPublicPropertiesTrait
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Exceptions; | |
use Exception; | |
class LockedPublicPropertyTamperException extends Exception | |
{ | |
// | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Traits; | |
use App\Exceptions\LockedPublicPropertyTamperException; | |
use ReflectionException; | |
use Str; | |
trait WithLockedPublicPropertiesTrait | |
{ | |
/** | |
* @throws LockedPublicPropertyTamperException|ReflectionException | |
*/ | |
public function updatingWithLockedPublicPropertiesTrait($name): void | |
{ | |
$propertyName = Str::of($name)->explode('.')->first(); | |
$reflectionProperty = new \ReflectionProperty($this, $propertyName); | |
if (Str::of($reflectionProperty->getDocComment())->contains('@locked')) { | |
throw new LockedPublicPropertyTamperException("You are not allowed to tamper with the protected property {$propertyName}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment