diff --git a/src/Category.php b/src/Category.php index ead43ce..bfb00fb 100644 --- a/src/Category.php +++ b/src/Category.php @@ -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); } diff --git a/src/Currency.php b/src/Currency.php index 6e45db2..c040665 100644 --- a/src/Currency.php +++ b/src/Currency.php @@ -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); } diff --git a/src/DeliveryOption.php b/src/DeliveryOption.php index 1b6ff35..fb5d4c4 100644 --- a/src/DeliveryOption.php +++ b/src/DeliveryOption.php @@ -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); } diff --git a/src/Exception/AException.php b/src/Exception/AException.php new file mode 100644 index 0000000..cebe41c --- /dev/null +++ b/src/Exception/AException.php @@ -0,0 +1,13 @@ +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); } diff --git a/src/Offer/AExtOffer.php b/src/Offer/AExtOffer.php index a65fb4b..f0ca76e 100644 --- a/src/Offer/AExtOffer.php +++ b/src/Offer/AExtOffer.php @@ -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' ]); } @@ -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; } @@ -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]; } /** @@ -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); @@ -196,7 +199,7 @@ public function setLocalDeliveryCost($value) */ public function getDeliveryOptions() { - return $this->deliveryOptions ? $this->deliveryOptions : null; + return $this->deliveryOptions ?: null; } /** diff --git a/src/Offer/AMainOffer.php b/src/Offer/AMainOffer.php index 4e749ec..9273f7e 100644 --- a/src/Offer/AMainOffer.php +++ b/src/Offer/AMainOffer.php @@ -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); } diff --git a/src/Offer/AOffer.php b/src/Offer/AOffer.php index ebbdc0c..222dd0a 100644 --- a/src/Offer/AOffer.php +++ b/src/Offer/AOffer.php @@ -2,10 +2,14 @@ namespace LireinCore\YMLParser\Offer; +use \LireinCore\YMLParser\TYML; +use \LireinCore\YMLParser\TError; + abstract class AOffer { - use \LireinCore\YMLParser\TYML; - use \LireinCore\YMLParser\TError; + + use TYML; + use TError; const DEFAULT_CPA = 1; @@ -132,7 +136,7 @@ public function getAttributesList() return [ //attributes 'id', 'cbid', 'bid', 'available', //type, - //subnodes + //subNodes 'price', 'oldprice', 'currencyId', 'categoryId', 'picture', 'delivery', 'pickup', 'store', 'outlets', 'description', 'sales_notes', 'country_of_origin', 'barcode', 'cpa', 'param', 'expiry', 'weight', 'dimensions' @@ -144,63 +148,79 @@ public function getAttributesList() */ public function isValid() { - if ($this->id === null) + if ($this->id === null) { $this->addError("Offer: missing required attribute 'id'"); - elseif (!$this->id) + } elseif (!$this->id) { $this->addError("Offer: incorrect value in attribute 'id'"); + } - if ($this->bid !== null && (!is_numeric($this->bid) || (int)$this->bid <= 0)) + if ($this->bid !== null && (!is_numeric($this->bid) || (int)$this->bid <= 0)) { $this->addError("Offer: incorrect value in attribute 'bid'"); + } - if ($this->cbid !== null && (!is_numeric($this->cbid) || (int)$this->cbid <= 0)) + if ($this->cbid !== null && (!is_numeric($this->cbid) || (int)$this->cbid <= 0)) { $this->addError("Offer: incorrect value in attribute 'cbid'"); + } if ($this->available === null) { - if ($this->getPickup()) + if ($this->getPickup()) { $this->addError("Offer: attribute 'available' is required when 'pickup' is true"); - } - elseif ($this->available !== 'true' && $this->available !== 'false') + } + } elseif ($this->available !== 'true' && $this->available !== 'false') { $this->addError("Offer: incorrect value in attribute 'available'"); + } - if ($this->price === null) + if ($this->price === null) { $this->addError("Offer: missing required attribute 'price'"); - elseif (!is_numeric($this->price) || (float)$this->price <= 0) + } elseif (!is_numeric($this->price) || (float)$this->price <= 0) { $this->addError("Offer: incorrect value in attribute 'price'"); + } - if ($this->oldprice !== null && (!is_numeric($this->oldprice) || (float)$this->oldprice <= (float)$this->price)) + if ($this->oldprice !== null && (!is_numeric($this->oldprice) || (float)$this->oldprice <= (float)$this->price)) { $this->addError("Offer: incorrect value in attribute 'oldprice'"); - - if ($this->currencyId === null) + } + + if ($this->currencyId === null) { $this->addError("Offer: missing required attribute 'currencyId'"); - elseif (!$this->currencyId) + } elseif (!$this->currencyId) { $this->addError("Offer: incorrect value in attribute 'currencyId'"); + } - if ($this->categoryId === null) + if ($this->categoryId === null) { $this->addError("Offer: missing required attribute 'categoryId'"); - elseif (!$this->categoryId) + } elseif (!$this->categoryId) { $this->addError("Offer: incorrect value in attribute 'categoryId'"); + } - if ($this->store !== null && $this->store !== 'true' && $this->store !== 'false') + if ($this->store !== null && $this->store !== 'true' && $this->store !== 'false') { $this->addError("Offer: incorrect value in attribute 'store'"); + } - if ($this->pickup !== null && $this->pickup !== 'true' && $this->pickup !== 'false') + if ($this->pickup !== null && $this->pickup !== 'true' && $this->pickup !== 'false') { $this->addError("Offer: incorrect value in attribute 'pickup'"); + } - if ($this->delivery !== null && $this->delivery !== 'true' && $this->delivery !== 'false') + if ($this->delivery !== null && $this->delivery !== 'true' && $this->delivery !== 'false') { $this->addError("Offer: incorrect value in attribute 'delivery'"); + } - if ($this->weight !== null && (!is_numeric($this->weight) || (float)$this->weight <= 0)) + if ($this->weight !== null && (!is_numeric($this->weight) || (float)$this->weight <= 0)) { $this->addError("Offer: incorrect value in attribute 'weight'"); + } $subIsValid = true; if ($this->outlets) { foreach ($this->outlets as $outlet) { - if (!$outlet->isValid()) $subIsValid = false; + if (!$outlet->isValid()) { + $subIsValid = false; + } } } if ($this->params) { foreach ($this->params as $param) { - if (!$param->isValid()) $subIsValid = false; + if (!$param->isValid()) { + $subIsValid = false; + } } } @@ -212,20 +232,20 @@ public function isValid() */ public function getErrors() { - $errors = $this->errors; + $errors[] = $this->errors; if ($this->outlets) { foreach ($this->outlets as $outlet) { - $errors = array_merge($errors, $outlet->getErrors()); + $errors[] = $outlet->getErrors(); } } if ($this->params) { foreach ($this->params as $param) { - $errors = array_merge($errors, $param->getErrors()); + $errors[] = $param->getErrors(); } } - return $errors; + return 1 === count($errors) ? $errors[0] : call_user_func_array('array_merge', $errors); } /** @@ -251,18 +271,20 @@ public function fillOffer(array $offerNode) */ public function addAttribute(array $attrNode) { - if ($attrNode['name'] == 'outlets') { + if ($attrNode['name'] === 'outlets') { foreach ($attrNode['nodes'] as $subNode) { $this->addOutlet((new Outlet())->addAttributes($subNode['attributes'])); } - } elseif ($attrNode['name'] == 'picture') { + } elseif ($attrNode['name'] === 'picture') { $this->addPicture($attrNode['value']); - } elseif ($attrNode['name'] == 'barcode') { + } elseif ($attrNode['name'] === 'barcode') { $this->addBarcode($attrNode['value']); - } elseif ($attrNode['name'] == 'param') { + } elseif ($attrNode['name'] === 'param') { $this->addParam((new Param())->addAttributes($attrNode['attributes'] + ['value' => $attrNode['value']])); } else { - if (!is_null($attrNode['value'])) $this->addField($attrNode['name'], $attrNode['value']); + if (!is_null($attrNode['value'])) { + $this->addField($attrNode['name'], $attrNode['value']); + } if (!empty($attrNode['attributes'])) { foreach ($attrNode['attributes'] as $name => $value) { $this->addField($name, $value); @@ -449,7 +471,7 @@ public function setCategoryId($value) */ public function getPictures() { - return $this->pictures ? $this->pictures : null; + return $this->pictures ?: null; } /** @@ -538,7 +560,7 @@ public function setDelivery($value) */ public function getOutlets() { - return $this->outlets ? $this->outlets : null; + return $this->outlets ?: null; } /** @@ -627,7 +649,7 @@ public function setCountryOfOrigin($value) */ public function getBarcodes() { - return $this->barcodes ? $this->barcodes : null; + return $this->barcodes ?: null; } /*** @@ -659,7 +681,7 @@ public function addBarcode($value) */ public function getCpa() { - return $this->cpa === null ? null : ($this->cpa === '1' ? true : false); + return $this->cpa === null ? null : ($this->cpa === '1'); } /** @@ -735,7 +757,7 @@ public function setDimensions($value) */ public function getParams() { - return $this->params ? $this->params : null; + return $this->params ?: null; } /** diff --git a/src/Offer/Age.php b/src/Offer/Age.php index 2d51dcd..64cff97 100644 --- a/src/Offer/Age.php +++ b/src/Offer/Age.php @@ -2,11 +2,14 @@ namespace LireinCore\YMLParser\Offer; +use \LireinCore\YMLParser\TYML; +use \LireinCore\YMLParser\TError; + class Age { - use \LireinCore\YMLParser\TYML; - use \LireinCore\YMLParser\TError; - + use TYML; + use TError; + /** * @var string */ @@ -22,15 +25,18 @@ class Age */ public function isValid() { - if ($this->unit === null) + if ($this->unit === null) { $this->addError("Age: missing required attribute 'unit'"); - elseif ($this->unit !== 'year' && $this->unit !== 'month') + } elseif ($this->unit !== 'year' && $this->unit !== 'month') { $this->addError("Age: incorrect value in attribute 'unit'"); + } + + if ($this->unit === 'year' && !in_array((string)$this->value, ['0', '6', '12', '16', '18'], true)) { + $this->addError('Age: incorrect value'); + } elseif ($this->unit === 'month' && !in_array((string)$this->value, ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'], true)) { + $this->addError('Age: incorrect value'); + } - if ($this->unit === 'year' && !in_array($this->value, ['0', '6', '12', '16', '18'])) - $this->addError("Age: incorrect value"); - elseif ($this->unit === 'month' && !in_array($this->value, ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'])) - $this->addError("Age: incorrect value"); return empty($this->errors); } diff --git a/src/Offer/ArtistTitleOffer.php b/src/Offer/ArtistTitleOffer.php index 9504fd6..03dc682 100644 --- a/src/Offer/ArtistTitleOffer.php +++ b/src/Offer/ArtistTitleOffer.php @@ -50,7 +50,7 @@ class ArtistTitleOffer extends AExtOffer public function getAttributesList() { return array_merge(parent::getAttributesList(), [ - //subnodes + //subNodes 'artist', 'title', 'year', 'media', 'starring', 'director', 'originalName', 'country' ]); } @@ -62,11 +62,13 @@ public function isValid() { $isValid = parent::isValid(); - if ($this->title === null) + if ($this->title === null) { $this->addError("Offer: missing required attribute 'title'"); + } - if ($this->year !== null && !is_numeric($this->year)) + if ($this->year !== null && !is_numeric($this->year)) { $this->addError("Offer: incorrect value in attribute 'year'"); + } return $isValid && empty($this->errors); } diff --git a/src/Offer/AudioBookOffer.php b/src/Offer/AudioBookOffer.php index ef5b978..7e076eb 100644 --- a/src/Offer/AudioBookOffer.php +++ b/src/Offer/AudioBookOffer.php @@ -13,7 +13,7 @@ class AudioBookOffer extends ABookOffer * @var string */ protected $performanceType; - + /** * @var string */ @@ -35,7 +35,7 @@ class AudioBookOffer extends ABookOffer public function getAttributesList() { return array_merge(parent::getAttributesList(), [ - //subnodes + //subNodes 'performed_by', 'performance_type', 'storage', 'format', 'recording_length' ]); } diff --git a/src/Offer/BookOffer.php b/src/Offer/BookOffer.php index 0f97761..fa62e8a 100644 --- a/src/Offer/BookOffer.php +++ b/src/Offer/BookOffer.php @@ -20,7 +20,7 @@ class BookOffer extends ABookOffer public function getAttributesList() { return array_merge(parent::getAttributesList(), [ - //subnodes + //subNodes 'binding', 'page_extent' ]); } @@ -32,8 +32,9 @@ public function isValid() { $isValid = parent::isValid(); - if ($this->pageExtent !== null && (!is_numeric($this->pageExtent) || (int)$this->volume <= 0)) + if ($this->pageExtent !== null && (!is_numeric($this->pageExtent) || (int)$this->volume <= 0)) { $this->addError("Offer: incorrect value in attribute 'page_extent'"); + } return $isValid && empty($this->errors); } diff --git a/src/Offer/EventTicketOffer.php b/src/Offer/EventTicketOffer.php index 1e6eea6..e265d1a 100644 --- a/src/Offer/EventTicketOffer.php +++ b/src/Offer/EventTicketOffer.php @@ -45,7 +45,7 @@ class EventTicketOffer extends AExtOffer public function getAttributesList() { return array_merge(parent::getAttributesList(), [ - //subnodes + //subNodes 'name', 'place', 'hall', 'hall_part', 'date', 'is_premiere', 'is_kids' ]); } @@ -57,20 +57,25 @@ public function isValid() { $isValid = parent::isValid(); - if ($this->name === null) + if ($this->name === null) { $this->addError("Offer: missing required attribute 'name'"); - - if ($this->place === null) + } + + if ($this->place === null) { $this->addError("Offer: missing required attribute 'place'"); - - if ($this->date === null) + } + + if ($this->date === null) { $this->addError("Offer: missing required attribute 'date'"); + } - if ($this->isPremiere !== null && $this->isPremiere !== '0' && $this->isPremiere !== '1') + if ($this->isPremiere !== null && $this->isPremiere !== '0' && $this->isPremiere !== '1') { $this->addError("Offer: incorrect value in attribute 'is_premiere'"); + } - if ($this->isKids !== null && $this->isKids !== '0' && $this->isKids !== '1') + if ($this->isKids !== null && $this->isKids !== '0' && $this->isKids !== '1') { $this->addError("Offer: incorrect value in attribute 'is_kids'"); + } return $isValid && empty($this->errors); } diff --git a/src/Offer/MedicineOffer.php b/src/Offer/MedicineOffer.php index 252b04d..d2bc441 100644 --- a/src/Offer/MedicineOffer.php +++ b/src/Offer/MedicineOffer.php @@ -25,7 +25,7 @@ class MedicineOffer extends AOffer public function getAttributesList() { return array_merge(parent::getAttributesList(), [ - //subnodes + //subNodes 'name', 'vendor', 'vendorCode' ]); } @@ -37,21 +37,25 @@ public function isValid() { $isValid = parent::isValid(); - if ($this->name === null) + if ($this->name === null) { $this->addError("Offer: missing required attribute 'name'"); + } - if ($this->delivery === null) + if ($this->delivery === null) { $this->addError("Offer: missing required attribute 'delivery'"); - elseif ($this->delivery !== 'false') + } elseif ($this->delivery !== 'false') { $this->addError("Offer: incorrect value in attribute 'delivery'"); + } - if ($this->pickup === null) + if ($this->pickup === null) { $this->addError("Offer: missing required attribute 'pickup'"); - elseif ($this->pickup !== 'true') + } elseif ($this->pickup !== 'true') { $this->addError("Offer: incorrect value in attribute 'pickup'"); + } - if ($this->cpa !== null && $this->cpa !== '0') + if ($this->cpa !== null && $this->cpa !== '0') { $this->addError("Offer: incorrect value in attribute 'cpa'"); + } return $isValid && empty($this->errors); } diff --git a/src/Offer/Outlet.php b/src/Offer/Outlet.php index 83284e0..c0f8729 100644 --- a/src/Offer/Outlet.php +++ b/src/Offer/Outlet.php @@ -2,10 +2,13 @@ namespace LireinCore\YMLParser\Offer; +use \LireinCore\YMLParser\TYML; +use \LireinCore\YMLParser\TError; + class Outlet { - use \LireinCore\YMLParser\TYML; - use \LireinCore\YMLParser\TError; + use TYML; + use TError; const DEFAULT_INSTOCK = 0; const DEFAULT_BOOKING = true; @@ -30,16 +33,19 @@ class Outlet */ public function isValid() { - if ($this->id === null) + if ($this->id === null) { $this->addError("Outlet: missing required attribute 'id'"); - elseif (!$this->id) + } elseif (!$this->id) { $this->addError("Outlet: incorrect value in attribute 'id'"); + } - if ($this->instock !== null && (!is_numeric($this->instock) || (int)$this->instock < 0)) + if ($this->instock !== null && (!is_numeric($this->instock) || (int)$this->instock < 0)) { $this->addError("Outlet: incorrect value in attribute 'instock'"); + } - if ($this->booking !== null && $this->booking !== 'true' && $this->booking !== 'false') + if ($this->booking !== null && $this->booking !== 'true' && $this->booking !== 'false') { $this->addError("Outlet: incorrect value in attribute 'booking'"); + } return empty($this->errors); } diff --git a/src/Offer/Param.php b/src/Offer/Param.php index edbca2b..3ce6fe8 100644 --- a/src/Offer/Param.php +++ b/src/Offer/Param.php @@ -2,11 +2,15 @@ namespace LireinCore\YMLParser\Offer; +use \LireinCore\YMLParser\TYML; +use \LireinCore\YMLParser\TError; + + class Param { - use \LireinCore\YMLParser\TYML; - use \LireinCore\YMLParser\TError; - + use TYML; + use TError; + /** * @var string */ @@ -27,13 +31,15 @@ class Param */ public function isValid() { - if ($this->name === null) + if ($this->name === null) { $this->addError("Param: missing required attribute 'name'"); - elseif (!$this->name) + } elseif (!$this->name) { $this->addError("Param: incorrect value in attribute 'name'"); + } - if (!$this->value) - $this->addError("Param: incorrect value"); + if (!$this->value) { + $this->addError('Param: incorrect value'); + } return empty($this->errors); } diff --git a/src/Offer/SimpleOffer.php b/src/Offer/SimpleOffer.php index 2c66bff..992dbaf 100644 --- a/src/Offer/SimpleOffer.php +++ b/src/Offer/SimpleOffer.php @@ -15,7 +15,7 @@ class SimpleOffer extends AMainOffer public function getAttributesList() { return array_merge(parent::getAttributesList(), [ - //subnodes + //subNodes 'name' ]); } @@ -27,8 +27,9 @@ public function isValid() { $isValid = parent::isValid(); - if ($this->name === null) + if ($this->name === null) { $this->addError("Offer: missing required attribute 'name'"); + } return $isValid && empty($this->errors); } diff --git a/src/Offer/TourOffer.php b/src/Offer/TourOffer.php index 9cf3397..80d66ec 100644 --- a/src/Offer/TourOffer.php +++ b/src/Offer/TourOffer.php @@ -80,7 +80,7 @@ class TourOffer extends AExtOffer public function getAttributesList() { return array_merge(parent::getAttributesList(), [ - //subnodes + //subNodes 'name', 'worldRegion', 'country', 'region', 'days', 'dataTour', 'hotel_stars', 'room', 'meal', 'included', 'transport', 'price_min', 'price_max', 'options' @@ -94,26 +94,32 @@ public function isValid() { $isValid = parent::isValid(); - if ($this->name === null) + if ($this->name === null) { $this->addError("Offer: missing required attribute 'name'"); - - if ($this->days === null) + } + if ($this->days === null) { $this->addError("Offer: missing required attribute 'days'"); - elseif (!is_numeric($this->days) || (int)$this->days <= 0) + } elseif (!is_numeric($this->days) || (int)$this->days <= 0) { $this->addError("Offer: incorrect value in attribute 'days'"); + } - if ($this->included === null) + if ($this->included === null) { $this->addError("Offer: missing required attribute 'included'"); + } - if ($this->transport === null) + if ($this->transport === null) { $this->addError("Offer: missing required attribute 'transport'"); + } - if ($this->priceMin !== null && (!is_numeric($this->priceMin) || (float)$this->priceMin <= 0)) + if ($this->priceMin !== null && (!is_numeric($this->priceMin) || (float)$this->priceMin <= 0)) { $this->addError("Offer: incorrect value in attribute 'price_min'"); + } if ($this->priceMax !== null && (!is_numeric($this->priceMax) || ((float)$this->priceMax <= 0 - || ($this->priceMin !== null && (float)$this->priceMin >= (float)$this->priceMax)))) + || ($this->priceMin !== null && (float)$this->priceMin >= (float)$this->priceMax))) + ) { $this->addError("Offer: incorrect value in attribute 'price_max'"); + } return $isValid && empty($this->errors); } @@ -124,7 +130,7 @@ public function isValid() */ public function addAttribute(array $attrNode) { - if ($attrNode['name'] == 'dataTour') { + if ($attrNode['name'] === 'dataTour') { $this->addDataTour($attrNode['value']); } else { parent::addAttribute($attrNode); @@ -214,7 +220,7 @@ public function setDays($value) */ public function getDataTours() { - return $this->dataTours ? $this->dataTours : null; + return $this->dataTours ?: null; } /** diff --git a/src/Offer/VendorModelOffer.php b/src/Offer/VendorModelOffer.php index 294a27d..0722a95 100644 --- a/src/Offer/VendorModelOffer.php +++ b/src/Offer/VendorModelOffer.php @@ -15,7 +15,7 @@ class VendorModelOffer extends AMainOffer public function getAttributesList() { return array_merge(parent::getAttributesList(), [ - //subnodes + //subNodes 'typePrefix' ]); } @@ -27,11 +27,12 @@ public function isValid() { $isValid = parent::isValid(); - if ($this->model === null) + if ($this->model === null) { $this->addError("Offer: missing required attribute 'model'"); - - if ($this->vendor === null) + } + if ($this->vendor === null) { $this->addError("Offer: missing required attribute 'vendor'"); + } return $isValid && empty($this->errors); } diff --git a/src/Shop.php b/src/Shop.php index 465277d..95d92b4 100644 --- a/src/Shop.php +++ b/src/Shop.php @@ -44,6 +44,11 @@ class Shop */ protected $email; + /** + * @var string + */ + protected $phone; + /** * @var Currency[] */ @@ -81,10 +86,10 @@ class Shop public function getAttributesList() { return [ - //subnodes + //subNodes 'name', 'company', 'url', 'platform', 'version', 'agency', //offers, 'email', 'currencies', 'categories', 'delivery-options', - 'local_delivery_cost', 'cpa', + 'local_delivery_cost', 'cpa', 'phone' ]; } @@ -93,47 +98,63 @@ public function getAttributesList() */ public function isValid() { - if ($this->offersCount == 0) - $this->addError("Shop: no offers"); + if ($this->offersCount === 0) { + $this->addError('Shop: no offers'); + } - if ($this->name === null) + + if ($this->name === null) { $this->addError("Shop: missing required attribute 'name'"); - elseif (!$this->name) + } elseif (!$this->name) { $this->addError("Shop: incorrect value in attribute 'name'"); + } - if ($this->company === null) + if ($this->company === null) { $this->addError("Shop: missing required attribute 'company'"); - elseif (!$this->company) + } elseif (!$this->company) { $this->addError("Shop: incorrect value in attribute 'company'"); + } - if ($this->url === null) + if ($this->url === null) { $this->addError("Shop: missing required attribute 'url'"); - elseif (!$this->url) + } elseif (!$this->url) { $this->addError("Shop: incorrect value in attribute 'url'"); + } - if (!$this->currencies) + if (!$this->currencies) { $this->addError("Shop: missing required attribute 'currencies'"); + } - if (!$this->categories) + if (!$this->categories) { $this->addError("Shop: missing required attribute 'categories'"); + } - 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("Shop: incorrect value in attribute 'local_delivery_cost'"); + } + $subIsValid = true; if ($this->currencies) { foreach ($this->currencies as $currency) { - if (!$currency->isValid()) $subIsValid = false; + if (!$currency->isValid()) { + $subIsValid = false; + } } } if ($this->categories) { foreach ($this->categories as $category) { - if (!$category->isValid()) $subIsValid = false; + if (!$category->isValid()) { + $subIsValid = false; + } } } if ($this->deliveryOptions) { foreach ($this->deliveryOptions as $deliveryOption) { - if (!$deliveryOption->isValid()) $subIsValid = false; + if (!$deliveryOption->isValid()) { + $subIsValid = false; + } } } @@ -145,24 +166,26 @@ public function isValid() */ public function getErrors() { - $errors = $this->errors; + $errors[] = $this->errors; if ($this->currencies) { foreach ($this->currencies as $currency) { - $errors = array_merge($errors, $currency->getErrors()); + $errors[] = $currency->getErrors(); } } if ($this->categories) { foreach ($this->categories as $category) { - $errors = array_merge($errors, $category->getErrors()); + $errors[] = $category->getErrors(); } } if ($this->deliveryOptions) { foreach ($this->deliveryOptions as $deliveryOption) { - $errors = array_merge($errors, $deliveryOption->getErrors()); + $errors[] = $deliveryOption->getErrors(); } } + $errors = count($errors) > 1 ? call_user_func_array('array_merge', $errors) : $errors[0]; + return $errors; } @@ -185,20 +208,22 @@ public function fillShop(array $shopNode) */ public function addAttribute(array $attrNode) { - if ($attrNode['name'] == 'currencies') { + if ($attrNode['name'] === 'currencies') { foreach ($attrNode['nodes'] as $subNode) { $this->addCurrency((new Currency())->addAttributes($subNode['attributes'])); } - } elseif ($attrNode['name'] == 'categories') { + } elseif ($attrNode['name'] === 'categories') { foreach ($attrNode['nodes'] as $subNode) { $this->addCategory((new Category())->addAttributes($subNode['attributes'] + ['name' => $subNode['value']])); } - } elseif ($attrNode['name'] == 'delivery-options') { + } elseif ($attrNode['name'] === 'delivery-options') { foreach ($attrNode['nodes'] as $subNode) { $this->addDeliveryOption((new DeliveryOption())->addAttributes($subNode['attributes'])); } } else { - if (!is_null($attrNode['value'])) $this->addField($attrNode['name'], $attrNode['value']); + if (null !== $attrNode['value']) { + $this->addField($attrNode['name'], $attrNode['value']); + } if (!empty($attrNode['attributes'])) { foreach ($attrNode['attributes'] as $name => $value) { $this->addField($name, $value); @@ -215,8 +240,7 @@ public function addAttribute(array $attrNode) public function getData() { $data = $this->toArray(); - unset($data['errors']); - unset($data['offersCount']); + unset($data['errors'], $data['offersCount']); return $data; } @@ -293,7 +317,6 @@ public function getPlatform() public function setPlatform($value) { $this->platform = $value; - return $this; } @@ -312,7 +335,6 @@ public function getVersion() public function setVersion($value) { $this->version = $value; - return $this; } @@ -331,7 +353,6 @@ public function getAgency() public function setAgency($value) { $this->agency = $value; - return $this; } @@ -350,7 +371,24 @@ public function getEmail() public function setEmail($value) { $this->email = $value; + return $this; + } + + /** + * @return string + */ + public function getPhone() + { + return $this->phone; + } + /** + * @param string $phone + * @return $this + */ + public function setPhone($phone) + { + $this->phone = $phone; return $this; } @@ -368,7 +406,7 @@ public function getCurrency($id) */ public function getCurrencies() { - return $this->currencies ? $this->currencies : null; + return $this->currencies ?: null; } /** @@ -411,8 +449,9 @@ public function getCategory($id) public function getCategoryParent($id) { if (array_key_exists($id, $this->categories) - && !is_null($parentId = $this->categories[$id]->getParentId()) - && array_key_exists($parentId, $this->categories)) { + && null !== ($parentId = $this->categories[$id]->getParentId()) + && array_key_exists($parentId, $this->categories) + ) { return $this->categories[$parentId]; } @@ -445,7 +484,7 @@ public function getCategoryHierarchy($id) */ public function getCategories() { - return $this->categories ? $this->categories : null; + return $this->categories ?: null; } /** @@ -477,7 +516,7 @@ public function addCategory(Category $value) */ public function getDeliveryOptions() { - return $this->deliveryOptions ? $this->deliveryOptions : null; + return $this->deliveryOptions ?: null; } /** @@ -528,7 +567,7 @@ public function setLocalDeliveryCost($value) */ public function getCpa() { - return $this->cpa === null ? null : ($this->cpa === '1' ? true : false); + return $this->cpa === null ? null : ($this->cpa === '1'); } /** @@ -556,7 +595,7 @@ public function getOffersCount() */ public function setOffersCount($value) { - $this->offersCount = $value; + $this->offersCount = (int)$value; return $this; } diff --git a/src/TYML.php b/src/TYML.php index 688c579..cb62d81 100644 --- a/src/TYML.php +++ b/src/TYML.php @@ -2,6 +2,10 @@ namespace LireinCore\YMLParser; +/** + * Trait TYML + * @package LireinCore\YMLParser + */ trait TYML { /** @@ -37,7 +41,9 @@ protected function toArray() $getter = 'get' . $key; if (method_exists($this, $getter)) { $getValue = $this->$getter(); - if ($getValue !== null) $array[$key] = $getValue; + if ($getValue !== null) { + $array[$key] = $getValue; + } } elseif ($value !== null) { $array[$key] = $value; } @@ -53,24 +59,26 @@ protected function toArray() */ protected function getArray($value) { - $subarray = []; + $subArray = []; - foreach ($value as $subkey => $subvalue) { - if (is_object($subvalue) && method_exists($subvalue, 'getData')) { - $subarray[$subkey] = $subvalue->getData(); - } elseif (is_array($subvalue)) { - $subarray[$subkey] = $this->getArray($subvalue); + foreach ($value as $subKey => $subValue) { + if (is_object($subValue) && method_exists($subValue, 'getData')) { + $subArray[$subKey] = $subValue->getData(); + } elseif (is_array($subValue)) { + $subArray[$subKey] = $this->getArray($subValue); } else { - $getter = 'get' . $subkey; + $getter = 'get' . $subKey; if (method_exists($this, $getter)) { $getValue = $this->$getter(); - if ($getValue !== null) $subarray[$subkey] = $getValue; - } elseif ($subvalue !== null) { - $subarray[$subkey] = $subvalue; + if ($getValue !== null) { + $subArray[$subKey] = $getValue; + } + } elseif ($subValue !== null) { + $subArray[$subKey] = $subValue; } } } - return $subarray; + return $subArray; } } \ No newline at end of file diff --git a/src/YML.php b/src/YML.php index 2ce4185..df898fe 100644 --- a/src/YML.php +++ b/src/YML.php @@ -2,6 +2,7 @@ namespace LireinCore\YMLParser; +use LireinCore\Exception\FileNotFoundException; use LireinCore\YMLParser\Offer\VendorModelOffer; use LireinCore\YMLParser\Offer\BookOffer; use LireinCore\YMLParser\Offer\AudioBookOffer; @@ -73,10 +74,10 @@ public function parse($uri, $schema = true) $this->open(); while ($this->read()) { - if ($this->path == 'yml_catalog') { + if ($this->path === 'yml_catalog') { $this->date = $this->XMLReader->getAttribute('date'); while ($this->read()) { - if ($this->path == 'yml_catalog/shop') { + if ($this->path === 'yml_catalog/shop') { $this->shop = $this->parseShop(); break; } @@ -113,11 +114,11 @@ public function getOffers() $this->open(); while ($this->read()) { - if ($this->path == 'yml_catalog/shop/offers') { + if ($this->path === 'yml_catalog/shop/offers') { while ($this->read()) { - if ($this->path == 'yml_catalog/shop/offers/offer') { + if ($this->path === 'yml_catalog/shop/offers/offer') { yield $this->parseOffer(); - } elseif ($this->path == 'yml_catalog/shop') { + } elseif ($this->path === 'yml_catalog/shop') { break; } } @@ -130,6 +131,7 @@ public function getOffers() /** * @return Shop + * @throws \Exception */ protected function parseShop() { @@ -138,11 +140,11 @@ protected function parseShop() $nodes = []; while ($this->read()) { - if ($this->path == 'yml_catalog/shop/offers') { + if ($this->path === 'yml_catalog/shop/offers') { $shop->setOffersCount($this->parseOffersCount()); - } elseif ($xml->nodeType == \XMLReader::ELEMENT) { + } elseif ($xml->nodeType === \XMLReader::ELEMENT) { $nodes[] = $this->parseNode('yml_catalog/shop'); - } elseif ($this->path == 'yml_catalog') { + } elseif ($this->path === 'yml_catalog') { break; } } @@ -154,20 +156,21 @@ protected function parseShop() /** * @return SimpleOffer|VendorModelOffer|BookOffer|AudioBookOffer|ArtistTitleOffer|MedicineOffer|EventTicketOffer|TourOffer + * @throws \Exception */ protected function parseOffer() { $offerNode = $this->parseNode('yml_catalog/shop/offers'); $type = isset($offerNode['attributes']['type']) ? $offerNode['attributes']['type'] : null; - $offer = $this->createOffer($type)->fillOffer($offerNode); + return $this->createOffer($type)->fillOffer($offerNode); - return $offer; } /** * @param string $basePath * @return array + * @throws \Exception */ protected function parseNode($basePath) { @@ -182,16 +185,16 @@ protected function parseNode($basePath) if (!$isEmpty) { while ($this->read()) { - if ($xml->nodeType == \XMLReader::ELEMENT) { + if ($xml->nodeType === \XMLReader::ELEMENT) { $nodes[] = $this->parseNode($path); - } elseif (($xml->nodeType == \XMLReader::TEXT || $xml->nodeType == \XMLReader::CDATA) && $xml->hasValue) { + } elseif (($xml->nodeType === \XMLReader::TEXT || $xml->nodeType === \XMLReader::CDATA) && $xml->hasValue) { $value .= $xml->value; - } elseif ($this->path == $basePath) { + } elseif ($this->path === $basePath) { break; } } } - $value = (trim($value)) ? $value : null; + $value = trim($value) ?: null; return ['name' => $name, 'attributes' => $attributes, 'value' => $value, 'nodes' => $nodes]; } @@ -215,6 +218,7 @@ protected function parseAttributes() /** * @return int + * @throws \Exception */ protected function parseOffersCount() { @@ -222,7 +226,7 @@ protected function parseOffersCount() $count = 0; while ($this->read()) { - if ($this->path == 'yml_catalog/shop/offers/offer') { + if ($this->path === 'yml_catalog/shop/offers/offer') { $count++; break; } @@ -236,23 +240,23 @@ protected function parseOffersCount() } /** - * @throws \Exception + * @throws FileNotFoundException */ protected function open() { $uri = (string)$this->uri; if (!$this->XMLReader->open($uri)) { - throw new \Exception("Failed to open XML file '{$uri}'"); + throw new FileNotFoundException("Failed to open XML file '{$uri}'"); } if (!empty($this->schema)) { $schema = $this->schema; if (!$this->XMLReader->setSchema($schema)) { - throw new \Exception("Failed to open XML Schema file '{$schema}'"); + throw new FileNotFoundException("Failed to open XML Schema file '{$schema}'"); } } } - + protected function close() { $this->pathArr = []; @@ -269,14 +273,13 @@ protected function read() $xml = $this->XMLReader; if ($xml->read()) { - if ($xml->nodeType == \XMLReader::ELEMENT && !$xml->isEmptyElement) { - array_push($this->pathArr, $xml->name); + if ($xml->nodeType === \XMLReader::ELEMENT && !$xml->isEmptyElement) { + $this->pathArr[] = $xml->name; $this->path = implode('/', $this->pathArr); - } elseif ($xml->nodeType == \XMLReader::END_ELEMENT) { + } elseif ($xml->nodeType === \XMLReader::END_ELEMENT) { array_pop($this->pathArr); $this->path = implode('/', $this->pathArr); } - return true; }