|
| 1 | +<?php |
| 2 | +namespace TestDbAcle\Commands; |
| 3 | + |
| 4 | + |
| 5 | +class FilterArrayByPsvCommand implements \TestDbAcle\Commands\CommandInterface |
| 6 | +{ |
| 7 | + protected $parser; |
| 8 | + protected $filterQueue; |
| 9 | + |
| 10 | + protected $placeHolders; |
| 11 | + protected $sourcePsv; |
| 12 | + |
| 13 | + protected $expectedData; |
| 14 | + protected $actualData; |
| 15 | + protected $actualDataUnfiltered; |
| 16 | + |
| 17 | + public function __construct($sourcePsv, $actualData, $placeHolders = array()) |
| 18 | + { |
| 19 | + $this->placeHolders = $placeHolders; |
| 20 | + $this->sourcePsv = $sourcePsv; |
| 21 | + $this->actualDataUnfiltered = $actualData; |
| 22 | + |
| 23 | + } |
| 24 | + |
| 25 | + public function execute() |
| 26 | + { |
| 27 | + $actualData = array(); |
| 28 | + |
| 29 | + $parsedTree = new \TestDbAcle\Psv\PsvTree(); |
| 30 | + $parsedTree->addTable(new \TestDbAcle\Psv\Table\Table('default', $this->parser->parsePsv($this->sourcePsv))); |
| 31 | + |
| 32 | + $filteredParsedTree = $this->filterQueue->filterDataTree($parsedTree); |
| 33 | + |
| 34 | + $expectedData = $filteredParsedTree->getTable('default')->toArray(); |
| 35 | + |
| 36 | + if (count($expectedData) == 0){ |
| 37 | + $this->expectedData = $expectedData; |
| 38 | + $this->actualData = $this->actualDataUnfiltered; |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + $allowedColumns = array_keys($expectedData[0]); |
| 43 | + foreach ($this->actualDataUnfiltered as $row){ |
| 44 | + $actualData[] = array_intersect_key($row, array_flip($allowedColumns)); |
| 45 | + } |
| 46 | + $this->expectedData = $expectedData; |
| 47 | + $this->actualData = $actualData; |
| 48 | + } |
| 49 | + |
| 50 | + public function initialise(\TestDbAcle\ServiceLocator $serviceLocator) |
| 51 | + { |
| 52 | + $this->parser = $serviceLocator->get('parser'); |
| 53 | + |
| 54 | + $this->filterQueue = new \TestDbAcle\Filter\FilterQueue(); |
| 55 | + $this->filterQueue->addRowFilter(new \TestDbAcle\Filter\PlaceholderRowFilter($this->placeHolders)); |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @return array An associative array representing the PSV |
| 61 | + */ |
| 62 | + function getExpectedData() |
| 63 | + { |
| 64 | + return $this->expectedData; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @return array An associative array representing the table state as configured by the Psv |
| 69 | + */ |
| 70 | + function getActualData() |
| 71 | + { |
| 72 | + return $this->actualData; |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | +} |
0 commit comments