Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace GGrach\BitrixDebugger\Configurator;

use \GGrach\BitrixDebugger\Contract\ShowModableContract;
use \GGrach\BitrixDebugger\Contract\IShowModable;

/**
* Description of DebugConfigurator
*
* @author ggrachdev
*/
class DebuggerConfigurator implements ShowModableContract {
class DebuggerConfigurator implements IShowModable {

/**
* Путь до лог файлов разного уровня
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @author ggrachdev
*/
interface ShowModableContract {
interface IShowModable {

/**
* Получить режимы отображения установленные
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use GGrach\BitrixDebugger\Configurator\DebuggerConfigurator;
use GGrach\BitrixDebugger\Configurator\DebugBarConfigurator;
use GGrach\Filtrator\FiltratorContract;
use GGrach\Filtrator\IFiltrator;
use GGrach\Filtrator\Filtrator;

/**
* Ответственность: создание полноценного объекта, который позволит осуществлять все возможные операции через текучий интерфейс
* Создание полноценного объекта, который позволит осуществлять все возможные операции через текучий интерфейс
*
* @author ggrachdev
*/
Expand All @@ -29,7 +29,7 @@ public function __construct($debuggerConfigurator = null, $debugBarConfigurator

if ($filtrator === null) {
$this->setFiltrator(new Filtrator());
} elseif ($filtrator instanceof FiltratorContract) {
} elseif ($filtrator instanceof IFiltrator) {
$this->setFiltrator($filtrator);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GGrach\BitrixDebugger\Debugger;

use GGrach\Filtrator\FiltratorContract;
use GGrach\Filtrator\IFiltrator;

/**
* Ответственность: фильтрация входящих данных для дебага
Expand All @@ -12,7 +12,7 @@
class FilterDebugger extends ConfigurationDebugger {

/**
* @var FiltratorContract
* @var IFiltrator
*/
protected $filtrator;

Expand All @@ -31,7 +31,7 @@ class FilterDebugger extends ConfigurationDebugger {
*/
public function __call($name, $arguments) {

if($this->getFiltrator()->hasCustomFilter($name))
if($this->getFiltrator()->hasFilter($name))
{
$this->getFiltrator()->addFilter($name, $arguments);
}
Expand All @@ -43,16 +43,16 @@ public function __call($name, $arguments) {
return $this;
}

public function getFiltrator(): FiltratorContract {
public function getFiltrator(): IFiltrator {
return $this->filtrator;
}

public function addFilter(string $nameMethod, callable $callback): self {
$this->getFiltrator()->addCustomFilter($nameMethod, $callback);
$this->getFiltrator()->addFilterRule($nameMethod, $callback);
return $this;
}

public function setFiltrator(FiltratorContract $filtrator): self {
public function setFiltrator(IFiltrator $filtrator): self {
$this->filtrator = $filtrator;
return $this;
}
Expand Down Expand Up @@ -82,28 +82,4 @@ public function filtrateItem($itemData) {
return $this->getFiltrator()->filtrate($itemData);
}

public function first(): self {
$this->getFiltrator()->addFilter('first');
return $this;
}

public function last(): self {
$this->getFiltrator()->addFilter('last');
return $this;
}

public function keys(array $availableKeys = []): self {
$this->getFiltrator()->addFilter('keys', [
'keys' => $availableKeys
]);
return $this;
}

public function limit(int $limit = 10): self {
$this->getFiltrator()->addFilter('limit', [
'count' => $limit
]);
return $this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GGrach\BitrixDebugger\Validator;

use GGrach\BitrixDebugger\Contract\ShowModableContract;
use GGrach\BitrixDebugger\Contract\IShowModable;

/**
* Проверка допустимых действий для режимов отображения
Expand All @@ -11,11 +11,11 @@
*/
class ShowModeDebuggerValidator {

public static function needShowInDebugBar(ShowModableContract $showModable) {
public static function needShowInDebugBar(IShowModable $showModable) {
return in_array('debug_bar', $showModable->getShowModes());
}

public static function needShowInCode(ShowModableContract $showModable) {
public static function needShowInCode(IShowModable $showModable) {
global $USER;
return in_array('code', $showModable->getShowModes()) && \is_object($USER) && $USER->IsAdmin();
}
Expand Down
84 changes: 16 additions & 68 deletions ggrachdev.debugbar/classes/general/Filtrator/Filtrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

namespace GGrach\Filtrator;

use \GGrach\Filtrator\FiltratorContract;
use \GGrach\Filtrator\IFiltrator;

/**
* @author ggrachdev
*/
class Filtrator implements FiltratorContract {

public const FILTERS_NAME = [
'limit', 'first', 'last', 'keys'
];
class Filtrator implements IFiltrator {

private $customFilters = [];

Expand All @@ -22,91 +18,43 @@ class Filtrator implements FiltratorContract {
*/
protected $sequenceFilters;

public function addFilter(string $filterType, array $filterParams = []): void {
if ($this->hasFilter($filterType) || $this->hasCustomFilter($filterType)) {
$this->sequenceFilters[] = [
'type' => $filterType,
'params' => $filterParams
];
}
}

public function filtrateItem(string $filterType, array $filterParams, $data) {
switch ($filterType) {
case 'limit':
if (\is_array($data) && !empty($data)) {
if (empty($filterParams['count']) || $filterParams['count'] < 1) {
$filterParams['count'] = 10;
}
$data = array_slice($data, 0, $filterParams['count'], true);
}
break;
case 'first':
if (\is_array($data) && !empty($data)) {
$data = array_shift($data);
}
break;
case 'keys':
if (\is_array($data) && !empty($data) && !empty($filterParams['keys'])) {
$newData = [];

foreach ($data as $k => $v) {
if (\in_array($k, $filterParams['keys'])) {
$newData[$k] = $v;
}
}

$data = $newData;
}
break;
case 'last':
if (\is_array($data) && !empty($data)) {
$data = array_pop($data);
}
break;
}

return $data;
}

public function clearFilters(): void {
$this->sequenceFilters = [];
}

public function filtrate($data) {
if (!empty($this->sequenceFilters) && !empty($data)) {
foreach ($this->sequenceFilters as $arFilter) {
if($this->hasCustomFilter($arFilter['type']))
{
$data = $this->customFiltrateItem($arFilter['type'], $arFilter['params'], $data);
}
else {
$data = $this->filtrateItem($arFilter['type'], $arFilter['params'], $data);
}
$data = $this->filtrateItem($arFilter['type'], $arFilter['params'], $data);
}
}

return $data;
}

public function addFilter(string $filterType, array $filterParams = []): void {
if ($this->hasFilter($filterType)) {
$this->sequenceFilters[] = [
'type' => $filterType,
'params' => $filterParams
];
}
}

public function hasFilter(string $filterType): bool {
return \in_array($filterType, self::FILTERS_NAME);
return \array_key_exists($filterType, $this->customFilters);
}

public function addCustomFilter(string $filterName, callable $callback) {
if (!$this->hasCustomFilter($filterName)) {
public function addFilterRule(string $filterName, callable $callback): IFiltrator {
if (!$this->hasFilter($filterName)) {
$this->customFilters[$filterName] = $callback;
}

return $this;
}

public function customFiltrateItem(string $filterType, array $filterParams, $data) {
public function filtrateItem(string $filterType, array $filterParams, $data) {
return $this->customFilters[$filterType]($data, $filterParams);
}

public function hasCustomFilter(string $filterName) {
return \array_key_exists($filterName, $this->customFilters);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@

namespace GGrach\Filtrator;

/**
*
* @author ggrachdev
*/
interface FiltratorContract {
interface IFiltrator {
public function filtrate($data);

public function addCustomFilter(string $filterName, callable $callback);
public function hasFilter(string $filterType): bool;

public function hasCustomFilter(string $filterName);
public function addFilterRule(string $filterName, callable $callback): self;

public function customFiltrateItem(string $filterType, array $filterParams, $data);
public function addFilter(string $filterType, array $filterParams = []): void;

public function filtrateItem(string $filterType, array $filterParams, $data);

public function addFilter(string $filterType, array $filterParams): void;

public function hasFilter(string $filterType): bool;

public function clearFilters(): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @author ggrachdev
*/
interface WritableContract {
interface IWritable {

/**
* Запись данных в ресурс
Expand Down
4 changes: 2 additions & 2 deletions ggrachdev.debugbar/classes/general/Writer/FileWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace GGrach\Writer;

use GGrach\Writer\Contract\WritableContract;
use GGrach\Writer\Contract\IWritable;

/**
* Запись данных в файл
*
* @author ggrachdev
*/
class FileWriter implements WritableContract {
class FileWriter implements IWritable {

/**
* Запись данных в ресурс
Expand Down
60 changes: 60 additions & 0 deletions ggrachdev.debugbar/filters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

DD()->addFilter('values', function ($data, $filterParams) {
if (\is_array($data)) {
return \array_values($data);
} else {
return $data;
}
});

DD()->addFilter('limit', function ($data, $filterParams) {
if (\is_array($data) && !empty($data)) {
if (empty($filterParams[0]) || !\is_numeric($filterParams[0]) || $filterParams[0] < 1) {
$count = 10;
} else {
$count = $filterParams[0];
}
$data = array_slice($data, 0, $count, true);
}

return $data;
});

DD()->addFilter('first', function ($data, $filterParams) {
if (\is_array($data) && !empty($data)) {
$data = array_shift($data);
}

return $data;
});

DD()->addFilter('keys', function ($data, $filterParams) {

if (
!empty($data) &&
is_array($data) &&
!empty($filterParams[0]) &&
\is_array($filterParams[0])
) {
$newData = [];

foreach ($data as $k => $v) {
if (\in_array($k, $filterParams[0])) {
$newData[$k] = $v;
}
}

$data = $newData;
}

return $data;
});

DD()->addFilter('last', function ($data, $filterParams) {
if (\is_array($data) && !empty($data)) {
$data = array_pop($data);
}

return $data;
});
Loading