Skip to content

Commit

Permalink
Code style, Exceptions and phone in Shop. Little performance improvem…
Browse files Browse the repository at this point in the history
…ent (#6)

* update code_style, add phone to shop, little performance improvement

* update code_style, add phone to shop, little performance improvement

* delete .gitignore

* code style, Exceptions

* code style, Exceptions
  • Loading branch information
urfinjuezz authored and lireincore committed Jul 24, 2017
1 parent 9ed7208 commit 980dc46
Show file tree
Hide file tree
Showing 23 changed files with 372 additions and 225 deletions.
15 changes: 8 additions & 7 deletions src/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ class Category
*/
public function isValid()
{
if ($this->id === null)
if ($this->id === null) {
$this->addError("Category: missing required attribute 'id'");
elseif (!is_numeric($this->id) || (int)$this->id <= 0)
} elseif (!is_numeric($this->id) || (int)$this->id <= 0) {
$this->addError("Category: incorrect value in attribute 'id'");

if ($this->parentId !== null && (!is_numeric($this->parentId) || (int)$this->parentId <= 0))
}
if ($this->parentId !== null && (!is_numeric($this->parentId) || (int)$this->parentId <= 0)) {
$this->addError("Category: incorrect value in attribute 'parentId'");

if (!$this->name)
$this->addError("Category: incorrect value");
}
if (!$this->name) {
$this->addError('Category: incorrect value');
}

return empty($this->errors);
}
Expand Down
16 changes: 8 additions & 8 deletions src/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ class Currency
*/
public function isValid()
{
if ($this->id === null)
if ($this->id === null) {
$this->addError("Currency: missing required attribute 'id'");
elseif (!in_array($this->id, ['RUR', 'RUB', 'UAH', 'BYN', 'BYR', 'KZT', 'USD', 'EUR']))
} elseif (!in_array($this->id, ['RUR', 'RUB', 'UAH', 'BYN', 'BYR', 'KZT', 'USD', 'EUR'], true)) {
$this->addError("Currency: incorrect value in attribute 'id'");

if ($this->rate === null)
}
if ($this->rate === null) {
$this->addError("Currency: missing required attribute 'rate'");
elseif (!(in_array($this->rate, ['CBRF', 'NBU', 'NBK', 'CB']) || (is_numeric($this->rate) && (float)$this->rate > 0)))
} elseif (!(in_array($this->rate, ['CBRF', 'NBU', 'NBK', 'CB'], true) || (is_numeric($this->rate) && (float)$this->rate > 0))) {
$this->addError("Currency: incorrect value in attribute 'rate'");

if ($this->plus !== null && (int)$this->plus < 0)
}
if ($this->plus !== null && (int)$this->plus < 0) {
$this->addError("Currency: incorrect value in attribute 'plus'");

}
return empty($this->errors);
}

Expand Down
11 changes: 7 additions & 4 deletions src/DeliveryOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@ class DeliveryOption
*/
public function isValid()
{
if ($this->cost === null)
if ($this->cost === null) {
$this->addError("DeliveryOption: missing required attribute 'cost'");
elseif (!is_numeric($this->cost) || ((int)$this->cost) < 0)
} elseif (!is_numeric($this->cost) || ((int)$this->cost) < 0) {
$this->addError("DeliveryOption: incorrect value in attribute 'cost'");
}

if ($this->days === null)
if ($this->days === null) {
$this->addError("DeliveryOption: missing required attribute 'days'");
}

if ($this->orderBefore !== null && (!is_numeric($this->orderBefore) || (int)$this->orderBefore < 0 || (int)$this->orderBefore > 24))
if ($this->orderBefore !== null && (!is_numeric($this->orderBefore) || (int)$this->orderBefore < 0 || (int)$this->orderBefore > 24)) {
$this->addError("DeliveryOption: incorrect value in attribute 'order-before'");
}

return empty($this->errors);
}
Expand Down
13 changes: 13 additions & 0 deletions src/Exception/AException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace LireinCore\Exception;
/**
* Created by PhpStorm.
* User: rudichev
* Date: 20.07.2017
* Time: 11:41
*/
class AException extends \Exception
{

}
13 changes: 13 additions & 0 deletions src/Exception/FileNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace LireinCore\Exception;
/**
* Created by PhpStorm.
* User: rudichev
* Date: 20.07.2017
* Time: 11:43
*/
class FileNotFoundException extends AException
{

}
18 changes: 9 additions & 9 deletions src/Offer/ABookOffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ abstract class ABookOffer extends AExtOffer
public function getAttributesList()
{
return array_merge(parent::getAttributesList(), [
//subnodes
//subNodes
'name', 'author', 'publisher', 'series', 'year', 'ISBN', 'volume', 'part', 'language', 'table_of_contents'
]);
}
Expand All @@ -72,18 +72,18 @@ public function isValid()
{
$isValid = parent::isValid();

if ($this->name === null)
if ($this->name === null) {
$this->addError("Offer: missing required attribute 'name'");

if ($this->year !== null && !is_numeric($this->year))
}
if ($this->year !== null && !is_numeric($this->year)) {
$this->addError("Offer: incorrect value in attribute 'year'");

if ($this->volume !== null && (!is_numeric($this->volume) || (int)$this->volume <= 0))
}
if ($this->volume !== null && (!is_numeric($this->volume) || (int)$this->volume <= 0)) {
$this->addError("Offer: incorrect value in attribute 'volume'");

if ($this->part !== null && (!is_numeric($this->part) || (int)$this->part <= 0))
}
if ($this->part !== null && (!is_numeric($this->part) || (int)$this->part <= 0)) {
$this->addError("Offer: incorrect value in attribute 'part'");

}
return $isValid && empty($this->errors);
}

Expand Down
49 changes: 26 additions & 23 deletions src/Offer/AExtOffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getAttributesList()
return array_merge(parent::getAttributesList(), [
//attributes
'fee',
//subnodes
//subNodes
'market_category', 'delivery-options', 'local_delivery_cost', 'manufacturer_warranty', 'adult', 'age', 'downloadable'
]);
}
Expand All @@ -67,32 +67,35 @@ public function isValid()
{
$isValid = parent::isValid();

if ($this->fee !== null && (!is_numeric($this->fee) || (int)$this->fee <= 0))
if ($this->fee !== null && (!is_numeric($this->fee) || (int)$this->fee <= 0)) {
$this->addError("Offer: incorrect value in attribute 'fee'");

if ($this->localDeliveryCost !== null && (!is_numeric($this->localDeliveryCost) || ((int)$this->localDeliveryCost) < 0))
}
if ($this->localDeliveryCost !== null && (!is_numeric($this->localDeliveryCost) || ((int)$this->localDeliveryCost) < 0)) {
$this->addError("Offer: incorrect value in attribute 'local_delivery_cost'");

if ($this->delivery === true && !$this->deliveryOptions && $this->localDeliveryCost == null)
}
if ($this->delivery === true && !$this->deliveryOptions && $this->localDeliveryCost === null) {
$this->addError("Offer: attribute 'delivery-options' is required when 'delivery' is true");

if ($this->manufacturerWarranty !== null && $this->manufacturerWarranty !== 'true' && $this->manufacturerWarranty !== 'false')
}
if ($this->manufacturerWarranty !== null && $this->manufacturerWarranty !== 'true' && $this->manufacturerWarranty !== 'false') {
$this->addError("Offer: incorrect value in attribute 'manufacturer_warranty'");

if ($this->downloadable !== null && $this->downloadable !== 'true' && $this->downloadable !== 'false')
}
if ($this->downloadable !== null && $this->downloadable !== 'true' && $this->downloadable !== 'false') {
$this->addError("Offer: incorrect value in attribute 'downloadable'");

if ($this->adult !== null && $this->adult !== 'true' && $this->adult !== 'false')
}
if ($this->adult !== null && $this->adult !== 'true' && $this->adult !== 'false') {
$this->addError("Offer: incorrect value in attribute 'adult'");

}
$subIsValid = true;
if ($this->deliveryOptions) {
foreach ($this->deliveryOptions as $deliveryOption) {
if (!$deliveryOption->isValid()) $subIsValid = false;
if (!$deliveryOption->isValid()) {
$subIsValid = false;
}
}
}
if ($this->age && !$this->age->isValid()) $subIsValid = false;

if ($this->age && !$this->age->isValid()) {
$subIsValid = false;
}
return $isValid && empty($this->errors) && $subIsValid;
}

Expand All @@ -101,18 +104,18 @@ public function isValid()
*/
public function getErrors()
{
$errors = parent::getErrors();
$errors[] = parent::getErrors();

if ($this->deliveryOptions) {
foreach ($this->deliveryOptions as $deliveryOption) {
$errors = array_merge($errors, $deliveryOption->getErrors());
$errors[] = $deliveryOption->getErrors();
}
}
if ($this->age) {
$errors = array_merge($errors, $this->age->getErrors());
$errors[] = $this->age->getErrors();
}

return $errors;
return 1 !== count($errors) ? call_user_func_array('array_merge', $errors) : $errors[0];
}

/**
Expand All @@ -121,11 +124,11 @@ public function getErrors()
*/
public function addAttribute(array $attrNode)
{
if ($attrNode['name'] == 'delivery-options') {
if ($attrNode['name'] === 'delivery-options') {
foreach ($attrNode['nodes'] as $subNode) {
$this->addDeliveryOption((new DeliveryOption())->addAttributes($subNode['attributes']));
}
} elseif ($attrNode['name'] == 'age') {
} elseif ($attrNode['name'] === 'age') {
$this->setAge((new Age())->addAttributes($attrNode['attributes'] + ['value' => $attrNode['value']]));
} else {
parent::addAttribute($attrNode);
Expand Down Expand Up @@ -196,7 +199,7 @@ public function setLocalDeliveryCost($value)
*/
public function getDeliveryOptions()
{
return $this->deliveryOptions ? $this->deliveryOptions : null;
return $this->deliveryOptions ?: null;
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/Offer/AMainOffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,21 @@ public function isValid()
{
$isValid = parent::isValid();

if ($this->minQuantity !== null && (!is_numeric($this->minQuantity) || (int)$this->minQuantity <= 0))
if ($this->minQuantity !== null && (!is_numeric($this->minQuantity) || (int)$this->minQuantity <= 0)) {
$this->addError("Offer: incorrect value in attribute 'min-quantity'");
}

if ($this->stepQuantity !== null && (!is_numeric($this->stepQuantity) || (int)$this->stepQuantity <= 0))
if ($this->stepQuantity !== null && (!is_numeric($this->stepQuantity) || (int)$this->stepQuantity <= 0)) {
$this->addError("Offer: incorrect value in attribute 'step-quantity'");
}

if ($this->groupId !== null && !is_numeric($this->groupId))
if ($this->groupId !== null && !is_numeric($this->groupId)) {
$this->addError("Offer: incorrect value in attribute 'group_id'");
}

if ($this->from !== null && $this->from !== 'true' && $this->from !== 'false')
if ($this->from !== null && $this->from !== 'true' && $this->from !== 'false') {
$this->addError("Price: incorrect value in attribute 'from'");
}

return $isValid && empty($this->errors);
}
Expand Down
Loading

0 comments on commit 980dc46

Please sign in to comment.