1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- // Copyright 2019-2022 Hackware SpA <human@hackware.cl>
- // This file is part of "Hackware Web Services Wallet" and licensed under
- // the terms of the GNU Affero General Public License version 3, or (at your
- // option) a later version. You should have received a copy of this license
- // along with the software. If not, see <https://www.gnu.org/licenses/>.
- namespace Hawese\Wallet;
- use Hawese\Core\Exceptions\ModelObjectNotFoundException;
- use Illuminate\Database\Query\Builder;
- class Currency extends TableModel
- {
- public static $table = 'currencies';
- public static $attributes = [
- 'id' => ['nullable', 'integer', 'min:1'],
- 'code' => ['required', 'size:3'],
- 'rate' => ['required', 'regex:/^\d{1,6}(?:\.\d{1,6})?$/'],
- 'step' => ['required', 'regex:/^\d{1,4}(?:\.\d{1,4})?$/'],
- 'created_at' => ['nullable', 'date'],
- ];
- /**
- * Select only the latest element per currency `code`
- */
- public static function select(
- ?array $attributes = null,
- bool $withDeleted = false
- ): Builder {
- $ids = app('db')
- ->table(self::$table)
- ->select(app('db')->raw('MAX(id) as id'))
- ->groupBy('code')
- ->pluck('id');
- return parent::select($attributes)
- ->whereIn('id', $ids);
- }
- /**
- * Return only the latest object with this `code`
- */
- public static function findByCode($code) : self
- {
- $query = 'SELECT * FROM ' . self::$table . ' ' .
- 'WHERE code = ? ORDER BY id DESC LIMIT 1';
- $row = app('db')->selectOne($query, [$code]);
- if ($row) {
- return new self($row);
- }
- throw new ModelObjectNotFoundException(static::class, ['code'], $code);
- }
- public function update($fields = []) : bool
- {
- return false;
- }
- public function delete() : bool
- {
- return false;
- }
- }
|