Skip to content

Commit

Permalink
verschiedene Filter hinzugefügt, andere AddOns können Filter via EP b…
Browse files Browse the repository at this point in the history
…ereitstellen
  • Loading branch information
tbaddade committed Feb 21, 2017
1 parent 5e719a5 commit a646146
Show file tree
Hide file tree
Showing 13 changed files with 356 additions and 16 deletions.
15 changes: 14 additions & 1 deletion boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,21 @@ function sprogarray(array $array, array $fields, $fallback_clang_id = 0, $separa
}



$filters = $this->getProperty('filter');
$filters = \rex_extension::registerPoint(new \rex_extension_point('SPROG_FILTER', $filters));

$registeredFilters = [];
if (count($filters) > 0) {
foreach ($filters as $filter) {
$instance = new $filter();
$registeredFilters[$instance->name()] = $instance;
}
}


if (!rex::isBackend()) {
\rex_extension::register('OUTPUT_FILTER', '\Sprog\Extension::replaceWildcards');
\rex_extension::register('OUTPUT_FILTER', '\Sprog\Extension::replaceWildcards', rex_extension::NORMAL, ['filters' => $registeredFilters]);
}

if (rex::isBackend() && rex::getUser()) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Sprog/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Extension
{
public static function replaceWildcards(\rex_extension_point $ep)
{
$ep->setSubject(Wildcard::parse($ep->getSubject()));
$ep->setSubject(Wildcard::parse($ep->getSubject(), null, $ep->getParams()));
}

public static function articleUpdated(\rex_extension_point $ep)
Expand Down
12 changes: 7 additions & 5 deletions lib/Sprog/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@
abstract class Filter
{
/**
* Provide the commands of the search.
* Returns the name of the filter.
*
* @return array
* @return string
*/
abstract function name();

/**
* Execute the filter
*
* @param Command $command
* @return Result
* @param string $value
* @param string $arguments
*
* @return string
*/
abstract function fire(Command $command);
abstract function fire($value, $arguments);
}
37 changes: 37 additions & 0 deletions lib/Sprog/Filter/Format.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* This file is part of the Sprog package.
*
* @author (c) Thomas Blum <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sprog\Filter;

use Sprog\Filter;

class Format extends Filter
{
/**
* {@inheritdoc}
*/
public function name()
{
return 'format';
}

/**
* {@inheritdoc}
*/
public function fire($value, $arguments)
{
if ($arguments == '') {
return $value;
}

return vsprintf($value, explode(',', $arguments));
}
}
44 changes: 44 additions & 0 deletions lib/Sprog/Filter/Limit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* This file is part of the Sprog package.
*
* @author (c) Thomas Blum <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sprog\Filter;

use Sprog\Filter;

class Limit extends Filter
{
/**
* {@inheritdoc}
*/
public function name()
{
return 'limit';
}

/**
* {@inheritdoc}
*/
public function fire($value, $arguments)
{
if ($arguments == '') {
return $value;
}

$parts = explode(',', $arguments);
$limit = (int)$parts[0];
$end = isset($parts[1]) ? $parts[1] : '';

if (mb_strwidth($value, 'UTF-8') <= $limit) {
return $value;
}
return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')) . $end;
}
}
33 changes: 33 additions & 0 deletions lib/Sprog/Filter/Lower.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* This file is part of the Sprog package.
*
* @author (c) Thomas Blum <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sprog\Filter;

use Sprog\Filter;

class Lower extends Filter
{
/**
* {@inheritdoc}
*/
public function name()
{
return 'lower';
}

/**
* {@inheritdoc}
*/
public function fire($value, $arguments)
{
return mb_strtolower($value, 'UTF-8');
}
}
33 changes: 33 additions & 0 deletions lib/Sprog/Filter/Markdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* This file is part of the Sprog package.
*
* @author (c) Thomas Blum <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sprog\Filter;

use Sprog\Filter;

class Markdown extends Filter
{
/**
* {@inheritdoc}
*/
public function name()
{
return 'markdown';
}

/**
* {@inheritdoc}
*/
public function fire($value, $arguments)
{
return \rex_markdown::factory()->parse($value);
}
}
33 changes: 33 additions & 0 deletions lib/Sprog/Filter/Raw.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* This file is part of the Sprog package.
*
* @author (c) Thomas Blum <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sprog\Filter;

use Sprog\Filter;

class Raw extends Filter
{
/**
* {@inheritdoc}
*/
public function name()
{
return 'raw';
}

/**
* {@inheritdoc}
*/
public function fire($value, $arguments)
{
return $value;
}
}
33 changes: 33 additions & 0 deletions lib/Sprog/Filter/Title.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* This file is part of the Sprog package.
*
* @author (c) Thomas Blum <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sprog\Filter;

use Sprog\Filter;

class Title extends Filter
{
/**
* {@inheritdoc}
*/
public function name()
{
return 'title';
}

/**
* {@inheritdoc}
*/
public function fire($value, $arguments)
{
return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
}
}
33 changes: 33 additions & 0 deletions lib/Sprog/Filter/Upper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* This file is part of the Sprog package.
*
* @author (c) Thomas Blum <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sprog\Filter;

use Sprog\Filter;

class Upper extends Filter
{
/**
* {@inheritdoc}
*/
public function name()
{
return 'upper';
}

/**
* {@inheritdoc}
*/
public function fire($value, $arguments)
{
return mb_strtoupper($value, 'UTF-8');
}
}
45 changes: 45 additions & 0 deletions lib/Sprog/Filter/Words.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* This file is part of the Sprog package.
*
* @author (c) Thomas Blum <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sprog\Filter;

use Sprog\Filter;

class Words extends Filter
{
/**
* {@inheritdoc}
*/
public function name()
{
return 'words';
}

/**
* {@inheritdoc}
*/
public function fire($value, $arguments)
{
if ($arguments == '') {
return $value;
}

$parts = explode(',', $arguments);
$words = (int)$parts[0];
$end = isset($parts[1]) ? $parts[1] : '';

preg_match('/^\s*+(?:\S++\s*+){1,' . $words . '}/u', $value, $matches);
if (!isset($matches[0]) || mb_strlen($value) === mb_strlen($matches[0])) {
return $value;
}
return rtrim($matches[0]) . $end;
}
}
Loading

0 comments on commit a646146

Please sign in to comment.