Commit 1319f074 authored by Karamel's avatar Karamel
Browse files

Add type and nullable for direct fields and throw 400 responses

Check them when merging data, update model definitions
parent 43159a02
......@@ -82,6 +82,13 @@ $filesToCheck = [
'src/lib/Model/TicketTax.php',
'src/lib/Model/CashSessionCatTax.php',
'src/lib/Model/Currency.php',
'src/lib/Model/Field/BoolField.php',
'src/lib/Model/Field/DateField.php',
'src/lib/Model/Field/EnumField.php',
'src/lib/Model/Field/Field.php',
'src/lib/Model/Field/FloatField.php',
'src/lib/Model/Field/IntField.php',
'src/lib/Model/Field/StringField.php',
'src/lib/System/DateUtils.php',
'src/lib/System/Thumbnailer.php',
'src/lib/System/Login.php',
......
......@@ -47,6 +47,12 @@ class InvalidFieldException extends \Exception implements PastequeException
const CSTR_DEFAULT_REQUIRED = 'DefaultRequired';
/** The field is an enum and the value is not within it. */
const CSTR_ENUM = 'Enum';
/** The value cannot be converted to float. */
const CSTR_FLOAT = 'Float';
/** The value cannot be converted to boolean. */
const CSTR_BOOL = 'Boolean';
/** The value cannot be converted to integer. */
const CSTR_INT = 'Integer';
const CSTR_INVALID_DATE = 'InvalidDate';
const CSTR_INVALID_DATERANGE = 'InvalidDateRange';
......
......@@ -21,6 +21,9 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\Model\Field\IntField;
use \Pasteque\Server\Model\Field\StringField;
/**
* A fiscal ticket archive. Archives are created from ArchiveRequests through
* ArchiveAPI.
......@@ -30,8 +33,9 @@ namespace Pasteque\Server\Model;
class Archive
{
protected static function getDirectFieldNames() {
return ['number', 'info', 'content_hash', 'content',
'signature'];
return [new IntField('number'), new StringField('info'),
new StringField('content_hash'), 'content',
new StringField('signature')];
}
protected static function getAssociationFieds() {
return [];
......
......@@ -21,6 +21,9 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\Model\Field\BoolField;
use \Pasteque\Server\Model\Field\DateField;
/**
* A request to create an archive. Archives are created in background to
* avoid http server execution limits. See ArchiveAPI to create requests
......@@ -32,7 +35,10 @@ namespace Pasteque\Server\Model;
class ArchiveRequest
{
protected static function getDirectFieldNames() {
return ['dateStart', 'dateStop', 'processing'];
return [
new DateField('dateStart'),
new DateField('dateStop'),
new BoolField('processing')];
}
protected static function getAssociationFieds() {
return [];
......
......@@ -20,6 +20,8 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\Model\Field\IntField;
use \Pasteque\Server\Model\Field\StringField;
use \Pasteque\Server\System\DAO\DoctrineMainModel;
/**
......@@ -32,7 +34,12 @@ use \Pasteque\Server\System\DAO\DoctrineMainModel;
class CashRegister extends DoctrineMainModel
{
protected static function getDirectFieldNames() {
return ['reference', 'label', 'nextTicketId', 'nextSessionId'];
return [
new StringField('reference'),
new StringField('label'),
new IntField('nextTicketId'),
new IntField('nextSessionId')
];
}
protected static function getAssociationFields() {
return [
......
......@@ -23,6 +23,10 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\CommonAPI\VersionAPI;
use \Pasteque\Server\Model\Field\BoolField;
use \Pasteque\Server\Model\Field\DateField;
use \Pasteque\Server\Model\Field\IntField;
use \Pasteque\Server\Model\Field\FloatField;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\DAO\DAOCondition;
use \Pasteque\Server\System\DAO\DoctrineMainModel;
......@@ -46,10 +50,21 @@ class CashSession extends DoctrineMainModel
const CLOSE_FYEAR = 2;
protected static function getDirectFieldNames() {
return ['sequence', 'continuous', 'openDate', 'closeDate',
'openCash', 'closeCash', 'expectedCash',
'ticketCount', 'custCount',
'cs', 'csPeriod', 'csFYear', 'csPerpetual'];
return [
new IntField('sequence'),
new BoolField('continuous'),
new DateField('openDate', ['nullable' => true]),
new DateField('closeDate', ['nullable' => true]),
new FloatField('openCash', ['nullable' => true]),
new FloatField('closeCash', ['nullable' => true]),
new FloatField('expectedCash', ['nullable' => true]),
new IntField('ticketCount', ['nullable' => true]),
new IntField('custCount', ['nullable' => true]),
new FloatField('cs', ['nullable' => true]),
new FloatField('csPeriod'),
new FloatField('csFYear'),
new FloatField('csPerpetual')
];
// 'closeType' (optional) imported on close but not exported nor stored.
}
protected static function getAssociationFields() {
......
......@@ -22,6 +22,8 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\Model\Field\FloatField;
use \Pasteque\Server\Model\Field\StringField;
use \Pasteque\Server\System\DAO\DAO;
use \Pasteque\Server\System\DAO\DoctrineEmbeddedModel;
......@@ -38,7 +40,11 @@ class CashSessionCat extends DoctrineEmbeddedModel
{
protected static function getDirectFieldNames() {
// Not associative to be able to delete empty categories.
return ['reference', 'label', 'amount'];
return [
new StringField('reference'),
new StringField('label'),
new FloatField('amount')
];
}
protected static function getAssociationFields() {
return [];
......
......@@ -22,6 +22,8 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\Model\Field\FloatField;
use \Pasteque\Server\Model\Field\StringField;
use \Pasteque\Server\System\DAO\DAO;
use \Pasteque\Server\System\DAO\DoctrineEmbeddedModel;
......@@ -38,7 +40,12 @@ class CashSessionCatTax extends DoctrineEmbeddedModel
{
protected static function getDirectFieldNames() {
// Not associative to be able to delete empty categories.
return ['reference', 'label', 'base', 'amount'];
return [
new StringField('reference'),
new StringField('label'),
new FloatField('base'),
new FloatField('amount')
];
}
protected static function getAssociationFields() {
return [
......
......@@ -22,6 +22,7 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\Model\Field\FloatField;
use \Pasteque\Server\System\DAO\DAO;
use \Pasteque\Server\System\DAO\DoctrineEmbeddedModel;
......@@ -37,7 +38,7 @@ use \Pasteque\Server\System\DAO\DoctrineEmbeddedModel;
class CashSessionCustBalance extends DoctrineEmbeddedModel
{
protected static function getDirectFieldNames() {
return ['balance'];
return [new FloatField('balance')];
}
protected static function getAssociationFields() {
return [
......
......@@ -24,6 +24,7 @@ namespace Pasteque\Server\Model;
use \Pasteque\Server\Exception\RecordNotFoundException;
use \Pasteque\Server\Model\PaymentMode;
use \Pasteque\Server\Model\Field\FloatField;
use \Pasteque\Server\System\DAO\DAO;
use \Pasteque\Server\System\DAO\DAOCondition;
use \Pasteque\Server\System\DAO\DoctrineEmbeddedModel;
......@@ -38,7 +39,7 @@ use \Pasteque\Server\System\DAO\DoctrineEmbeddedModel;
class CashSessionPayment extends DoctrineEmbeddedModel
{
protected static function getDirectFieldNames() {
return ['amount', 'currencyAmount'];
return [new FloatField('amount'), new FloatField('currencyAmount')];
}
protected static function getAssociationFields() {
return [
......
......@@ -22,6 +22,8 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\Model\Field\DateField;
use \Pasteque\Server\Model\Field\FloatField;
use \Pasteque\Server\System\DAO\DAO;
use \Pasteque\Server\System\DAO\DoctrineEmbeddedModel;
......@@ -37,9 +39,15 @@ use \Pasteque\Server\System\DAO\DoctrineEmbeddedModel;
class CashSessionTax extends DoctrineEmbeddedModel
{
protected static function getDirectFieldNames() {
return ['taxRate',
'base', 'basePeriod', 'baseFYear',
'amount', 'amountPeriod', 'amountFYear'];
return [
new FloatField('taxRate'),
new FloatField('base'),
new FloatField('basePeriod'),
new FloatField('baseFYear'),
new FloatField('amount'),
new FloatField('amountPeriod'),
new FloatField('amountFYear')
];
}
protected static function getAssociationFields() {
return [
......
......@@ -21,6 +21,8 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\Model\Field\IntField;
use \Pasteque\Server\Model\Field\StringField;
use \Pasteque\Server\System\DAO\DoctrineMainModel;
/**
......@@ -34,7 +36,12 @@ class Category extends DoctrineMainModel
{
protected static function getDirectFieldNames() {
return ['id', 'reference', 'label', 'dispOrder', 'hasImage'];
return ['id',
new StringField('reference'),
new StringField('label'),
new IntField('dispOrder'),
'hasImage'
];
}
protected static function getAssociationFields() {
return [
......
......@@ -21,6 +21,8 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\Model\Field\IntField;
use \Pasteque\Server\Model\Field\StringField;
use \Pasteque\Server\System\DAO\DoctrineEmbeddedModel;
/**
......@@ -33,7 +35,7 @@ use \Pasteque\Server\System\DAO\DoctrineEmbeddedModel;
class CompositionGroup extends DoctrineEmbeddedModel
{
protected static function getDirectFieldNames() {
return ['label', 'dispOrder'];
return [new StringField('label'), new IntField('dispOrder')];
}
protected static function getAssociationFields() {
return [
......
......@@ -21,6 +21,7 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\Model\Field\IntField;
use \Pasteque\Server\System\DAO\DoctrineEmbeddedModel;
/**
......@@ -33,7 +34,7 @@ use \Pasteque\Server\System\DAO\DoctrineEmbeddedModel;
class CompositionProduct extends DoctrineEmbeddedModel
{
protected static function getDirectFieldNames() {
return ['dispOrder'];
return [new IntField('dispOrder')];
}
protected static function getAssociationFields() {
return [
......
......@@ -22,6 +22,9 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\Model\Field\BoolField;
use \Pasteque\Server\Model\Field\FloatField;
use \Pasteque\Server\Model\Field\StringField;
use \Pasteque\Server\System\DAO\DoctrineMainModel;
/**
......@@ -34,8 +37,17 @@ use \Pasteque\Server\System\DAO\DoctrineMainModel;
class Currency extends DoctrineMainModel
{
protected static function getDirectFieldNames() {
return ['reference', 'label', 'symbol', 'decimalSeparator',
'thousandsSeparator', 'format', 'rate', 'main', 'visible'];
return [
new StringField('reference'),
new StringField('label'),
new StringField('symbol'),
new StringField('decimalSeparator'),
new StringField('thousandsSeparator'),
new StringField('format'),
new FloatField('rate'),
new BoolField('main'),
new BoolField('visible')
];
}
protected static function getAssociationFields() {
return [];
......
......@@ -21,6 +21,10 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\Model\Field\BoolField;
use \Pasteque\Server\Model\Field\DateField;
use \Pasteque\Server\Model\Field\FloatField;
use \Pasteque\Server\Model\Field\StringField;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\DAO\DoctrineMainModel;
......@@ -34,11 +38,27 @@ use \Pasteque\Server\System\DAO\DoctrineMainModel;
class Customer extends DoctrineMainModel
{
protected static function getDirectFieldNames() {
return ['id', 'dispName', 'card',
'maxDebt', 'balance',
'firstName', 'lastName', 'email', 'phone1', 'phone2', 'fax',
'addr1', 'addr2', 'zipCode', 'city', 'region', 'country',
'note', 'visible', 'hasImage', 'expireDate'];
return ['id',
new StringField('dispName'),
new StringField('card'),
new FloatField('maxDebt'),
new FloatField('balance'),
new StringField('firstName'),
new StringField('lastName'),
new StringField('email'),
new StringField('phone1'),
new StringField('phone2'),
new StringField('fax'),
new StringField('addr1'),
new StringField('addr2'),
new StringField('zipCode'),
new StringField('city'),
new StringField('region'),
new StringField('country'),
new StringField('note'),
new BoolField('visible'),
'hasImage',
new DateField('expireDate', ['nullable' => true])];
}
protected static function getAssociationFields() {
return [
......
......@@ -22,6 +22,10 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\Model\Field\DateField;
use \Pasteque\Server\Model\Field\FloatField;
use \Pasteque\Server\Model\Field\IntField;
use \Pasteque\Server\Model\Field\StringField;
use \Pasteque\Server\System\DAO\DoctrineMainModel;
/**
......@@ -35,8 +39,15 @@ use \Pasteque\Server\System\DAO\DoctrineMainModel;
class Discount extends DoctrineMainModel
{
protected static function getDirectFieldNames() {
return ['id', 'label', 'startDate', 'endDate', 'rate', 'barcode',
'barcodeType', 'dispOrder'];
return ['id',
new StringField('label'),
new DateField('startDate', ['nullable' => true]),
new DateField('endDate', ['nullable' => true]),
new FloatField('rate'),
new StringField('barcode'),
new IntField('barcodeType'),
new IntField('dispOrder')
];
}
protected static function getAssociationFields() {
return [];
......
......@@ -21,6 +21,8 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\Model\Field\FloatField;
use \Pasteque\Server\Model\Field\StringField;
use \Pasteque\Server\System\DAO\DoctrineMainModel;
/**
......@@ -33,7 +35,8 @@ use \Pasteque\Server\System\DAO\DoctrineMainModel;
class DiscountProfile extends DoctrineMainModel
{
protected static function getDirectFieldNames() {
return ['label', 'rate'];
return [new StringField('label'),
new FloatField('rate')];
}
protected static function getAssociationFields() {
return [];
......
<?php
// Pastèque API
//
// Copyright (C)
// 2012 Scil (http://scil.coop)
// 2017 Karamel, Association Pastèque (karamel@creativekara.fr, https://pasteque.org)
//
// This file is part of Pastèque.
//
// Pastèque is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Pastèque is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Pastèque. If not, see <http://www.gnu.org/licenses/>.
namespace Pasteque\Server\Model\Field;
use \Pasteque\Server\Exception\InvalidFieldException;
/** Field for boolean values. Null may be valid. */
class BoolField extends Field
{
public const TYPE = 'bool';
public const CONVERT_CSTR = InvalidFieldException::CSTR_BOOL;
/**
* @Override From Field::convert
* Convert to boolean. Any integer values outside 0 is considered true,
* 0 is false. 'true' and 'false' ignoring case are converted.
* String representation of integers are accepted.
*/
public function convert($input) {
if ($input === null || is_bool($input)) {
return $input;
}
if (is_int($input)) {
return $input != 0;
}
if (is_string($input)) {
$str = str_replace(' ', '', $input);
if (strtolower($str) == 'true') {
return true;
}
if (strtolower($str) == 'false') {
return false;
}
if (preg_match('/^[+-]?[0-9]+$/', $str)) {
$conv = intval($str);
return $conv != 0;
}
}
throw new \InvalidArgumentException();
}
}
<?php
// Pastèque API
//
// Copyright (C)
// 2012 Scil (http://scil.coop)
// 2017 Karamel, Association Pastèque (karamel@creativekara.fr, https://pasteque.org)
//
// This file is part of Pastèque.
//
// Pastèque is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Pastèque is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Pastèque. If not, see <http://www.gnu.org/licenses/>.
namespace Pasteque\Server\Model\Field;
use \Pasteque\Server\Exception\InvalidFieldException;
use \Pasteque\Server\System\DateUtils;
class DateField extends Field
{
public const TYPE = 'date';
public const CONVERT_CSTR = InvalidFieldException::CSTR_INVALID_DATE;
/**
* @Override From Field::convert
*/
public function convert($input) {
$converted = DateUtils::readDate($input);
if ($converted === false) {
throw new \InvalidArgumentException();
}
return $converted;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment