Skip to content

Adds beforeSerialize and afterSerialize events#156

Open
cnizzardini wants to merge 9 commits intomasterfrom
add-serialize-events
Open

Adds beforeSerialize and afterSerialize events#156
cnizzardini wants to merge 9 commits intomasterfrom
add-serialize-events

Conversation

@cnizzardini
Copy link
Copy Markdown
Member

Adds the following new events:

  • MixerApi.CollectionView.beforeSerialize
  • MixerApi.CollectionView.afterSerialize
  • MixerApi.HalView.beforeSerialize
  • MixerApi.HalView.afterSerialize
  • MixerApi.JsonLdView.beforeSerialize
  • MixerApi.JsonLdView.afterSerialize

@cnizzardini cnizzardini changed the base branch from master to 2.next January 28, 2025 02:02
@cnizzardini cnizzardini changed the base branch from 2.next to master April 1, 2026 02:00
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds new CakePHP event hooks around serialization for MixerApi’s CollectionView, HAL, and JSON-LD serializers so consumers can observe/adjust data pre-encode and react post-encode.

Changes:

  • Dispatch beforeSerialize and afterSerialize events from the CollectionView, HalView, and JsonLdView serializers.
  • Add serializer accessors/mutators (setData(), getRequest(), getPaginatorHelper()) to support event handlers.
  • Extend plugin test suites and READMEs to cover/document the new events.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 15 comments.

Show a summary per file
File Description
plugins/json-ld-view/src/JsonSerializer.php Dispatches JSON-LD before/after serialize events; adds setters/getters for event handlers.
plugins/json-ld-view/tests/TestCase/JsonSerializerTest.php Adds assertions that the new JSON-LD events fire.
plugins/json-ld-view/README.md Documents JSON-LD serialization events with example listeners.
plugins/hal-view/src/JsonSerializer.php Dispatches HAL before/after serialize events; adds setters/getters for event handlers.
plugins/hal-view/tests/TestCase/JsonSerializerTest.php Adds assertions that the new HAL events fire.
plugins/hal-view/README.md Documents HAL serialization events with example listeners.
plugins/collection-view/src/Serializer.php Dispatches CollectionView before/after serialize events (JSON/XML), includes type metadata.
plugins/collection-view/tests/TestCase/SerializerTest.php Adds assertions that the new CollectionView events fire for JSON and XML.
plugins/collection-view/README.md Documents CollectionView serialization events with example listeners.
Comments suppressed due to low confidence (1)

plugins/collection-view/src/Serializer.php:30

  • The $data property is declared mixed, but the docblock still says @var array. Please update the docblock to match the actual type (or tighten the property type back to array if only arrays are supported).
     * serialized data
     *
     * @var array
     */
    private mixed $data;

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


public function test_as_json(): void
{
EventManager::instance()->trackEvents(true)->setEventList(new EventList());
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EventManager::trackEvents() in CakePHP returns a boolean flag, so chaining ->setEventList(...) will call a method on a bool and fail. Split this into two statements (enable tracking, then set the EventList) before asserting events fired.

Suggested change
EventManager::instance()->trackEvents(true)->setEventList(new EventList());
$eventManager = EventManager::instance();
$eventManager->trackEvents(true);
$eventManager->setEventList(new EventList());

Copilot uses AI. Check for mistakes.

public function test_as_xml(): void
{
EventManager::instance()->trackEvents(true)->setEventList(new EventList());
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EventManager::trackEvents() returns a boolean flag and isn't chainable; ->setEventList(...) will fail at runtime here as well. Use separate calls to enable tracking and then set the EventList.

Suggested change
EventManager::instance()->trackEvents(true)->setEventList(new EventList());
$eventManager = EventManager::instance();
$eventManager->trackEvents(true);
$eventManager->setEventList(new EventList());

Copilot uses AI. Check for mistakes.

public function test_collection(): void
{
EventManager::instance()->trackEvents(true)->setEventList(new EventList());
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EventManager::trackEvents() isn't chainable (it returns a boolean flag), so ->setEventList(...) will be invoked on a bool and cause a failure. Use separate calls to enable tracking and set the EventList.

Suggested change
EventManager::instance()->trackEvents(true)->setEventList(new EventList());
$eventManager = EventManager::instance();
$eventManager->trackEvents(true);
$eventManager->setEventList(new EventList());

Copilot uses AI. Check for mistakes.
*/
public function test_collection(string $resultType): void
{
EventManager::instance()->trackEvents(true)->setEventList(new EventList());
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EventManager::trackEvents() returns a boolean flag, so chaining ->setEventList(...) will fail at runtime. Use separate calls to enable tracking and to set the EventList.

Suggested change
EventManager::instance()->trackEvents(true)->setEventList(new EventList());
$eventManager = EventManager::instance();
$eventManager->trackEvents(true);
$eventManager->setEventList(new EventList());

Copilot uses AI. Check for mistakes.
Comment on lines +114 to +116
public function setData(mixed $data): void
{
$this->data = $data;
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setData() accepts mixed (and $data is stored as mixed), but getData() is still declared as ?array. If listeners set non-array data (or if the serializer is constructed with non-array data), getData() will throw a return type TypeError. Align the getData() return type (and its docblock) with the actual $data type, or constrain setData()/$data to arrays only.

Copilot uses AI. Check for mistakes.
use \MixerApi\JsonLdView\JsonSerializer;

EventManager::instance()->on(JsonSerializer::BEFORE_SERIALIZE_EVENT, function (Event $event) {
/** @var Serializer $serializer */
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docblock says @var Serializer $serializer, but the example uses JsonSerializer and there is no Serializer type imported. Update the annotation to JsonSerializer (or fully qualify it) to avoid confusion.

Suggested change
/** @var Serializer $serializer */
/** @var JsonSerializer $serializer */

Copilot uses AI. Check for mistakes.
Comment on lines +377 to +379
EventManager::instance()->on(JsonSerializer::AFTER_SERIALIZE_EVENT, function (Event $event, string $data) {
// whatever you want to do here
})
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This afterSerialize listener example is missing a terminating semicolon after the EventManager::instance()->on(...) call, so the snippet is invalid PHP.

Copilot uses AI. Check for mistakes.
Comment on lines +176 to +182
EventManager::instance()->on(Serializer::BEFORE_SERIALIZE_EVENT, function (Event $event, string $type) {
/** @var Serializer $serializer */
$serializer = $event->getSubject();
$data = $serializer->getData(); // modify if you want or other stuff
$serializer->setData($data);
})
```
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example listener registration is missing a terminating semicolon after the EventManager::instance()->on(...) call ();), so the snippet is invalid PHP as written.

Copilot uses AI. Check for mistakes.
Comment on lines +263 to +269
EventManager::instance()->on(JsonSerializer::BEFORE_SERIALIZE_EVENT, function (Event $event) {
/** @var Serializer $serializer */
$serializer = $event->getSubject();
$data = $serializer->getData(); // modify if you want or other stuff
$serializer->setData($data);
})
```
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example listener registration is missing a terminating semicolon after the EventManager::instance()->on(...) call ();), so it’s not valid PHP as written.

Copilot uses AI. Check for mistakes.
Comment on lines +363 to +369
EventManager::instance()->on(JsonSerializer::BEFORE_SERIALIZE_EVENT, function (Event $event) {
/** @var Serializer $serializer */
$serializer = $event->getSubject();
$data = $serializer->getData(); // modify if you want or other stuff
$serializer->setData($data);
})
```
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example listener registration is missing a terminating semicolon after the EventManager::instance()->on(...) call ();), so it’s not valid PHP as written.

Copilot uses AI. Check for mistakes.
cnizzardini and others added 2 commits March 31, 2026 22:17
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants