<?php
namespace App\Utils\BaseParser\Utils\Extractor; abstract class AbstractFindResponse { protected $count = null; protected $rawMatches = null; public function __construct($count, $rawMatches) { $this->count = $count; $this->rawMatches = $rawMatches; } public function __get($key) { return $this->get($key); } abstract function get($key); abstract public function getOffset($key); public function cleanup() { $this->rawMatches = $this->getCleanedUp(); return $this; } public function mapKeyValue($callback) { $remappedMatches = []; foreach ($this->rawMatches as $k=>$v) { $remappedMatches[]= [ 'key' => $k, 'value' => $v[0], 'offset' => $v[1] ]; } $remappedMatches = array_map($callback, $remappedMatches); $this->rawMatches = []; foreach ($remappedMatches as $rkv) { if ($rkv===null) { continue; } foreach ($rkv as $kv) { $this->rawMatches[$kv['key']] = [$kv['value'], $kv['offset'], ]; } } return $this; } private function getCleanedUp() { $rawMatches = $this->rawMatches; $rawMatches = array_diff_key($rawMatches, range(1, count($rawMatches))); $rawMatches = array_filter($rawMatches); return $rawMatches; } public function getRawMatches() { return $this->rawMatches; } public function getRawMatchedStrings() { return array_map(function($a) { return $a[0]; }, $this->getRawMatches()); } public function hasValue() { return $this->count!==0; } public function getCount() { return $this->count; } public function getRealCount() { return count($this->getCleanedUp()); } } 