12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- // Copyright 2019 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 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) : 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]);
- return new self($row);
- }
- public function update($fields = []) : bool
- {
- return false;
- }
- public function delete() : bool
- {
- return false;
- }
- }
|