<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of the Superdesk Web Publisher Core Bundle.
 *
 * Copyright 2016 Sourcefabric z.ú. and contributors.
 *
 * For the full copyright and license information, please see the
 * AUTHORS and LICENSE files distributed with this source code.
 *
 * @copyright 2016 Sourcefabric z.ú
 * @license http://www.superdesk.org/license
 */

namespace SWP\Bundle\CoreBundle\Matcher;

use SWP\Bundle\ContentBundle\Model\ArticleInterface;
use SWP\Component\Common\Criteria\Criteria;

final class ArticleCriteriaMatcher implements ArticleCriteriaMatcherInterface
{
    /**
     * {@inheritdoc}
     */
    public function match(ArticleInterface $article, Criteria $criteria)
    {
        if (0 === $criteria->count()) {
            return false;
        }

        if ($criteria->has('route')) {
            foreach ($criteria->get('route') as $value) {
                if (null !== $article->getRoute() && (int) $value !== $article->getRoute()->getId()) {
                    return false;
                }
            }
        }

        if ($criteria->has('author') && is_array($criteria->get('author'))) {
            foreach ($criteria->get('author') as $key => $value) {
                if ($value !== $article->getMetadataByKey('byline')) {
                    return false;
                }
            }
        }

        if ($criteria->has('publishedBefore')) {
            $publishedBefore = new \DateTime($criteria->get('publishedBefore'));
            if ($article->getPublishedAt() > $publishedBefore) {
                return false;
            }
        }

        if ($criteria->has('publishedAfter')) {
            $publishedAfter = new \DateTime($criteria->get('publishedAfter'));
            if ($article->getPublishedAt() < $publishedAfter) {
                return false;
            }
        }

        if ($criteria->has('publishedAt')
            && (!$criteria->has('publishedBefore') || !$criteria->has('publishedAfter'))) {
            $publishedAt = new \DateTime($criteria->get('publishedAt'));
            if ($publishedAt->format('d-m-Y') !== $article->getPublishedAt()->format('d-m-Y')) {
                return false;
            }
        }

        if ($criteria->has('metadata')) {
            foreach ($criteria->get('metadata') as $key => $value) {
                if ($value !== $article->getMetadataByKey($key)) {
                    return false;
                }
            }
        }

        return true;
    }
}<commit_msg>do not allow empty metadata (#697)<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of the Superdesk Web Publisher Core Bundle.
 *
 * Copyright 2016 Sourcefabric z.ú. and contributors.
 *
 * For the full copyright and license information, please see the
 * AUTHORS and LICENSE files distributed with this source code.
 *
 * @copyright 2016 Sourcefabric z.ú
 * @license http://www.superdesk.org/license
 */

namespace SWP\Bundle\CoreBundle\Matcher;

use SWP\Bundle\ContentBundle\Model\ArticleInterface;
use SWP\Component\Common\Criteria\Criteria;

final class ArticleCriteriaMatcher implements ArticleCriteriaMatcherInterface
{
    /**
     * {@inheritdoc}
     */
    public function match(ArticleInterface $article, Criteria $criteria)
    {
        if (0 === $criteria->count()) {
            return false;
        }

        if ($criteria->has('route')) {
            foreach ($criteria->get('route') as $value) {
                if (null !== $article->getRoute() && (int) $value !== $article->getRoute()->getId()) {
                    return false;
                }
            }
        }

        if ($criteria->has('author') && is_array($criteria->get('author'))) {
            foreach ($criteria->get('author') as $key => $value) {
                if ($value !== $article->getMetadataByKey('byline')) {
                    return false;
                }
            }
        }

        if ($criteria->has('publishedBefore')) {
            $publishedBefore = new \DateTime($criteria->get('publishedBefore'));
            if ($article->getPublishedAt() > $publishedBefore) {
                return false;
            }
        }

        if ($criteria->has('publishedAfter')) {
            $publishedAfter = new \DateTime($criteria->get('publishedAfter'));
            if ($article->getPublishedAt() < $publishedAfter) {
                return false;
            }
        }

        if ($criteria->has('publishedAt')
            && (!$criteria->has('publishedBefore') || !$criteria->has('publishedAfter'))) {
            $publishedAt = new \DateTime($criteria->get('publishedAt'));
            if ($publishedAt->format('d-m-Y') !== $article->getPublishedAt()->format('d-m-Y')) {
                return false;
            }
        }

        if ($criteria->has('metadata') && empty($criteria->get('metadata'))) {
            return false;
        }

        foreach ((array) $criteria->get('metadata') as $key => $value) {
            if ($value !== $article->getMetadataByKey($key)) {
                return false;
            }
        }

        return true;
    }
}
{'fraction_non_alphanumeric': 0.15071163969584714, 'fraction_numerical': 0.004484304932735426, 'mean_word_length': 2.040901007705987, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2140986', 'n_tokens_mistral': 1405, 'n_tokens_neox': 1334, 'n_words': 289}