Skip to content

Commit

Permalink
Fixed the issues reported by phpcs, formated the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Miu committed Apr 12, 2014
1 parent a2bfd12 commit 62481a2
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 92 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ php:
before_script:
- composer self-update
- composer install --prefer-source --dev

script:
- mkdir -p build/logs
- cd tests
- phpunit
- cd ../

after_script:
- php vendor/bin/coveralls -v
12 changes: 12 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="phpunit_bootstrap.php" colors="false">
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="coverage-html" target="build/coverage/"/>
</logging>
<testsuites>
<testsuite name="Sirius Filtrator Test Suite">
<directory>./tests/src/</directory>
</testsuite>
</testsuites>
</phpunit>
5 changes: 5 additions & 0 deletions phpunit_bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

require_once('autoload.php');

error_reporting(E_ALL);
12 changes: 6 additions & 6 deletions src/Filter/AbstractFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ function getUniqueId()
*
* The options are also be passed to the error message.
*
* @param string $name
* @param mixed $value
* @return \Sirius\Validation\Validator\AbstractValidator
* @param string $name
* @param mixed $value
* @return self
*/
function setOption($name, $value)
{
Expand All @@ -53,8 +53,8 @@ function setOption($name, $value)
* For example, when you need to validate an email field matches another email field,
* to confirm the email address
*
* @param array|object $context
* @return \Sirius\Validation\Validator\AbstractValidator
* @param array|object $context
* @return self
*/
function setContext($context)
{
Expand All @@ -77,4 +77,4 @@ function filter($value, $valueIdentifier = null)
}

abstract function filterSingle($value, $valueIdentifier = null);
}
}
16 changes: 7 additions & 9 deletions src/Filter/Truncate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ function filterSingle($value, $valueIdentifier = null)
if (! is_string($value)) {
return $value;
}

if (! $this->options[self::OPTION_LIMIT] || strlen($value) <= $this->options[self::OPTION_LIMIT]) {
return $value;
}

$truncated = $value;


$limit = $this->options[self::OPTION_LIMIT];
$firstSpace = strpos($value, ' ');

// in case word breaking is not allowed find the previous space
if (!$this->options[self::OPTION_BREAK_WORDS]) {
if ($firstSpace === false) {
Expand All @@ -46,14 +44,14 @@ function filterSingle($value, $valueIdentifier = null)
&& substr($value, $limit + 1, 1) != ' ') {
$isWordBreaker = true;
}

$truncated = rtrim(substr($value, 0, $limit));

if ($this->options[self::OPTION_ELLIPSIS]
&& $truncated != $value) {
$truncated .= $this->options[self::OPTION_ELLIPSIS];
}

return $truncated;
}
}
}
27 changes: 15 additions & 12 deletions src/FilterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Sirius\Filtration;

use Sirius\Filtration\Filter\AbstractFilter;
use Sirius\Filtration\Filter\Callback;

class FilterFactory {

protected $filtersMap = array(
'callback' => '\Sirius\Filtration\Filter\Callback',
'censor' => '\Sirius\Filtration\Filter\Censor',
Expand All @@ -26,16 +29,16 @@ function registerFilter($name, $class)
}
return $this;
}


/**
* Factory method to create a filter from various options
*
* @param callable|class|filter $callbackOrFilterName
* @param string|array $options
* @param bool $resursive
* @param callable|string $callbackOrFilterName
* @param string|array $options
* @param bool $resursive
* @throws \InvalidArgumentException
* @return \Sirius\Filtration\Filter\AbstractFilter
* @return AbstractFilter
*/
function createFilter($callbackOrFilterName, $options = null, $resursive = false)
{
Expand All @@ -50,13 +53,13 @@ function createFilter($callbackOrFilterName, $options = null, $resursive = false
} elseif (! $options) {
$options = array();
}

if (! is_array($options)) {
throw new \InvalidArgumentException('Validator options should be an array, JSON string or query string');
}

if (is_callable($callbackOrFilterName)) {
$filter = new \Sirius\Filtration\Filter\Callback(array(
$filter = new Callback(array(
'callback' => $callbackOrFilterName,
'arguments' => $options
), $resursive);
Expand All @@ -73,7 +76,7 @@ function createFilter($callbackOrFilterName, $options = null, $resursive = false
} else {
throw new \InvalidArgumentException(sprintf('Impossible to determine the filter based on the name %s', (string) $callbackOrFilterName));
}
} elseif (is_object($callbackOrFilterName) && $callbackOrFilterName instanceof \Sirius\Filtration\Filter\AbstractFilter) {
} elseif (is_object($callbackOrFilterName) && $callbackOrFilterName instanceof AbstractFilter) {
$filter = $callbackOrFilterName;
}
if (! isset($filter)) {
Expand All @@ -82,4 +85,4 @@ function createFilter($callbackOrFilterName, $options = null, $resursive = false
return $filter;
}

}
}
29 changes: 17 additions & 12 deletions src/FilterSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
class FilterSet extends \SplPriorityQueue{
/**
* Cache of priorities that were already taken
*
*
* @var array
*/
protected $allocatedPriorities = array();

function __construct() {
$this->setExtractFlags(static::EXTR_BOTH);
}

function compare($priority1, $priority2) {
if ($priority1 === $priority2) return 0;
return $priority1 < $priority2 ? 1 : -1;
}

function insert($filter, $priority) {
if (!$filter instanceof Filter\AbstractFilter) {
throw new \InvalidArgumentException('Only filter instances can be added to the filter set');
Expand All @@ -31,24 +31,28 @@ function insert($filter, $priority) {
}
// use `clone` because iterating over Priority Queues removes elements from the queue
foreach (clone $this as $v) {
/* @var $v \Sirius\Filtration\Filter\AbstractFilter */
if ($v->getUniqueId() === $filter->getUniqueId()) {
return;
}
}
array_push($this->allocatedPriorities, $priority);
return parent::insert($filter, $priority);
}

function remove($filter) {
/* @var $filter \Sirius\Filtration\Filter\AbstractFilter */
if (!$filter instanceof Filter\AbstractFilter) {
throw new \InvalidArgumentException('Only filter instances can be removed from the filter set');
}
$filters = array();
$this->top();
while($this->valid()) {
$node = $this->current();
if ($node['data']->getUniqueId() !== $filter->getUniqueId()) {
$filters[$node['priority']] = $node['data'];
$item = $this->current();
/* @var $itemFilter \Sirius\Filtration\Filter\AbstractFilter */
$itemFilter = $item['data'];
if ($itemFilter->getUniqueId() !== $filter->getUniqueId()) {
$filters[$item['priority']] = $item['data'];
}
$this->next();
}
Expand All @@ -57,7 +61,7 @@ function remove($filter) {
}
return $this;
}

/**
* Get a valid priority number to attach to a filter
*
Expand All @@ -82,13 +86,14 @@ protected function getValidPriority($desiredPriority)
}
return $desiredPriority;
}

function applyFilters($value, $valueIdentifier = null, $context = null) {
foreach (clone $this as $filter) {
/* @var $filter \Sirius\Filtration\Filter\AbstractFilter */
$filter->setContext($context);
$value = $filter->filter($value, $valueIdentifier);
}
return $value;
}
}

}
2 changes: 1 addition & 1 deletion src/FiltratableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sirius\Filtration;

use Sirius\Filtration\Filtrator;


trait FiltratableTrait {
protected $filtrator;
Expand Down

0 comments on commit 62481a2

Please sign in to comment.