Installation
composer require amare53/helper-bundle
Add to config/bundles.php:
Amare53\HelperBundle\Amare53HelperBundle::class => ['all' => true],
First Use Case: Dynamic Property Access
Use the PropertyAccessHelper for safe property access (avoids UndefinedPropertyException):
use Amare53\HelperBundle\Helper\PropertyAccessHelper;
$helper = new PropertyAccessHelper();
$value = $helper->getValue($object, 'nested.property', 'default');
Key Entry Points
PropertyAccessHelper (safe property access)SerializerHelper (JSON/XML serialization)ValidatorHelper (Symfony Validator wrapper)PaginatorHelper (KNP Paginator integration)Replace manual isset() checks with PropertyAccessHelper:
public function show(User $user)
{
$profilePicture = $this->propertyAccessHelper->getValue($user, 'profile.picture');
return view('user.profile', compact('profilePicture'));
}
Convert complex objects to arrays/JSON:
$serialized = $this->serializerHelper->serialize($entity, 'json');
return response()->json($serialized);
Reuse ValidatorHelper for consistent validation logic:
$errors = $this->validatorHelper->validate($data, [
'email' => 'email|required',
'age' => 'integer|min:18'
]);
Integrate with KNP Paginator:
$paginated = $this->paginatorHelper->paginate(
$queryBuilder,
$request->query->getInt('page', 1),
20
);
return $this->serializerHelper->serialize($paginated, 'json');
Bind helpers in services.php:
$app->bind(PropertyAccessHelper::class, function ($app) {
return new PropertyAccessHelper();
});
PropertyAccessHelper to avoid hydration issues with nested associations.SerializerHelper + ValidatorHelper for request/response handling.isset($obj->prop) with $helper->hasValue($obj, 'prop').PropertyAccessHelper to isolate property access logic.Doctrine ORM Caching
PropertyAccessHelper may bypass Doctrine’s proxy hydration. Use ->getValue($entity, 'prop', null) to avoid LazyLoadingException.Symfony Validator Conflicts
ValidatorHelper constraints don’t override global validation rules.KNP Paginator Version Mismatch
knp-paginator-bundle:^5.6. Downgrade or upgrade dependencies to avoid:
Class 'Knp\Bundle\PaginatorBundle\Paginator\AbstractPaginator' not found
Serialization Edge Cases
SerializerHelper may cause infinite loops. Use ignore_circular_references: true in config.Property Access Issues Enable debug mode and check for:
$helper->getValue($obj, 'invalid.path'); // Returns `null` (not throws exception)
Validator Errors Inspect raw errors:
$errors = $this->validatorHelper->validate($data, $rules);
foreach ($errors as $error) {
logger($error->getMessage());
}
Custom Property Access Strategies
Extend PropertyAccessHelper to add custom accessors:
class CustomPropertyAccessHelper extends PropertyAccessHelper
{
public function getValue($object, $property, $default = null)
{
if ($property === 'custom_method') {
return $object->customMethod();
}
return parent::getValue($object, $property, $default);
}
}
Serializer Encoders
Register custom encoders in services.yaml:
amare53_helper.serializer.encoder.custom:
class: App\Serializer\CustomEncoder
tags: ['amare53_helper.serializer.encoder']
Validator Constraints
Add custom constraints to ValidatorHelper:
$this->validatorHelper->addConstraint('custom_rule', new CustomConstraint());
Default Serialization Groups
Override in config/packages/amare53_helper.yaml:
amare53_helper:
serializer:
default_groups: ['api', 'default']
Paginator Defaults Set global pagination settings:
amare53_helper:
paginator:
default_items_per_page: 15
max_items_per_page: 100
How can I help you explore Laravel packages today?