Skip to content

Commit 6b01483

Browse files
committed
Added array filter processor.
1 parent 1a22e3d commit 6b01483

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

src/Gwt/Processor/ArrayFilter.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
/**
3+
* PHP class for filtering array data from GWT Google Webmaster Tools.
4+
*
5+
* This class does NOT require the Zend gdata package be installed
6+
* in order to run.
7+
*
8+
* Copyright 2012 eyecatchUp UG. All Rights Reserved.
9+
*
10+
* Licensed under the Apache License, Version 2.0 (the "License");
11+
* you may not use this file except in compliance with the License.
12+
* You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing, software
17+
* distributed under the License is distributed on an "AS IS" BASIS,
18+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
* See the License for the specific language governing permissions and
20+
* limitations under the License.
21+
*
22+
* @author Anton Kolenkov <dorantor@gmail.com>
23+
* @author Stephan Schmitz <eyecatchup@gmail.com>
24+
* @link https://github.com/dorantor/php-webmaster-tools-downloads/
25+
* @link https://github.com/eyecatchup/php-webmaster-tools-downloads/
26+
*/
27+
28+
/**
29+
* Class for filtering array data from GWT
30+
*/
31+
class Gwt_Processor_ArrayFilter
32+
extends Gwt_Processor_ProcessorAbstract
33+
{
34+
/**
35+
* Column names that should be removed from data
36+
*
37+
* @var array
38+
*/
39+
private $_columnNamesToRemove = array();
40+
41+
/**
42+
* Explicitly set column keys to remove
43+
*
44+
* @var array
45+
*/
46+
private $_columnKeysToRemove = array();
47+
48+
/**
49+
* Process data
50+
*
51+
* @param mixed $data
52+
* @param string $tableName
53+
* @return mixed
54+
*/
55+
public function process($data, $tableName)
56+
{
57+
list($fieldNames, $result) = $data;
58+
59+
$columnKeys = $this->getColumnsKeysToRemove($fieldNames);
60+
61+
foreach ($columnKeys as $key) {
62+
unset($fieldNames[$key]);
63+
}
64+
65+
foreach ($result as &$row) {
66+
foreach ($columnKeys as $key) {
67+
unset($row[$key]);
68+
}
69+
}
70+
71+
return array($fieldNames, $result);
72+
}
73+
74+
/**
75+
* Get list of column keys to remove
76+
*
77+
* @param array $fieldNames
78+
* @return array
79+
*/
80+
protected function getColumnsKeysToRemove(array $fieldNames)
81+
{
82+
$result = array();
83+
foreach ($fieldNames as $key => $value) {
84+
if (in_array($value, $this->getColumnNamesToRemove())) {
85+
$result[] = $key;
86+
}
87+
}
88+
89+
return array_unique(
90+
array_merge(
91+
$result,
92+
$this->_columnKeysToRemove
93+
)
94+
);
95+
}
96+
97+
/**
98+
* Set column keys to remove
99+
*
100+
* @param array $keys
101+
* @return $this
102+
*/
103+
public function setColumnKeysToRemove(array $keys)
104+
{
105+
$this->_columnKeysToRemove = $keys;
106+
107+
return $this;
108+
}
109+
110+
/**
111+
* Set columns names which should be removed
112+
*
113+
* @param array $names
114+
* @return $this
115+
*/
116+
public function setColumnNamesToRemove(array $names)
117+
{
118+
$this->_columnNamesToRemove = $names;
119+
120+
return $this;
121+
}
122+
123+
/**
124+
* Get list of column names to remove
125+
*
126+
* @return array
127+
*/
128+
public function getColumnNamesToRemove()
129+
{
130+
return $this->_columnNamesToRemove;
131+
}
132+
}

0 commit comments

Comments
 (0)