Initial commit
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace App\Logic\Dividends;
|
||||
|
||||
use App\Helper\Currency;
|
||||
|
||||
class SheetData
|
||||
{
|
||||
private array $rawData;
|
||||
// eg. 0 -> name, 1 -> ISIN
|
||||
private array $dataIndex;
|
||||
private array $data;
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
$this->rawData = $data;
|
||||
}
|
||||
|
||||
public function get() : array
|
||||
{
|
||||
if (isset($this->data)) {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
$needIndex = [];
|
||||
foreach ($this->rawData as $index => $rawDatum) {
|
||||
if ($index === 0) {
|
||||
$this->dataIndex = $rawDatum;
|
||||
continue;
|
||||
}
|
||||
$setDatas=[];
|
||||
foreach ($rawDatum as $i => $data) {
|
||||
$dataType = $this->getDataType($data,$setDatas);
|
||||
if ($dataType){
|
||||
$needIndex[$i] = $dataType;
|
||||
}
|
||||
}
|
||||
unset($setDatas);
|
||||
break;
|
||||
}
|
||||
$ret = [];
|
||||
foreach ($this->rawData as $index => $datum) {
|
||||
if ($index === 0) {
|
||||
continue;
|
||||
}
|
||||
$row=[];
|
||||
foreach ($datum as $i => $d) {
|
||||
if (isset($needIndex[$i])) {
|
||||
$row[$needIndex[$i]] = $d;
|
||||
}
|
||||
}
|
||||
$dividendObj = new DividendsData();
|
||||
$dividendObj->setCount($row['count']);
|
||||
$dividendObj->setIsin($row['isin']);
|
||||
$dividendObj->setCurrency($row['currency']);
|
||||
$dividendObj->setTax($row['tax']);
|
||||
$dividendObj->setValue($row['value']);
|
||||
$dividendObj->setDateTime(new \DateTime(str_replace(".","-",$row['date'])));
|
||||
$ret[] = $dividendObj;
|
||||
}
|
||||
$this->data = $ret;
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public function getByCountry() : array
|
||||
{
|
||||
$data = $this->get();
|
||||
if (!$data) {
|
||||
return [];
|
||||
}
|
||||
$newArr = [];
|
||||
/** @var DividendsData $datum */
|
||||
foreach ($data as $datum) {
|
||||
$key = mb_substr($datum->getIsin(), 0, 2);
|
||||
$newArr[$key][] = $datum;
|
||||
}
|
||||
|
||||
return $newArr;
|
||||
}
|
||||
|
||||
public function takeColumnNames() : array
|
||||
{
|
||||
return $this->dataIndex;
|
||||
}
|
||||
|
||||
private function getDataType($data,&$setDatas) : ?string
|
||||
{
|
||||
if (is_int($data)){
|
||||
$ret = "count";
|
||||
$count = &$setDatas['int'];
|
||||
if ($count===1){
|
||||
$ret = "tax";
|
||||
}
|
||||
if ($count > 1) {
|
||||
$ret = null;
|
||||
}
|
||||
$count++;
|
||||
return $ret;
|
||||
}
|
||||
if (is_float($data)) {
|
||||
$ret = "value";
|
||||
$count = &$setDatas['float'];
|
||||
if ($count > 0) {
|
||||
$ret = null;
|
||||
}
|
||||
$count++;
|
||||
return $ret;
|
||||
}
|
||||
if (in_array(mb_substr($data, 0, 2), ['CA', 'US', 'IE', 'GB']) && intval(mb_substr($data, 2, 1))>0) {
|
||||
$count = &$setDatas['data'];
|
||||
$ret = "isin";
|
||||
if ($count > 0) {
|
||||
$ret = null;
|
||||
}
|
||||
$count++;
|
||||
return $ret;
|
||||
}
|
||||
if (in_array($data, Currency::$currencies)) {
|
||||
return "currency";
|
||||
}
|
||||
if (count(explode(".", $data)) === 3) {
|
||||
return "date";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user