Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unreasonable use of JSON_FORCE_OBJECT in PersistenceStrategies' implementations #87

Closed
afoeder opened this issue Jun 21, 2017 · 4 comments · Fixed by #93
Closed

Unreasonable use of JSON_FORCE_OBJECT in PersistenceStrategies' implementations #87

afoeder opened this issue Jun 21, 2017 · 4 comments · Fixed by #93

Comments

@afoeder
Copy link

afoeder commented Jun 21, 2017

In every implementation of \Prooph\EventStore\Pdo\PersistenceStrategy::prepareData there are json_encode occurrences with the JSON_FORCE_OBJECT flag set:

public function prepareData(Iterator $streamEvents): array
    {
        $data = [];

        foreach ($streamEvents as $event) {
            $data[] = $event->uuid()->toString();
            $data[] = $event->messageName();
            $data[] = json_encode($event->payload(), \JSON_FORCE_OBJECT);
            $data[] = json_encode($event->metadata(), \JSON_FORCE_OBJECT);
            $data[] = $event->createdAt()->format('Y-m-d\TH:i:s.u');
        }

        return $data;
    }

This results in plain arrays being handled like objects, i.e. array('hoo', 'ray') results in {"0":"hoo","1":"ray"}" (3v4l example)

From the PHP docs:

JSON_FORCE_OBJECT Outputs an object rather than an array when a non-associative array is used. Especially useful when the recipient of the output is expecting an object and the array is empty. Available since PHP 5.3.0.

Please reconsider its necessity.

// additional note: decoding it back with second argument (assoc) true will restore a properly indexed array: https://3v4l.org/Brr7i

@bweston92
Copy link
Member

@afoeder unfortunately PHPUnit thinks it is fine because the keys match. See #91

@afoeder
Copy link
Author

afoeder commented Jun 23, 2017

I disagree with closing it. Just because no presently existing unit test fails or changes doesn't mean the implementation ist correct.
It coincidentally works when reconstituting from the database, but the intermediate format is incorrect as a matter of fact and I propose fixing it...

@prolic
Copy link
Member

prolic commented Jun 23, 2017 via email

@afoeder
Copy link
Author

afoeder commented Jun 24, 2017

sorry @prolic, read that via mobile and didn't see it was closed through #93. Thank you!

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 a pull request may close this issue.

3 participants