Sources
This commit is contained in:
@ -79,4 +79,10 @@ class Currency extends Model {
|
||||
$result = Value::add($this->factory, $arr);
|
||||
return $result;
|
||||
}
|
||||
public function addSource($info) {
|
||||
$arr = (array) $info;
|
||||
$arr['currency_id'] = (int) $this->id;
|
||||
$result = Source::add($this->factory, $arr);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ use Carbon\CarbonInterval;
|
||||
use ProVM\Common\Alias\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property Currency $currency_id
|
||||
* @property string $url
|
||||
* @property \DateInterval $frecuency
|
||||
@ -24,4 +23,42 @@ class Source extends Model {
|
||||
}
|
||||
$this->frecuency = CarbonInterval::getDateIntervalSpec($frecuency);
|
||||
}
|
||||
|
||||
protected static $fields = ['currency_id', 'url', 'frecuency'];
|
||||
public static function add(ModelFactory $factory, $info) {
|
||||
$input = array_intersect_key((array) $info, array_combine(self::$fields, self::$fields));
|
||||
$source = $factory->find(Source::class)->where([['currency_id', $input['currency_id']], ['url', $input['url']]])->one();
|
||||
$created = false;
|
||||
$result = (object) compact('input', 'source', 'created');
|
||||
if (!$value) {
|
||||
$source = $factory->create(Source::class, $input);
|
||||
$created = $source->save();
|
||||
$result->created = $created;
|
||||
}
|
||||
$result->value = $source->asArray();
|
||||
return $result;
|
||||
}
|
||||
public function edit($info): bool {
|
||||
$data = array_intersect_key((array) $info, array_combine(self::$fields, self::$fields));
|
||||
$edited = false;
|
||||
foreach ($data as $field => $value) {
|
||||
if ($this->{$field} != $value) {
|
||||
if ($field == 'currency_id' or $field == 'url') {
|
||||
continue;
|
||||
}
|
||||
$this->{$field} = $value;
|
||||
$edited = true;
|
||||
}
|
||||
}
|
||||
if ($edited) {
|
||||
$edited = $this->save();
|
||||
}
|
||||
return $edited;
|
||||
}
|
||||
public function asArray(): array {
|
||||
$output = parent::asArray();
|
||||
$output['currency'] = $this->currency()->asArray();
|
||||
$output['frecuency'] = $this->frecuency()->format('Y-m-d H:i:s');
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user