Use Assert lib for value checking in setters

This commit is contained in:
Jan Böhmer 2019-03-05 17:02:48 +01:00
parent 6276d93389
commit 8c8bbbbd86
4 changed files with 130 additions and 109 deletions

View file

@ -150,6 +150,8 @@ abstract class Company extends StructuralDBElement
/** /**
* Set the addres * Set the addres
* @param string $new_address the new address (with "\n" as line break) * @param string $new_address the new address (with "\n" as line break)
*
* @return self
*/ */
public function setAddress(string $new_address) : self public function setAddress(string $new_address) : self
{ {
@ -161,6 +163,8 @@ abstract class Company extends StructuralDBElement
* Set the phone number * Set the phone number
* *
* @param string $new_phone_number the new phone number * @param string $new_phone_number the new phone number
*
* @return self
*/ */
public function setPhoneNumber(string $new_phone_number) : self public function setPhoneNumber(string $new_phone_number) : self
{ {
@ -172,6 +176,8 @@ abstract class Company extends StructuralDBElement
* Set the fax number * Set the fax number
* *
* @param string $new_fax_number the new fax number * @param string $new_fax_number the new fax number
*
* @return self
*/ */
public function setFaxNumber(string $new_fax_number) : self public function setFaxNumber(string $new_fax_number) : self
{ {
@ -183,6 +189,8 @@ abstract class Company extends StructuralDBElement
* Set the e-mail address * Set the e-mail address
* *
* @param string $new_email_address the new e-mail address * @param string $new_email_address the new e-mail address
*
* @return self
*/ */
public function setEmailAddress(string $new_email_address) : self public function setEmailAddress(string $new_email_address) : self
@ -195,6 +203,8 @@ abstract class Company extends StructuralDBElement
* Set the website * Set the website
* *
* @param string $new_website the new website * @param string $new_website the new website
*
* @return self
*/ */
public function setWebsite(string $new_website) : self public function setWebsite(string $new_website) : self
{ {
@ -207,6 +217,7 @@ abstract class Company extends StructuralDBElement
* *
* @param string $new_url the new URL with the placeholder %PARTNUMBER% for the part number * @param string $new_url the new URL with the placeholder %PARTNUMBER% for the part number
* *
* @return self
*/ */
public function setAutoProductUrl(string $new_url) : self public function setAutoProductUrl(string $new_url) : self
{ {

View file

@ -108,6 +108,7 @@ class Device extends PartsContainingDBElement
* Set the order quantity * Set the order quantity
* *
* @param integer $new_order_quantity the new order quantity * @param integer $new_order_quantity the new order quantity
* @return self
*/ */
public function setOrderQuantity(int $new_order_quantity) : self public function setOrderQuantity(int $new_order_quantity) : self
{ {
@ -121,9 +122,8 @@ class Device extends PartsContainingDBElement
/** /**
* Set the "order_only_missing_parts" attribute * Set the "order_only_missing_parts" attribute
*
* @param boolean $new_order_only_missing_parts the new "order_only_missing_parts" attribute * @param boolean $new_order_only_missing_parts the new "order_only_missing_parts" attribute
* * @return self
*/ */
public function setOrderOnlyMissingParts(bool $new_order_only_missing_parts) : self public function setOrderOnlyMissingParts(bool $new_order_only_missing_parts) : self
{ {

View file

@ -35,6 +35,7 @@ namespace App\Entity;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Webmozart\Assert\Assert;
/** /**
* Class Part * Class Part
@ -174,48 +175,24 @@ class Part extends AttachmentContainingDBElement
********************************************************************************/ ********************************************************************************/
/** /**
* Get the description * Get the description string like it is saved in the database.
* This can contain BBCode, it is not parsed yet.
* *
* @param boolean|int $bbcode_parse_level Should BBCode converted to HTML, before returning
* @param int $short_output If this is bigger than 0, than the description will be shortened to this length.
* @return string the description * @return string the description
*/ */
public function getDescription($bbcode_parse_level = 0/*BBCodeParsingLevel::PARSE*/, int $short_output = 0) : string public function getDescription() : string
{ {
$val = htmlspecialchars($this->description); return htmlspecialchars($this->description);
if ($short_output > 0 && \strlen($val) > $short_output) {
$val = substr($val, 0, $short_output);
$val .= '...';
$val = '<span class="text-muted">' . $val . '</span class="text-muted">';
}
//TODO
/**
if ($bbcode_parse_level === BBCodeParsingLevel::PARSE) {
$bbcode = new BBCodeParser();
$val = $bbcode->only('bold', 'italic', 'underline', 'linethrough')->parse($val);
} elseif ($bbcode_parse_level === BBCodeParsingLevel::STRIP) {
$bbcode = new BBCodeParser();
$val = $bbcode->stripBBCodeTags($val);
}*/
return $val;
} }
/** /**
* Get the count of parts which are in stock * Get the count of parts which are in stock.
* @param $with_unknown bool Set this, to true, if the unknown state should be returned as string. Otherwise -2 is returned. * When the instock is unkown, then Part::INSTOCK_UNKNOWN is returned.
* *
* @return integer|string count of parts which are in stock, "Unknown" if $with_unknown is set and instock is unknown. * @return int count of parts which are in stock
*/ */
public function getInstock(bool $with_unknown = false) : int public function getInstock() : int
{ {
if ($with_unknown && $this->isInstockUnknown()) {
return _('[Unbekannt]');
}
return $this->instock; return $this->instock;
} }
@ -239,30 +216,14 @@ class Part extends AttachmentContainingDBElement
} }
/** /**
* Get the comment * Get the comment associated with this part.
* *
* @param boolean|int $bbcode_parsing_level Should BBCode converted to HTML, before returning * @return string The raw/unparsed comment
* @return string the comment
*/ */
public function getComment($bbcode_parsing_level = true /*= BBCodeParsingLevel::PARSE*/) : string public function getComment() : string
{ {
$val = htmlspecialchars($this->comment); return htmlspecialchars($this->comment);
//TODO
/*if ($bbcode_parsing_level === BBCodeParsingLevel::PARSE) {
$bbcode = new BBCodeParser();
$bbcode->setParser('brLinebreak', "/\[br\]/s", '<br/>', '');
$bbcode->setParser('namedlink', '/\[url\=(.*?)\](.*?)\[\/url\]/s', '<a href="$1" class="link-external" target="_blank">$2</a>', '$2');
$bbcode->setParser('link', '/\[url\](.*?)\[\/url\]/s', '<a href="$1" class="link-external" target="_blank">$1</a>', '$1');
$val = $bbcode->parse($val);
} elseif ($bbcode_parsing_level === BBCodeParsingLevel::STRIP) {
$bbcode = new BBCodeParser();
$val = str_replace("\n", ' ', $val);
$val = $bbcode->stripBBCodeTags($val);
}*/
return $val;
} }
/** /**
@ -271,11 +232,9 @@ class Part extends AttachmentContainingDBElement
* A Part is marked as "obsolete" if all their orderdetails are marked as "obsolete". * A Part is marked as "obsolete" if all their orderdetails are marked as "obsolete".
* If a part has no orderdetails, the part isn't marked as obsolete. * If a part has no orderdetails, the part isn't marked as obsolete.
* *
* @return boolean @li true if this part is obsolete * @return boolean true, if this part is obsolete. false, if this part isn't obsolete
* @li false if this part isn't obsolete
* @throws Exception
*/ */
public function getObsolete() : bool public function isObsolete() : bool
{ {
$all_orderdetails = $this->getOrderdetails(); $all_orderdetails = $this->getOrderdetails();
@ -295,12 +254,12 @@ class Part extends AttachmentContainingDBElement
/** /**
* Get if this part is visible * Get if this part is visible
* *
* @return boolean @li true if this part is visible * @return boolean true if this part is visible
* @li false if this part isn't visible * false if this part isn't visible
*/ */
public function getVisible() : bool public function isVisible() : bool
{ {
return (bool) $this->visible; return $this->visible;
} }
/** /**
@ -309,9 +268,9 @@ class Part extends AttachmentContainingDBElement
* @return bool * true if this part is a favorite * @return bool * true if this part is a favorite
* * false if this part is not a favorite. * * false if this part is not a favorite.
*/ */
public function getFavorite() : bool public function isFavorite() : bool
{ {
return (bool) $this->favorite; return $this->favorite;
} }
/** /**
@ -340,7 +299,7 @@ class Part extends AttachmentContainingDBElement
*/ */
public function getOrderQuantity() : int public function getOrderQuantity() : int
{ {
return (int) $this->order_quantity; return $this->order_quantity;
} }
/** /**
@ -380,20 +339,21 @@ class Part extends AttachmentContainingDBElement
} }
/** /**
* Get the "manual_order" attribute * Check if this part is marked for manual ordering
* *
* @return boolean the "manual_order" attribute * @return boolean the "manual_order" attribute
*/ */
public function getManualOrder() : bool public function isMarkedForManualOrder() : bool
{ {
return (bool) $this->manual_order; return $this->manual_order;
} }
/** /**
* Check if the part is automatically marked for Ordering, because the instock value is smaller than the min instock value. * Check if the part is automatically marked for Ordering, because the instock value is smaller than the min instock value.
* This is called automatic ordering
* @return bool True, if the part should be ordered. * @return bool True, if the part should be ordered.
*/ */
public function getAutoOrder() : bool public function isAutoOrdered() : bool
{ {
//Parts with negative instock never gets ordered. //Parts with negative instock never gets ordered.
if ($this->getInstock() < 0) { if ($this->getInstock() < 0) {
@ -405,23 +365,35 @@ class Part extends AttachmentContainingDBElement
/** /**
* Get the link to the website of the article on the manufacturers website * Get the link to the website of the article on the manufacturers website
* When no this part has no explicit url set, then it is tried to generate one from the Manufacturer of this part
* automatically.
* *
* @param * @param
* *
* @return string the link to the article * @return string the link to the article
* @throws Exception
*/ */
public function getManufacturerProductUrl(bool $no_auto_url = false) : string public function getManufacturerProductUrl() : string
{ {
if ($no_auto_url || $this->manufacturer_product_url != '') { if ($this->manufacturer_product_url != '') {
return $this->manufacturer_product_url; return $this->manufacturer_product_url;
} elseif (\is_object($this->getManufacturer())) { }
if ($this->getManufacturer() !== null) {
return $this->getManufacturer()->getAutoProductUrl($this->name); return $this->getManufacturer()->getAutoProductUrl($this->name);
} else { } else {
return ''; return '';
} // no url is available } // no url is available
} }
/**
* Similar to getManufacturerProductUrl, but here only the database value is returned.
* @return string The manufacturer url saved in DB for this part.
*/
public function getOwnProductURL() : string
{
return $this->manufacturer_product_url;
}
/** /**
* Get the category of this part * Get the category of this part
* *
@ -805,6 +777,8 @@ class Part extends AttachmentContainingDBElement
* Set the description * Set the description
* *
* @param string $new_description the new description * @param string $new_description the new description
*
* @return self
*/ */
public function setDescription(string $new_description) : self public function setDescription(string $new_description) : self
{ {
@ -817,11 +791,12 @@ class Part extends AttachmentContainingDBElement
* *
* @param integer $new_instock the new count of parts which are in stock * @param integer $new_instock the new count of parts which are in stock
* *
* @throws Exception if the new instock is not valid * @return self
* @throws Exception if there was an error
*/ */
public function setInstock(int $new_instock, $comment = null) : self public function setInstock(int $new_instock, $comment = null) : self
{ {
Assert::natural($new_instock, 'New instock must be positive. Got: %s');
$old_instock = (int) $this->getInstock(); $old_instock = (int) $this->getInstock();
$this->instock = $new_instock; $this->instock = $new_instock;
//TODO //TODO
@ -839,22 +814,40 @@ class Part extends AttachmentContainingDBElement
return $this; return $this;
} }
/**
* Sets the unknown status of this part.
* When the instock is currently unknown and you pass false, then the instock is set to zero.
* If the instock is not unknown and you pass false, nothing is changed.
*
* @param bool $new_unknown Set this to true if the instock should be marked as unknown.
* @return Part
*/
public function setInstockUnknown(bool $new_unknown) : self
{
if($new_unknown == true) {
$this->instock = self::INSTOCK_UNKNOWN;
} else {
//Change only if instock is currently unknown.
if ($this->isInstockUnknown()) {
$this->setInstock(0);
}
}
}
/** /**
* Withdrawal the given number of parts. * Withdrawal the given number of parts.
* @param $count int The number of parts which should be withdrawan. * @param $count int The number of parts which should be withdrawan.
* @param $comment string A comment that should be associated with the withdrawal. * @param $comment string A comment that should be associated with the withdrawal.
* @throws Exception if there was an error *
* @return self
*/ */
public function withdrawalParts(int $count, $comment = null) : self public function withdrawalParts(int $count, $comment = null) : self
{ {
if ($count <= 0) { Assert::greaterThan($count,0, 'Count of withdrawn parts must be greater 0! Got %s!');
throw new \Exception('Zahl der entnommenen Bauteile muss größer 0 sein!'); Assert::greaterThan($count, $this->instock, 'You can not withdraw more parts, than there are existing!');
}
if ($count > $this->getInstock()) {
throw new Exception('Es können nicht mehr Bauteile entnommen werden, als vorhanden sind!');
}
$old_instock = (int) $this->getInstock(); $old_instock = $this->getInstock();
$new_instock = $old_instock - $count; $new_instock = $old_instock - $count;
//TODO //TODO
@ -878,17 +871,16 @@ class Part extends AttachmentContainingDBElement
* Add the given number of parts. * Add the given number of parts.
* @param $count int The number of parts which should be withdrawan. * @param $count int The number of parts which should be withdrawan.
* @param $comment string A comment that should be associated with the withdrawal. * @param $comment string A comment that should be associated with the withdrawal.
* @throws Exception if there was an error *
* @return self
*/ */
public function addParts(int $count, string $comment = null) : self public function addParts(int $count, string $comment = null) : self
{ {
Assert::greaterThan($count, 0, 'Count of added parts must be greater zero! Got %s.');
//TODO //TODO
if ($count <= 0) {
throw new Exception('Zahl der entnommenen Bauteile muss größer 0 sein!');
}
$old_instock = (int) $this->getInstock(); $old_instock = $this->getInstock();
$new_instock = $old_instock + $count; $new_instock = $old_instock + $count;
//TODO //TODO
@ -912,17 +904,13 @@ class Part extends AttachmentContainingDBElement
* Set the count of parts which should be in stock at least * Set the count of parts which should be in stock at least
* *
* @param integer $new_mininstock the new count of parts which should be in stock at least * @param integer $new_mininstock the new count of parts which should be in stock at least
* * @return self
* @throws Exception if the new mininstock is not valid
*/ */
public function setMinInstock(int $new_mininstock) : self public function setMinInstock(int $new_mininstock) : self
{ {
if($new_mininstock < 0) { Assert::natural($new_mininstock, 'The new minimum instock value must be positive! Got %s.');
throw new \InvalidArgumentException('$new_mininstock must be positive!');
}
$this->mininstock = $new_mininstock; $this->mininstock = $new_mininstock;
return $this; return $this;
} }
@ -931,7 +919,7 @@ class Part extends AttachmentContainingDBElement
* *
* @param string $new_comment the new comment * @param string $new_comment the new comment
* *
* @throws Exception if there was an error * @return self
*/ */
public function setComment(string $new_comment) : self public function setComment(string $new_comment) : self
{ {
@ -952,11 +940,15 @@ class Part extends AttachmentContainingDBElement
* set this orderdetails as order orderdetails. * set this orderdetails as order orderdetails.
* Otherwise, set "no order orderdetails") * Otherwise, set "no order orderdetails")
* *
* @throws Exception if there was an error * @return self
*/ */
public function setManualOrder(bool $new_manual_order, int $new_order_quantity = 1, $new_order_orderdetails_id = null) : self public function setManualOrder(bool $new_manual_order, int $new_order_quantity = 1, $new_order_orderdetails_id = null) : self
{ {
Assert::greaterThan($new_order_quantity, 0, 'The new order quantity must be greater zero. Got %s!');
$this->manual_order = $new_manual_order; $this->manual_order = $new_manual_order;
//TODO; //TODO;
/* $this->order_orderdetail = $new_order_orderdetails_id; */ /* $this->order_orderdetail = $new_order_orderdetails_id; */
$this->order_quantity = $new_order_quantity; $this->order_quantity = $new_order_quantity;
@ -969,6 +961,8 @@ class Part extends AttachmentContainingDBElement
* *
* @param integer|NULL $new_order_orderdetails_id @li the new order orderdetails ID * @param integer|NULL $new_order_orderdetails_id @li the new order orderdetails ID
* @li Or, to remove the orderdetails, pass a NULL * @li Or, to remove the orderdetails, pass a NULL
*
* @return self
*/ */
public function setOrderOrderdetailsID($new_order_orderdetails_id) : self public function setOrderOrderdetailsID($new_order_orderdetails_id) : self
{ {
@ -982,9 +976,13 @@ class Part extends AttachmentContainingDBElement
* Set the order quantity * Set the order quantity
* *
* @param integer $new_order_quantity the new order quantity * @param integer $new_order_quantity the new order quantity
*
* @return self
*/ */
public function setOrderQuantity(int $new_order_quantity) : self public function setOrderQuantity(int $new_order_quantity) : self
{ {
Assert::greaterThan($new_order_quantity,0, 'The new order quantity must be greater zero. Got %s!');
$this->order_quantity = $new_order_quantity; $this->order_quantity = $new_order_quantity;
return $this; return $this;
@ -1000,6 +998,8 @@ class Part extends AttachmentContainingDBElement
* *
* @throws Exception if the new category ID is not valid * @throws Exception if the new category ID is not valid
* @throws Exception if there was an error * @throws Exception if there was an error
*
* @return self
*/ */
public function setCategoryID(int $new_category_id) : self public function setCategoryID(int $new_category_id) : self
{ {
@ -1064,6 +1064,8 @@ class Part extends AttachmentContainingDBElement
* Set the favorite status for this part. * Set the favorite status for this part.
* @param $new_favorite_status bool The new favorite status, that should be applied on this part. * @param $new_favorite_status bool The new favorite status, that should be applied on this part.
* Set this to true, when the part should be a favorite. * Set this to true, when the part should be a favorite.
*
* @return self
*/ */
public function setFavorite(bool $new_favorite_status) : self public function setFavorite(bool $new_favorite_status) : self
{ {
@ -1075,7 +1077,7 @@ class Part extends AttachmentContainingDBElement
/** /**
* Sets the URL to the manufacturer site about this Part. Set to "" if this part should use the automatically URL based on its manufacturer. * Sets the URL to the manufacturer site about this Part. Set to "" if this part should use the automatically URL based on its manufacturer.
* @param string $new_url The new url * @param string $new_url The new url
* @throws Exception when an error happens. * @return self
*/ */
public function setManufacturerProductURL(string $new_url) : self public function setManufacturerProductURL(string $new_url) : self
{ {
@ -1092,6 +1094,8 @@ class Part extends AttachmentContainingDBElement
* *
* @throws Exception if the new ID is not valid * @throws Exception if the new ID is not valid
* @throws Exception if there was an error * @throws Exception if there was an error
*
* @return self
*/ */
public function setMasterPictureAttachementID($new_master_picture_attachement_id) : self public function setMasterPictureAttachementID($new_master_picture_attachement_id) : self
{ {

View file

@ -35,6 +35,7 @@ namespace App\Entity;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Webmozart\Assert\Assert;
/** /**
* Class Pricedetail * Class Pricedetail
@ -138,7 +139,7 @@ class Pricedetail extends DBElement
*/ */
public function getPriceRelatedQuantity() : int public function getPriceRelatedQuantity() : int
{ {
return (int) $this->price_related_quantity; return $this->price_related_quantity;
} }
/** /**
@ -153,7 +154,7 @@ class Pricedetail extends DBElement
*/ */
public function getMinDiscountQuantity() : int public function getMinDiscountQuantity() : int
{ {
return (int) $this->min_discount_quantity; return $this->min_discount_quantity;
} }
/******************************************************************************** /********************************************************************************
@ -170,13 +171,13 @@ class Pricedetail extends DBElement
* @li This is the price for "price_related_quantity" parts!! * @li This is the price for "price_related_quantity" parts!!
* @li Example: if "price_related_quantity" is '10', * @li Example: if "price_related_quantity" is '10',
* you have to set here the price for 10 parts! * you have to set here the price for 10 parts!
*
* @return self
*/ */
public function setPrice(float $new_price) : self public function setPrice(float $new_price) : self
{ {
if($new_price < 0) Assert::natural($new_price, 'The new price must be positive! Got %s!');
{
throw new \InvalidArgumentException('$new_price must be positive!');
}
$this->price = $new_price; $this->price = $new_price;
return $this; return $this;
@ -192,12 +193,15 @@ class Pricedetail extends DBElement
* quantity to 100. The single price (20$/100 = 0.2$) will be calculated automatically. * quantity to 100. The single price (20$/100 = 0.2$) will be calculated automatically.
* *
* @param integer $new_price_related_quantity the price related quantity * @param integer $new_price_related_quantity the price related quantity
*
* @return self
*/ */
public function setPriceRelatedQuantity(int $new_price_related_quantity) : self public function setPriceRelatedQuantity(int $new_price_related_quantity) : self
{ {
if($new_price_related_quantity <= 0) {
throw new \InvalidArgumentException('$new_price_related_quantity must be greater 0!'); Assert::greaterThan($new_price_related_quantity, 0,
} 'The new price related quantity must be greater zero! Got %s.');
$this->price_related_quantity = $new_price_related_quantity; $this->price_related_quantity = $new_price_related_quantity;
return $this; return $this;
@ -219,12 +223,14 @@ class Pricedetail extends DBElement
* So the orderdetails would have three Pricedetails for one supplier.) * So the orderdetails would have three Pricedetails for one supplier.)
* *
* @param integer $new_min_discount_quantity the minimum discount quantity * @param integer $new_min_discount_quantity the minimum discount quantity
*
* @return self
*/ */
public function setMinDiscountQuantity(int $new_min_discount_quantity) : self public function setMinDiscountQuantity(int $new_min_discount_quantity) : self
{ {
if($new_min_discount_quantity <= 0 ){ Assert::greaterThan($new_min_discount_quantity, 0,
throw new \InvalidArgumentException('$new_min_discount_quantity must be positive!'); 'The new minimum discount quantity must be greater zero! Got %s.');
}
$this->min_discount_quantity = $new_min_discount_quantity; $this->min_discount_quantity = $new_min_discount_quantity;
return $this; return $this;