<?php
namespace Medienreaktor\Contact\Domain\Repository;


/***
 *
 * This file is part of the "Contact Persons" Extension for TYPO3 CMS.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 *  (c) 2019 Daniel Kestler <info@medienreaktor.de>, medienreaktor GmbH
 *
 ***/
/**
 * The repository for Persons
 */
class PersonRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
{

    /**
     * @var array
     */
    protected $defaultOrderings = [
    'sorting' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
];

    /**
     * @param array $categories
     */
    public function findByCategories(array $categories)
    {
        $query = $this->createQuery();
        $categoryConstraints = [];

        // Create constraints for each category
        foreach ($categories as $category) {
            $categoryConstraints[] = $query->contains('categories', $category);
        }

        // Ensure constraints are provided and use spread operator
        if (!empty($categoryConstraints)) {
            $query->matching(
                $query->logicalOr(...$categoryConstraints) // Spread operator to unpack the array
            );
        }
        return $query->execute();
    }
}
