Skip to content

Instantly share code, notes, and snippets.

@stefro
Last active June 4, 2023 10:08
Show Gist options
  • Save stefro/be3dc0ab8c458985c53194c61b5fa531 to your computer and use it in GitHub Desktop.
Save stefro/be3dc0ab8c458985c53194c61b5fa531 to your computer and use it in GitHub Desktop.
WithLockedPublicPropertiesTrait
<?php
namespace App\Exceptions;
use Exception;
class LockedPublicPropertyTamperException extends Exception
{
//
}
<?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