Skip to content

Commit

Permalink
Implemented IsPreview View Matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Oct 23, 2023
1 parent 37f1de0 commit acc4803
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/lib/MVC/Symfony/Matcher/ContentBased/IsPreview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Core\MVC\Symfony\Matcher\ContentBased;

use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
use Ibexa\Core\MVC\Symfony\Controller\Content\PreviewController;
use Ibexa\Core\MVC\Symfony\Matcher\ViewMatcherInterface;
use Ibexa\Core\MVC\Symfony\View\View;

/**
* @internal
*/
final class IsPreview implements ViewMatcherInterface
{
private bool $isPreview;

/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
*/
public function setMatchingConfig($matchingConfig): void
{
if (!is_bool($matchingConfig)) {
throw new InvalidArgumentException(
'$matchConfig',
sprintf(
'IsPreview matcher expects true or false value, got a value of %s type',
gettype($matchingConfig)
)
);
}

$this->isPreview = $matchingConfig;
}

public function match(View $view): bool
{
$isPreview = $view->hasParameter(PreviewController::PREVIEW_PARAMETER_NAME)
&& $view->getParameter(PreviewController::PREVIEW_PARAMETER_NAME);

return $this->isPreview === $isPreview;
}
}

0 comments on commit acc4803

Please sign in to comment.