mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
parent
d59882159d
commit
13f7dcb00b
6
Search/ParsingException.php
Normal file
6
Search/ParsingException.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
namespace Chill\MainBundle\Search;
|
||||
|
||||
class ParsingException extends \Exception
|
||||
{
|
||||
}
|
@ -31,7 +31,7 @@ namespace Chill\MainBundle\Search;
|
||||
*/
|
||||
interface SearchInterface #-> good name ?
|
||||
{
|
||||
/*
|
||||
/**
|
||||
* return the result in a html string. The string will be inclued (as raw)
|
||||
* into a global view.
|
||||
*
|
||||
@ -40,15 +40,15 @@ interface SearchInterface #-> good name ?
|
||||
* {{ result|raw }}
|
||||
* {% endfor %}
|
||||
*
|
||||
* @param string $pattern the string to search
|
||||
* @param array $terms the string to search
|
||||
* @param int $start the first result (for pagination)
|
||||
* @param int $limit the number of result (for pagination)
|
||||
* @param array $option the options, specific for each search
|
||||
* @return string, an HTML string
|
||||
*/
|
||||
public function renderResult($pattern, $start=0, $limit=50, array $options = array());
|
||||
public function renderResult($terms, $start=0, $limit=50, array $options = array());
|
||||
|
||||
/*
|
||||
/**
|
||||
* we may desactive the search interface by default. in this case,
|
||||
* the search will be launch and rendered only with "advanced search"
|
||||
*
|
||||
@ -58,14 +58,7 @@ interface SearchInterface #-> good name ?
|
||||
*/
|
||||
public function isActiveByDefault();
|
||||
|
||||
/*
|
||||
* a string used in advanced search to activate the search
|
||||
*
|
||||
* @return string a string which will be translated by twig
|
||||
*/
|
||||
public function getLabel();
|
||||
|
||||
/*
|
||||
/**
|
||||
* the order in which the results will appears in the global view
|
||||
*
|
||||
* (this may be eventually defined in config.yml)
|
||||
@ -73,4 +66,12 @@ interface SearchInterface #-> good name ?
|
||||
* @return int
|
||||
*/
|
||||
public function getOrder();
|
||||
|
||||
/**
|
||||
* indicate if the implementation supports the given domain
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function supports($domain);
|
||||
|
||||
}
|
||||
|
@ -39,6 +39,55 @@ class SearchProvider
|
||||
|
||||
return $this->searchServices;
|
||||
}
|
||||
|
||||
/**
|
||||
* parse the search string to extract domain and terms
|
||||
*
|
||||
* @param string $pattern
|
||||
* @return string[] an array where the keys are _domain, _default (residual terms) or term
|
||||
*/
|
||||
public function parse($pattern)
|
||||
{
|
||||
$terms = array();
|
||||
|
||||
$terms['_domain'] = $this->getDomain($pattern);
|
||||
|
||||
return $terms;
|
||||
}
|
||||
|
||||
private function getDomain($subject)
|
||||
{
|
||||
preg_match_all( '/@([a-z]+)/', $subject, $terms);
|
||||
|
||||
if (count($terms[0]) > 1) {
|
||||
throw new ParsingException('You should not have more than one domain');
|
||||
}
|
||||
|
||||
return isset($terms[1][0]) ? $terms[1][0] : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* search through services which supports domain and give
|
||||
* results as html string
|
||||
*
|
||||
* @param string $pattern
|
||||
* @param number $start
|
||||
* @param number $limit
|
||||
* @return array of html results
|
||||
*/
|
||||
public function getResults($pattern, $start = 0, $limit = 50)
|
||||
{
|
||||
$terms = $this->parse($pattern);
|
||||
$results = array();
|
||||
|
||||
foreach ($searchServices as $service) {
|
||||
if ($service->supports($terms['_domain'])) {
|
||||
$results[] = $service->renderResult($terms, $start, $limit);
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/*
|
||||
* return search services with a specific name, defined in service
|
||||
|
58
Tests/Search/SearchProviderTest.php
Normal file
58
Tests/Search/SearchProviderTest.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/*
|
||||
*
|
||||
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Test\Search;
|
||||
|
||||
use Chill\MainBundle\Search\SearchProvider;
|
||||
|
||||
|
||||
class SearchProviderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->search = new SearchProvider();
|
||||
}
|
||||
|
||||
public function testDomain()
|
||||
{
|
||||
$term = $this->p("@person is not my name");
|
||||
|
||||
$this->assertEquals('person', $term['_domain']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Chill\MainBundle\Search\ParsingException
|
||||
*/
|
||||
public function testMultipleDomainError()
|
||||
{
|
||||
$term = $this->p("@person @report");
|
||||
var_dump($term);
|
||||
}
|
||||
|
||||
/**
|
||||
* shortcut for executing parse method
|
||||
*
|
||||
* @param unknown $pattern
|
||||
* @return string[]
|
||||
*/
|
||||
private function p($pattern)
|
||||
{
|
||||
return $this->search->parse($pattern);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user