/** * @param FieldAccess $access * * @return void * * @throws \ErrorException|\Exception */ public function __invoke(FieldAccess $access) { if (FieldAccess::WRITE !== $access->getAccessType()) { return; } $that = $access->getThis(); $contextClass = $that ? get_class($that) : $access->getField()->getDeclaringClass()->getName(); $baseCheckers = [new IntegerTypeChecker(), new CallableTypeChecker(), new StringTypeChecker(), new GenericObjectTypeChecker(), new ObjectTypeChecker(), new ResourceTypeChecker(), new MixedTypeChecker(), new NullTypeChecker()]; (new ApplyTypeChecks(new TypedTraversableChecker(...$baseCheckers), ...$baseCheckers))->__invoke((new PropertyTypeFinder())->__invoke($access->getField(), $contextClass), $access->getValueToSet()); }
/** * Advice that controls an access to the properties * * @param FieldAccess $fieldAccess Joinpoint * * @Around("access(public|protected Demo\Example\PropertyDemo->*)") * @return mixed */ public function aroundFieldAccess(FieldAccess $fieldAccess) { $isRead = $fieldAccess->getAccessType() == FieldAccess::READ; // proceed all internal advices $fieldAccess->proceed(); if ($isRead) { // if you want to change original property value, then return it by reference $value = $fieldAccess->getValue(); } else { // if you want to change value to set, then return it by reference $value = $fieldAccess->getValueToSet(); } echo "Calling After Interceptor for ", $fieldAccess, ", value: ", json_encode($value), PHP_EOL; }
/** * @param FieldAccess $access * * @return void * * @throws \RuntimeException */ public function __invoke(FieldAccess $access) { if (!($that = $access->getThis())) { return; } if (FieldAccess::WRITE !== $access->getAccessType()) { return; } $field = $access->getField(); $field->setAccessible(true); // simplistic check - won't check for multiple assignments of "null" to a "null" valued field if (null === ($currentValue = $field->getValue($that))) { return; } if (!(new DocBlock($field))->getTagsByName('immutable')) { return; } $newValue = $access->getValueToSet(); throw new \RuntimeException(sprintf('Trying to overwrite property %s#$%s of object %s#%s with a value of type "%s".' . ' The property was already given a value of type %s', $field->getDeclaringClass()->getName(), $field->getName(), get_class($that), spl_object_hash($that), is_object($newValue) ? get_class($newValue) : gettype($newValue), is_object($currentValue) ? get_class($currentValue) : gettype($currentValue))); }