/**
  * Returns banners matching the given demand
  *
  * @param \DERHANSEN\SfBanners\Domain\Model\BannerDemand $demand The demand
  * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
  */
 public function findDemanded(BannerDemand $demand)
 {
     /* Override the default sorting for random mode. Must be called before
        createQuery() */
     if ($demand->getDisplayMode() == 'allRandom') {
         $this->defaultOrderings = array();
     }
     $query = $this->createQuery();
     $constraints = array();
     if ($demand->getStartingPoint() != 0) {
         $pidList = GeneralUtility::intExplode(',', $demand->getStartingPoint(), true);
         $constraints[] = $query->in('pid', $pidList);
     }
     if ($demand->getCategories() != 0) {
         $categoryConstraints = array();
         $categories = GeneralUtility::intExplode(',', $demand->getCategories(), true);
         foreach ($categories as $category) {
             $categoryConstraints[] = $query->contains('category', $category);
         }
         if (count($categoryConstraints) > 0) {
             $constraints[] = $query->logicalOr($categoryConstraints);
         }
     }
     $query->matching($query->logicalAnd($constraints));
     /* Get banners without respect to limitations */
     $unfilteredResult = $query->execute();
     if ($unfilteredResult->count() > 0) {
         $finalQuery = $this->getQueryWithLimitation($unfilteredResult, $demand);
         $result = $this->getResult($finalQuery, $demand);
     } else {
         $result = $unfilteredResult;
     }
     return $result;
 }
 /**
  * Test if categories can be set
  *
  * @test
  * @return void
  */
 public function categoriesCanBeSetTest()
 {
     $categories = '1,2,3,4';
     $this->fixture->setCategories($categories);
     $this->assertEquals($categories, $this->fixture->getCategories());
 }