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

Merge feature/improve-projection into master #9

Merged
merged 5 commits into from
Aug 11, 2023

Conversation

LartTyler
Copy link
Owner

@LartTyler LartTyler commented Aug 11, 2023

What's Changed

  • Projection query results now differentiate between explicit and implicit results, meaning that it is possible to detect e.g. if a path was denied because the projection was an allow-list and the path wasn't found in the list. See Projection Examples for more details.

Projection Examples

The Projection class now includes a suite of methods for testing exactly why a path did or did not pass the projection.

Starting with the most basic of the methods, query(), which returns an integer describing the result of the query. The integer is a bitmask (see QueryResult) representing the states a result can take.

use DaybreakStudios\DoctrineQueryDocument\Projection\Projection;
use DaybreakStudios\DoctrineQueryDocument\Projection\QueryResult;

// ===== Allow List =====
$projection = Projection::fromFields([
    'a.b.c' => true,
]);

// Our projection is an allow-list, and the path "a.b.c" is part of the list, so
// it is both `allowed` and `explicit`.
assert($projection->query('a.b.c') === QueryResult::ALLOW | QueryResult::IS_EXPLICIT);

// Our projection is an allow-list, and the path "1.2.3" is not part of the list, so
// it is only `denied`.
assert($projection->query('1.2.3') === QueryResult::DENY);

// ===== Deny List =====
$projection = Projection::fromFields([
    'a.b.c' => false,
]);

// Our projection is a deny-list, and the path "a.b.c" is part of the list, so
// it is both `denied` and `explicit`.
assert($projection->query('a.b.c') === QueryResult::DENY | QueryResult::IS_EXPLICIT);

// Our projection is a deny-list, and the path "1.2.3" is not part of the list, so
// it is only `allowed`.
assert($projection->query('1.2.3') === QueryResult::ALLOW);

In addition to the existing Projection::isAllowed() method, there are several new methods you can use to easily test the results of a query without using QueryResult directly.

use DaybreakStudios\DoctrineQueryDocument\Projection\Projection;

$projection = Projection::fromFields([
    'a.b.c' => true,
]);

// An explicit allow is still an allow
assert($projection->isAllowed('a.b.c'));

// Explicitly allowed because the path is present in the allow-list
assert($projection->isAllowedExplicitly('a.b.c'));

// Denied by default because the path is not present in the allow-list
assert($projection->isDenied('1.2.3'));

// Not explicitly denied
assert($projection->isDeniedExplicitly('1.2.3') === false);

$projection = Projection::fromFields([
    '1.2.3' => false,
]);

// An explicit deny is still a deny
assert($projection->isDenied('1.2.3'));

// Explicitly denied because the path is present in the deny-list
assert($projection->isDeniedExplicitly('1.2.3'));

// Allowed by default because the path is not present in the deny-list
assert($projection->isAllowed('a.b.c'));

// Not explicitly allowed
assert($projection->isAllowedExplicitly('a.b.c') === false);

@LartTyler LartTyler marked this pull request as ready for review August 11, 2023 16:51
@LartTyler LartTyler merged commit 147d045 into master Aug 11, 2023
@LartTyler LartTyler deleted the feature/improve-projection branch August 11, 2023 18:22
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.

None yet

2 participants