diff --git a/src/packages/jbuniversal/jbuniversal/cart-elements/core/element/element.php b/src/packages/jbuniversal/jbuniversal/cart-elements/core/element/element.php index f55389ea..bf4666d5 100644 --- a/src/packages/jbuniversal/jbuniversal/cart-elements/core/element/element.php +++ b/src/packages/jbuniversal/jbuniversal/cart-elements/core/element/element.php @@ -16,7 +16,7 @@ defined('_JEXEC') or die('Restricted access'); use Joomla\String\StringHelper; /** - * Class Element + * Class JBCartElement */ abstract class JBCartElement { @@ -89,9 +89,10 @@ abstract class JBCartElement /** * Constructor - * @param App $app - * @param string $type - * @param string $group + * + * @param App $app + * @param string $type + * @param string $group */ public function __construct($app, $type, $group) { @@ -118,11 +119,13 @@ protected function _loadLangs() /** * Set new config data + * * @param $config */ public function setConfig($config) { - if (is_array($config)) { + if (is_array($config)) + { $config = $this->app->data->create($config); } @@ -149,13 +152,17 @@ public function getElementGroup() /** * Set data through data array. - * @param array $data + * + * @param array $data + * * @return $this */ public function bindData($data = array()) { - if (!empty($data)) { - foreach ($data as $key => $value) { + if (!empty($data)) + { + foreach ($data as $key => $value) + { $this->set($key, $value); } } @@ -165,14 +172,19 @@ public function bindData($data = array()) /** * Gets the elements data - * @param $key - * @param null $default + * + * @param $key + * @param null $default + * * @return mixed + * @since 4.15.7 */ - public function get($key, $default = null) + public function get($key, $default = null): mixed { $result = $this->_data->get($key, $default); - if ($result === null) { + + if ($result === null) + { $result = $default; } @@ -181,8 +193,10 @@ public function get($key, $default = null) /** * Sets the elements data. + * * @param $key * @param $value + * * @return $this */ public function set($key, $value) @@ -203,7 +217,9 @@ public function data() /** * Get element layout path and use override if exists - * @param null|string $layout + * + * @param null|string $layout + * * @return string */ public function getLayout($layout = null) @@ -213,9 +229,12 @@ public function getLayout($layout = null) $group = $this->getElementGroup(); // set default - if (empty($layout)) { + if (empty($layout)) + { $layout = $type . '.php'; - } else if (strpos($layout, '.php') === false) { + } + else if (strpos($layout, '.php') === false) + { $layout .= '.php'; } @@ -223,17 +242,20 @@ public function getLayout($layout = null) $layoutPath = $this->app->path->path("cart-elements:{$group}/{$type}/tmpl/{$layout}"); // parent option - if (empty($layoutPath)) { + if (empty($layoutPath)) + { $layoutPath = $this->app->path->path("cart-elements:{$group}/option/tmpl/{$layout}"); } // parent group - if (empty($layoutPath)) { + if (empty($layoutPath)) + { $layoutPath = $this->app->path->path("cart-elements:core/{$group}/tmpl/{$layout}"); } // global - if (empty($layoutPath)) { + if (empty($layoutPath)) + { $layoutPath = $this->app->path->path("cart-elements:core/element/tmpl/{$layout}"); } @@ -269,7 +291,8 @@ public function getGroup() /** * Set related item object - * @param JBCartOrder $order + * + * @param JBCartOrder $order */ public function setOrder(JBCartOrder $order) { @@ -278,7 +301,9 @@ public function setOrder(JBCartOrder $order) /** * Set related type object - * @param array $params + * + * @param array $params + * * @return bool */ public function hasValue($params = array()) @@ -290,14 +315,17 @@ public function hasValue($params = array()) /** * Renders the element - * @param array $params + * + * @param array $params + * * @return mixed|string */ public function render($params = array()) { $paramLayout = $params->get('layout') ? $params->get('layout') . '.php' : null; - if ($layout = $this->getLayout($paramLayout)) { + if ($layout = $this->getLayout($paramLayout)) + { return $this->renderLayout($layout, array( 'params' => $params, 'value' => $this->get('value'), @@ -309,15 +337,17 @@ public function render($params = array()) } /** - * @param string $layout - * @param array $vars + * @param string $layout + * @param array $vars + * * @return null|string */ protected function _partial($layout, $vars = array()) { $vars['order'] = $this->getOrder(); - if ($layout = $this->getLayout($layout . '.php')) { + if ($layout = $this->getLayout($layout . '.php')) + { return self::renderLayout($layout, $vars); } @@ -327,15 +357,19 @@ protected function _partial($layout, $vars = array()) /** * Renders the element using template layout file - * @param string $__layout layouts template file - * @param array $__args layouts template file args + * + * @param string $__layout layouts template file + * @param array $__args layouts template file args + * * @return string */ protected function renderLayout($__layout, $__args = array()) { // init vars - if (is_array($__args)) { - foreach ($__args as $__var => $__value) { + if (is_array($__args)) + { + foreach ($__args as $__var => $__value) + { $$__var = $__value; } } @@ -374,35 +408,41 @@ public function loadAssets() public function loadConfigAssets() { $this->app->path->register($this->app->path->path('helpers:fields'), 'fields'); + return $this; } /** * Register a callback function + * * @param $method */ public function registerCallback($method) { - if (!in_array(strtolower($method), $this->_callbacks)) { + if (!in_array(strtolower($method), $this->_callbacks)) + { $this->_callbacks[] = strtolower($method); } } /** * Execute elements callback function - * @param $method - * @param array $args + * + * @param $method + * @param array $args */ public function callback($method, $args = array()) { // try to call a elements class method - if (in_array(strtolower($method), $this->_callbacks) && method_exists($this, $method)) { + if (in_array(strtolower($method), $this->_callbacks) && method_exists($this, $method)) + { // call method $res = call_user_func_array(array($this, $method), $args); // output if method returns a string - if (is_string($res)) { + if (is_string($res)) + { echo $res; } } @@ -421,9 +461,11 @@ public function getConfigForm() $params = array(); $class = new ReflectionClass($this); - while ($class !== false) { + while ($class !== false) + { $xmlPath = preg_replace('#\.php$#i', '.xml', $class->getFileName()); - if (file_exists($xmlPath)) { + if (file_exists($xmlPath)) + { $params[] = $xmlPath; } @@ -438,12 +480,14 @@ public function getConfigForm() // skip if there are no config files $params = array_filter($params); - if (empty($params)) { + if (empty($params)) + { return null; } // add config xml files - foreach ($params as $xml) { + foreach ($params as $xml) + { $form->addXML($xml); } @@ -457,7 +501,8 @@ public function getConfigForm() $this->app->jbevent->fire($this, 'cart-element:configform', compact('form')); $fieldsPath = JPath::clean($this->getPath() . '/fields'); - if (is_dir($fieldsPath)) { + if (is_dir($fieldsPath)) + { $form->addElementPath($fieldsPath); } @@ -466,35 +511,39 @@ public function getConfigForm() /** * Get elements xml meta data - * @param null $key + * + * @param null $key + * * @return bool */ public function getMetaData($key = null) { - if (!isset($this->_metaData)) { + if (!isset($this->_metaData)) + { $data = array(); $xml = $this->loadXML(); - if (!$xml) { + if (!$xml) + { return false; } - $data['type'] = $xml->attributes()->type ? (string)$xml->attributes()->type : 'Unknown'; - $data['group'] = $xml->attributes()->group ? (string)$xml->attributes()->group : 'Unknown'; - $data['hidden'] = $xml->attributes()->hidden ? (string)$xml->attributes()->hidden : 'false'; - $data['core'] = $xml->attributes()->core ? (string)$xml->attributes()->core : 'false'; - $data['system-tmpl'] = $xml->attributes()->{'system-tmpl'} ? (string)$xml->attributes()->{'system-tmpl'} : 'false'; - $data['trusted'] = $xml->attributes()->trusted ? (string)$xml->attributes()->trusted : 'false'; - $data['orderable'] = $xml->attributes()->orderable ? (string)$xml->attributes()->orderable : 'false'; - $data['name'] = JText::_((string)$xml->name); - $data['creationdate'] = $xml->creationDate ? (string)$xml->creationDate : 'Unknown'; - $data['author'] = $xml->author ? (string)$xml->author : 'Unknown'; - $data['copyright'] = (string)$xml->copyright; - $data['authorEmail'] = (string)$xml->authorEmail; - $data['authorUrl'] = (string)$xml->authorUrl; - $data['version'] = (string)$xml->version; - $data['description'] = JText::_((string)$xml->description); + $data['type'] = $xml->attributes()->type ? (string) $xml->attributes()->type : 'Unknown'; + $data['group'] = $xml->attributes()->group ? (string) $xml->attributes()->group : 'Unknown'; + $data['hidden'] = $xml->attributes()->hidden ? (string) $xml->attributes()->hidden : 'false'; + $data['core'] = $xml->attributes()->core ? (string) $xml->attributes()->core : 'false'; + $data['system-tmpl'] = $xml->attributes()->{'system-tmpl'} ? (string) $xml->attributes()->{'system-tmpl'} : 'false'; + $data['trusted'] = $xml->attributes()->trusted ? (string) $xml->attributes()->trusted : 'false'; + $data['orderable'] = $xml->attributes()->orderable ? (string) $xml->attributes()->orderable : 'false'; + $data['name'] = JText::_((string) $xml->name); + $data['creationdate'] = $xml->creationDate ? (string) $xml->creationDate : 'Unknown'; + $data['author'] = $xml->author ? (string) $xml->author : 'Unknown'; + $data['copyright'] = (string) $xml->copyright; + $data['authorEmail'] = (string) $xml->authorEmail; + $data['authorUrl'] = (string) $xml->authorUrl; + $data['version'] = (string) $xml->version; + $data['description'] = JText::_((string) $xml->description); $this->_metaData = $this->app->data->create($data); } @@ -508,7 +557,8 @@ public function getMetaData($key = null) */ public function loadXML() { - if (!isset($this->_xmlData)) { + if (!isset($this->_xmlData)) + { $type = $this->getElementType(); $group = $this->getElementGroup(); @@ -521,8 +571,10 @@ public function loadXML() /** * Gets the controle name for given name - * @param $name - * @param bool $array + * + * @param $name + * @param bool $array + * * @return string */ public function getControlName($name, $array = false) @@ -532,7 +584,9 @@ public function getControlName($name, $array = false) /** * Check if element is accessible with users access rights - * @param null $user + * + * @param null $user + * * @return mixed */ public function canAccess($user = null) @@ -556,7 +610,8 @@ public function getPath() public function isSystemTmpl() { $systemTmpl = strtolower($this->getMetaData('system-tmpl')); - if ($systemTmpl == 'true') { + if ($systemTmpl == 'true') + { return true; } @@ -570,7 +625,8 @@ public function isSystemTmpl() public function isCore() { $core = strtolower($this->getMetaData('core')); - if ($core == 'true') { + if ($core == 'true') + { return true; } @@ -584,7 +640,8 @@ public function isCore() public function isHidden() { $hidden = strtolower($this->getMetaData('hidden')); - if ($hidden == 'true' || $hidden == '1') { + if ($hidden == 'true' || $hidden == '1') + { return true; } @@ -593,12 +650,15 @@ public function isHidden() /** * Renders the element in submission - * @param array $params + * + * @param array $params + * * @return string|void */ public function renderSubmission($params = array()) { - if ($layout = $this->getLayout('submission.php')) { + if ($layout = $this->getLayout('submission.php')) + { return self::renderLayout($layout, array( 'params' => $params, 'value' => $this->get('value'), @@ -611,8 +671,10 @@ public function renderSubmission($params = array()) /** * Validates the submitted element + * * @param $value * @param $params + * * @return array */ public function validateSubmission($value, $params) @@ -623,7 +685,7 @@ public function validateSubmission($value, $params) return array( 'value' => $this->app->validator ->create('textfilter', array( - 'required' => (int)$params->get('required'), + 'required' => (int) $params->get('required'), 'trim' => true, )) ->clean($value->get('value')), @@ -637,15 +699,18 @@ public function getName() { $name = $this->config->get('name'); - if (empty($name)) { - $retult = $this->getMetaData('name'); - } else { - $retult = JText::_($name); + if (empty($name)) + { + $result = $this->getMetaData('name'); + } + else + { + $result = JText::_($name); } - $retult = StringHelper::trim($retult); + $result = StringHelper::trim($result); - return $retult; + return $result; } /** @@ -655,12 +720,13 @@ public function getOrderData() { return $this->app->data->create(array( 'data' => $this->data(), - 'config' => (array)$this->config, + 'config' => (array) $this->config, )); } /** - * @param null $default + * @param null $default + * * @return mixed */ public function getDescription($default = null) @@ -669,14 +735,16 @@ public function getDescription($default = null) } /** - * @param bool $uniq + * @param bool $uniq + * * @return string */ public function htmlId($uniq = false) { $id = 'jbcart-' . $this->identifier; - if ($uniq) { + if ($uniq) + { return $this->app->jbstring->getId($id); } @@ -684,8 +752,9 @@ public function htmlId($uniq = false) } /** - * @param string $methodName - * @param array $params + * @param string $methodName + * @param array $params + * * @return mixed */ public function getAjaxUrl($methodName = 'ajax', array $params = array()) @@ -701,6 +770,7 @@ public function getAjaxUrl($methodName = 'ajax', array $params = array()) /** * @param $attrs + * * @return mixed */ protected function _attrs($attrs) @@ -709,14 +779,14 @@ protected function _attrs($attrs) } /** - * @param string $positon + * @param string $positon */ public function saveConfig($positon = JBCart::DEFAULT_POSITION) { $config = JBModelConfig::model(); $list = $config->getGroup('cart.' . $this->getElementGroup()); - $list[$positon][$this->identifier] = (array)$this->config; + $list[$positon][$this->identifier] = (array) $this->config; $config->setGroup('cart.' . $this->getElementGroup(), $list); } @@ -728,4 +798,4 @@ public function saveConfig($positon = JBCart::DEFAULT_POSITION) */ class JBCartElementException extends AppException { -} \ No newline at end of file +} diff --git a/src/packages/jbuniversal/jbuniversal/cart-elements/core/price/price.php b/src/packages/jbuniversal/jbuniversal/cart-elements/core/price/price.php index fed1476a..d467437a 100644 --- a/src/packages/jbuniversal/jbuniversal/cart-elements/core/price/price.php +++ b/src/packages/jbuniversal/jbuniversal/cart-elements/core/price/price.php @@ -122,11 +122,12 @@ abstract class JBCartElementPrice extends JBCartElement protected $_jbhtml; /** + * @param App $app + * @param string $type + * @param string $group + * * @todo args[4] $params ? - * Constructor - * @param App $app - * @param string $type - * @param string $group + * Constructor */ public function __construct($app, $type, $group) { @@ -143,7 +144,8 @@ public function __construct($app, $type, $group) */ public function getJBPrice() { - if (!$this->_jbprice instanceof ElementJBPrice) { + if (!$this->_jbprice instanceof ElementJBPrice) + { throw new JBCartElementPriceException('Item price is not set in ' . get_class($this)); } @@ -152,7 +154,9 @@ public function getJBPrice() /** * Set related price object. - * @param ElementJBPrice $object + * + * @param ElementJBPrice $object + * * @return $this */ public function setJBPrice($object) @@ -164,24 +168,29 @@ public function setJBPrice($object) /** * Set the variant key to which element belongs. - * @param int $key + * + * @param int $key + * * @return $this */ public function setVariant($key) { - $this->variant = (int)$key; + $this->variant = (int) $key; return $this; } /** * Unique string for related price elements and his elements. - * @param string $hash + * + * @param string $hash + * * @return $this */ public function setHash($hash) { - if ($this->hash === null) { + if ($this->hash === null) + { $this->hash = $hash; } @@ -190,6 +199,7 @@ public function setHash($hash) /** * @param $template + * * @return $this */ public function setTemplate($template) @@ -201,6 +211,7 @@ public function setTemplate($template) /** * @param $layout + * * @return $this */ public function setLayout($layout) @@ -211,26 +222,28 @@ public function setLayout($layout) } /** - * @param int $index + * @param int $index + * * @return $this */ public function setIndex($index) { - if(is_numeric($index)) + if (is_numeric($index)) { - $this->index = (int)$index; + $this->index = (int) $index; } return $this; } /** - * @param string $position + * @param string $position + * * @return $this */ public function setPosition($position) { - if(is_string($position)) + if (is_string($position)) { $this->position = $position; } @@ -239,7 +252,8 @@ public function setPosition($position) } /** - * @param boolean $enable + * @param boolean $enable + * * @return $this */ public function setCache($enable) @@ -256,13 +270,13 @@ public function setCache($enable) */ public function isBasic() { - return (bool)($this->variant === 0); + return (bool) ($this->variant === 0); } /** - * @todo Complete method - * Check if element is required. * @return bool + * @todo Complete method + * Check if element is required. */ public function isRequired() { @@ -279,7 +293,9 @@ public function isCache() /** * Check element id. - * @param string $identifier + * + * @param string $identifier + * * @return bool */ public function is($identifier) @@ -299,7 +315,9 @@ public function id() /** * Get an element from the price. * Will be created new instance of JBCartElementPrice. - * @param string $identifier Id of element + * + * @param string $identifier Id of element + * * @return JBCartElement|mixed */ public function getElement($identifier) @@ -323,19 +341,22 @@ public function getHelper() */ public function count() { - return count((array)$this->data()); + return count((array) $this->data()); } /** * Set data through data array. - * @param array|string $data - * @param string $key Default key for $data. If data is not array. + * + * @param array|string $data + * @param string $key Default key for $data. If data is not array. + * * @return $this */ public function bindData($data = array(), $key = 'value') { - if (!is_array($data)) { - $data = array($key => (string)$data); + if (!is_array($data)) + { + $data = array($key => (string) $data); } return parent::bindData($data); @@ -352,7 +373,9 @@ public function getSearchData() /** * Check if element has value. - * @param AppData|array $params + * + * @param AppData|array $params + * * @return bool */ public function hasValue($params = array()) @@ -363,7 +386,8 @@ public function hasValue($params = array()) } /** - * @param array $params + * @param array $params + * * @return bool */ public function hasFilterValue($params = array()) @@ -382,7 +406,9 @@ public function hasOptions() /** * Returns data when variant changes. - * @param AppData|array $params + * + * @param AppData|array $params + * * @return mixed */ public function renderAjax($params = array()) @@ -391,8 +417,9 @@ public function renderAjax($params = array()) } /** - * @param string $html - * @param array $args + * @param string $html + * @param array $args + * * @return string * @throws ElementJBPriceException */ @@ -403,7 +430,9 @@ public function renderWrapper($html = '', $args = array()) /** * Get params for widget - * @param AppData|array $params + * + * @param AppData|array $params + * * @return mixed */ public function interfaceParams($params = array()) @@ -413,15 +442,20 @@ public function interfaceParams($params = array()) /** * Get elements value. If value not set trying to get it from basic variant. - * @param bool $toString A string representation of the value. - * @param string $key Array key. - * @param mixed $default Default value if data is empty. - * @return mixed|string|JBCartValue + * + * @param bool $toString A string representation of the value. + * @param string $key Array key. + * @param mixed $default Default value if data is empty. + * + * @return mixed + * @since 4.15.7 */ public function getValue($toString = false, $key = 'value', $default = null) { $value = $this->get($key); - if ((StringHelper::strlen($value) === 0) && ($this->isCore()) && (!$this->isBasic())) { + + if((StringHelper::strlen($value) === 0) && ($this->isCore()) && (!$this->isBasic())) + { $value = $this->get('_basic', $default); } @@ -470,8 +504,9 @@ public function loadEditAssets() } /** - * @param string $name - * @param boolean $array + * @param string $name + * @param boolean $array + * * @return string */ public function getControlName($name, $array = false) @@ -480,8 +515,9 @@ public function getControlName($name, $array = false) } /** - * @param string $name - * @param bool $array + * @param string $name + * @param bool $array + * * @return string */ public function getRenderName($name, $array = false) @@ -491,13 +527,16 @@ public function getRenderName($name, $array = false) /** * Get unique string for element - * @param bool $unique + * + * @param bool $unique + * * @return string */ public function htmlId($unique = false) { $id = 'jbcart-' . $this->layout . '-' . $this->item_id . '-' . $this->identifier; - if ($unique) { + if ($unique) + { return $this->getId($id); } @@ -514,13 +553,14 @@ public function getConfigForm() } /** - * @todo move to element _value - * Array of prices * @return array + * @todo move to element _value + * Array of prices */ protected function getPrices() { - if ($this->prices !== null) { + if ($this->prices !== null) + { return $this->prices; } @@ -548,10 +588,11 @@ protected function currency() } /** - * @todo Use helper - * @param string $value - * @param array $symbols + * @param string $value + * @param array $symbols + * * @return mixed + * @todo Use helper */ protected function clearSymbols($value, $symbols = array('%', '+', '-')) { @@ -559,7 +600,8 @@ protected function clearSymbols($value, $symbols = array('%', '+', '-')) } /** - * @param string $prefix + * @param string $prefix + * * @return string */ protected function getId($prefix = 'unique-') @@ -569,14 +611,17 @@ protected function getId($prefix = 'unique-') /** * Renders the element using template layout file - * @param string $__layout layouts template file - * @param array $__args layouts template file args + * + * @param string $__layout layouts template file + * @param array $__args layouts template file args + * * @return string */ protected function renderEditLayout($__layout, $__args = array()) { $html = parent::renderLayout($__layout, $__args); - if ($layout = $this->getLayout('_edit.php')) { + if ($layout = $this->getLayout('_edit.php')) + { $html = parent::renderLayout($layout, array( 'html' => $html )); @@ -587,8 +632,10 @@ protected function renderEditLayout($__layout, $__args = array()) /** * Renders the element using template layout file - * @param string $__layout layouts template file - * @param array $__args layouts template file args + * + * @param string $__layout layouts template file + * @param array $__args layouts template file args + * * @return string */ protected function renderLayout($__layout, $__args = array()) @@ -599,18 +646,21 @@ protected function renderLayout($__layout, $__args = array()) } /** - * @param array $assets - * @param string $method + * @param array $assets + * @param string $method + * * @return $this */ protected function addToStorage($assets, $method = 'js') { - $assets = (array)$assets; + $assets = (array) $assets; $count = count($assets); $this->app->jbassets->$method($assets); - if ($this->cache && $count) { - foreach ($assets as $path) { + if ($this->cache && $count) + { + foreach ($assets as $path) + { $this->_addToStorage($path, $method); } } @@ -619,9 +669,10 @@ protected function addToStorage($assets, $method = 'js') } /** - * @todo Use helper - * @param string $file + * @param string $file + * * @return $this|JBCartElementPrice + * @todo Use helper */ protected function less($file) { @@ -629,9 +680,10 @@ protected function less($file) } /** - * @todo Use helper - * @param string $file + * @param string $file + * * @return $this|JBCartElementPrice + * @todo Use helper */ protected function js($file) { @@ -639,9 +691,10 @@ protected function js($file) } /** - * @todo Use helper - * @param string $file + * @param string $file + * * @return $this|JBCartElementPrice + * @todo Use helper */ protected function css($file) { @@ -649,7 +702,8 @@ protected function css($file) } /** - * @param string $asset + * @param string $asset + * * @return $this * @throws JBCartElementPriceException */ @@ -661,8 +715,9 @@ protected function _addToStorage($asset) } /** - * @param string $html - * @param array $args + * @param string $html + * @param array $args + * * @return string * @throws ElementJBPriceException */ diff --git a/src/packages/jbuniversal/jbuniversal/elements/jbprice/jbprice.php b/src/packages/jbuniversal/jbuniversal/elements/jbprice/jbprice.php index de91e07e..76ef859a 100644 --- a/src/packages/jbuniversal/jbuniversal/elements/jbprice/jbprice.php +++ b/src/packages/jbuniversal/jbuniversal/elements/jbprice/jbprice.php @@ -92,7 +92,7 @@ abstract class ElementJBPrice extends Element implements iSubmittable */ protected $_layout; - const BASIC_VARIANT = 0; + const BASIC_VARIANT = 0; const SIMPLE_PARAM_LENGTH = 36; /** @@ -110,15 +110,16 @@ public function __construct() $this->_position = $this->app->jbcartposition; - $this->_helper = $this->app->jbprice; - $this->_cache = $this->app->jbcache; + $this->_helper = $this->app->jbprice; + $this->_cache = $this->app->jbcache; $this->_storage = $this->app->jbstorage; } public function getSearchData() { return array_reduce($this->getList()->all(), function ($acc, $item) { - if ($skuElem = $item->get('_sku')) { + if ($skuElem = $item->get('_sku')) + { $acc .= "\n" . $skuElem->getValue(); } @@ -128,25 +129,28 @@ public function getSearchData() /** * Set related type object. - * @param Type $type + * + * @param Type $type */ public function setType($type) { parent::setType($type); - $this->cache = (bool)($this->config->get('cache', 0) && $this->canAccess()); - $this->showAll = (bool)!$this->config->get('only_selected', 0); + $this->cache = (bool) ($this->config->get('cache', 0) && $this->canAccess()); + $this->showAll = (bool) !$this->config->get('only_selected', 0); } /** * Check if elements value is set - * @param array|AppData $params + * + * @param array|AppData $params + * * @return bool */ public function hasValue($params = []) { - $params = new AppData($params); + $params = new AppData($params); $template = $params->get('template', 'default'); $config = $this->setTemplate($template)->getParameters($template); @@ -155,20 +159,24 @@ public function hasValue($params = []) } /** - * @param array $params + * @param array $params + * * @return null|string */ public function edit($params = []) { $config = $this->getConfigs(); - if (!empty($config)) { - if ($layout = $this->getLayout('edit.php')) { + if (!empty($config)) + { + if ($layout = $this->getLayout('edit.php')) + { $this->loadEditAssets(); $variations = $this->get('variations', [0 => []]); - $renderer = $this->app->jbrenderer->create('jbprice'); + $renderer = $this->app->jbrenderer->create('jbprice'); - if (count($variations) === 1) { + if (count($variations) === 1) + { $variations[count($variations)] = []; } $hash = $this->getHash($params); @@ -193,22 +201,25 @@ public function edit($params = []) /** * Renders the element. - * @param array|AppData $params Element render parameters + * + * @param array|AppData $params Element render parameters + * * @return string|null */ public function render($params = []) { $params = new AppData($params); - $hash = $this->setTemplate($params->get('template', 'default'))->getHash($params); + $hash = $this->setTemplate($params->get('template', 'default'))->getHash($params); $this->loadAssets(); - if (!$this->cache || ($this->cache && !$cache = $this->_cache->get($hash, 'price_elements', true))) { + if (!$this->cache || ($this->cache && !$cache = $this->_cache->get($hash, 'price_elements', true))) + { - $template = $this->getTemplate(); + $template = $this->getTemplate(); $this->_layout = $params->get('_layout'); $renderer = $this->app->jbrenderer->create('jbprice'); - $variant = $this->getList()->current(); + $variant = $this->getList()->current(); $data = $renderer->render($template, [ '_variant' => $variant, @@ -221,7 +232,8 @@ public function render($params = []) //Must be after renderer $elements = $this->elementsInterfaceParams(); - if ($layout = $this->getLayout('render.php')) { + if ($layout = $this->getLayout('render.php')) + { return $this->renderLayout($layout, [ 'hash' => $hash, 'data' => $data, @@ -239,26 +251,30 @@ public function render($params = []) /** * Render submission - * @param array $params + * + * @param array $params + * * @return null|string */ public function renderSubmission($params = []) { $this->toStorage('elements:jbprice/assets/less/submission.less'); - return $this->edit((array)$params); + return $this->edit((array) $params); } /** - * @param string $template - * @param string $message + * @param string $template + * @param string $message + * * @return string */ public function renderWarning($template = '_warning.php', $message = '') { - if (!$this->app->jbenv->isSite()) { + if (!$this->app->jbenv->isSite()) + { - $link = $this->app->jbrouter->admin([ + $link = $this->app->jbrouter->admin([ 'controller' => 'jbcart', 'task' => 'price', 'element' => $this->identifier, @@ -270,7 +286,8 @@ public function renderWarning($template = '_warning.php', $message = '') $message = '' . $message . ''; } - if (($layout = $this->getLayout($template)) && !$this->_helper->isEmpty($message)) { + if (($layout = $this->getLayout($template)) && !$this->_helper->isEmpty($message)) + { return parent::renderLayout($layout, [ 'message' => $message, ]); @@ -281,14 +298,17 @@ public function renderWarning($template = '_warning.php', $message = '') /** * Renders the element using template layout file. - * @param $__layout - * @param array $__args + * + * @param $__layout + * @param array $__args + * * @return string */ public function renderLayout($__layout, $__args = []) { $html = parent::renderLayout($__layout, $__args); - if ($this->cache) { + if ($this->cache) + { $storage = $this->_storage->get('assets', $this->hash, ''); $this->_cache->set($this->hash, $html, 'price_elements', true); @@ -300,25 +320,31 @@ public function renderLayout($__layout, $__args = []) /** * Validate submission + * * @param $value * @param $params + * * @return mixed * @throws AppValidatorException */ public function validateSubmission($value, $params) { $this->app->validator; - if ((int)$params->get('required', 0)) { + if ((int) $params->get('required', 0)) + { $variations = array_filter($value->get('variations')); - $list = $this->getList($variations); + $list = $this->getList($variations); - if ($list->count() === 0) { + if ($list->count() === 0) + { throw new AppValidatorException('This field is required'); } - foreach ($list as $variant) { - if (count($variant->data()) === 0) { + foreach ($list as $variant) + { + if (count($variant->data()) === 0) + { throw new AppValidatorException('This field is required'); } } @@ -333,7 +359,8 @@ public function validateSubmission($value, $params) */ public function clearList() { - if ($this->_list !== null) { + if ($this->_list !== null) + { $this->_list->clear(); }; $this->_list = null; @@ -351,27 +378,32 @@ public function hasList() } /** - * @param array $data - * @param array $options + * @param array $data + * @param array $options + * * @return JBCartVariantList */ public function getList($data = [], $options = []) { - $data = (array)$data; + $data = (array) $data; $hasData = !empty($data); - if ($hasData || !$this->hasList()) { + if ($hasData || !$this->hasList()) + { $data = $hasData ? $data : $this->defaultData(); // If basic variant not exists, add him. - if ($hasData && !array_key_exists(self::BASIC_VARIANT, $data)) { + if ($hasData && !array_key_exists(self::BASIC_VARIANT, $data)) + { $data[self::BASIC_VARIANT] = $this->getData(self::BASIC_VARIANT); } - if (!array_key_exists('selected', $options)) { + if (!array_key_exists('selected', $options)) + { $options['selected'] = $this->get('values.' . $this->defaultKey()); } - if (!array_key_exists('values', $options)) { + if (!array_key_exists('values', $options)) + { $options['values'] = $this->getValues($options['selected']); } // Create variants objects. @@ -384,7 +416,8 @@ public function getList($data = [], $options = []) $this->createList($data, $options); - if ($this->_template === null) { + if ($this->_template === null) + { //$this->clearList(); // fatal on edit and submission } } @@ -394,17 +427,20 @@ public function getList($data = [], $options = []) /** * Build array of JBCartVariant instances from item data - * @param array $variations - * @param array $options + * + * @param array $variations + * @param array $options + * * @return array */ public function prepareList(array $variations, $options = []) { ksort($variations, SORT_NUMERIC); - $list = []; - $params = array_merge((array)$this->getConfigs(), (array)$this->getParameters()); - foreach ($variations as $id => $data) { + $list = []; + $params = array_merge((array) $this->getConfigs(), (array) $this->getParameters()); + foreach ($variations as $id => $data) + { $list[$id] = $this->doVariant(array_keys($params), [ 'id' => $id, 'data' => $data, @@ -416,14 +452,16 @@ public function prepareList(array $variations, $options = []) /** * Build JBCartVariant instance from items data - * @param array $data Array of price elements data. - * @param int $id Variant id + * + * @param array $data Array of price elements data. + * @param int $id Variant id + * * @return JBCartVariant */ public function buildVariant($data = [], $id = self::BASIC_VARIANT) { $elements = array_merge($this->getConfigs(), $this->getParameters()); - $variant = $this->doVariant(array_keys($elements), [ + $variant = $this->doVariant(array_keys($elements), [ 'id' => $id, 'data' => $data, ]); @@ -432,8 +470,9 @@ public function buildVariant($data = [], $id = self::BASIC_VARIANT) } /** - * @param array $ids - * @param array $options + * @param array $ids + * @param array $options + * * @return mixed */ public function doVariant(array $ids, array $options = []) @@ -441,7 +480,7 @@ public function doVariant(array $ids, array $options = []) $options['id'] = isset($options['id']) ? $options['id'] : self::BASIC_VARIANT; $elements = $this->_getElements($ids, $options['id']); - $variant = $this->_storage->create('variant', [ + $variant = $this->_storage->create('variant', [ 'elements' => $elements, 'options' => $options, ]); @@ -450,7 +489,8 @@ public function doVariant(array $ids, array $options = []) } /** - * @param array $options + * @param array $options + * * @return array */ public function itemOptions(array $options = []) @@ -466,7 +506,7 @@ public function itemOptions(array $options = []) 'cache' => $this->cache, 'showAll' => $this->showAll, 'default' => $this->_item ? $this->defaultKey() : self::BASIC_VARIANT, - ], (array)$options); + ], (array) $options); } /** @@ -477,8 +517,9 @@ public function key() $args = func_get_args(); $type = array_shift($args); - if (method_exists($this, "get{$type}key")) { - return call_user_func_array([$this, "get{$type}key"], (array)$args); + if (method_exists($this, "get{$type}key")) + { + return call_user_func_array([$this, "get{$type}key"], (array) $args); } return null; @@ -495,8 +536,10 @@ public function data() /** * Get variation data. - * @param int $key Index/key of variant. - * @param mixed $default Default value + * + * @param int $key Index/key of variant. + * @param mixed $default Default value + * * @return mixed */ public function getData($key, $default = null) @@ -506,17 +549,20 @@ public function getData($key, $default = null) /** * Get elements default data. - * Default data are consist of the basic variant @see ElementJBPrice::BASIC_VARIANT - * and default variant @see ElementJBPrice::defaultKey() - * + * Default data are consist of the basic variant + * @see ElementJBPrice::defaultKey() + * @see ElementJBPrice::BASIC_VARIANT + * and default variant * @return array + * @since 4.15.7 */ - public function defaultData() + public function defaultData(): array { - $list = [$this->getData(self::BASIC_VARIANT)]; + $list = [$this->getData(self::BASIC_VARIANT)]; $default = $this->defaultKey(); - if ($default !== self::BASIC_VARIANT) { + if ($default !== self::BASIC_VARIANT) + { $list[$default] = $this->getData($default); } @@ -529,21 +575,25 @@ public function defaultData() */ public function defaultKey() { - return $this->_item ? (int)$this->get('default_variant', self::BASIC_VARIANT) : self::BASIC_VARIANT; + return $this->_item ? (int) $this->get('default_variant', self::BASIC_VARIANT) : self::BASIC_VARIANT; } /** * Set new variant as default. - * @param int|array $key Number of variant. + * + * @param int|array $key Number of variant. + * * @return $this */ public function setDefault($key) { $old = $this->defaultKey(); - if ($old !== (int)$key) { + if ($old !== (int) $key) + { $this->set('default_variant', $key); - if ($this->_list instanceof JBCartVariantList) { + if ($this->_list instanceof JBCartVariantList) + { $this->_list->setDefault($key); } } @@ -552,49 +602,54 @@ public function setDefault($key) } /** - * @param array $template - * @param array $values + * @param array $template + * @param array $values */ abstract public function ajaxChangeVariant($template = ['default'], $values = []); /** * Ajax add to cart method - * @param array $template - * @param int $quantity - * @param array $values + * + * @param array $template + * @param int $quantity + * @param array $values + * * @return */ abstract public function ajaxAddToCart($template = ['default'], $quantity = 1, $values = []); /** - * @param string $template Template to render - * @param string $layout Current price layout - * @param string $hash Hash string for communication between the elements in/out modal window + * @param string $template Template to render + * @param string $layout Current price layout + * @param string $hash Hash string for communication between the elements in/out modal window */ abstract public function ajaxModalWindow($hash, $template = 'default', $layout = 'default'); /** * Remove from cart method - * @param string|bool $key + * + * @param string|bool $key + * * @return mixed */ abstract public function ajaxRemoveFromCart($key = null); /** * Render default variant when he changes. - * Calling this method after calculation in @see ElementJBPrice::ajacChangeVariant() - * @return array + * Calling this method after calculation in @return array + * @see ElementJBPrice::ajacChangeVariant() */ protected function renderVariant() { $result = []; $parameters = $this->loadParams(); - $variant = $this->_list->current(); - $storage = $this->_storage; + $variant = $this->_list->current(); + $storage = $this->_storage; array_walk($parameters, function ($params) use ($variant, $storage, &$result) { - if (($element = $variant->get($params['identifier'])) && $element->isCore()) { - $params = new AppData($params); + if (($element = $variant->get($params['identifier'])) && $element->isCore()) + { + $params = new AppData($params); $options = [ 'template' => $params->get('_template'), 'position' => $params->get('_position'), @@ -606,7 +661,8 @@ protected function renderVariant() $data = $element->renderAjax($params); //return data if not null - if ($data !== null) { + if ($data !== null) + { $key = strtolower('jselement' . implode($options)); $result[$params->get('type')][$key] = $data; @@ -626,9 +682,11 @@ public function elementsInterfaceParams() $options = []; $parameters = $this->loadParams(); - $variant = $this->_list->current(); - foreach ($parameters as $params) { - if (($element = $variant->get($params['identifier'])) && $element->isCore()) { + $variant = $this->_list->current(); + foreach ($parameters as $params) + { + if (($element = $variant->get($params['identifier'])) && $element->isCore()) + { $params = new AppData($params); $element = $this->_storage->configure($element, [ @@ -646,7 +704,9 @@ public function elementsInterfaceParams() /** * Get missing elements names. + * * @param $values + * * @return array */ protected function getMissing($values) @@ -672,17 +732,20 @@ protected function getRequired() } /** - * @param array $values + * @param array $values + * * @return mixed */ public function getValues($values = []) { $result = []; - $values = (array)$values; + $values = (array) $values; - foreach ($values as $key => $value) { - if (($element = $this->getElement($key)) && !$element->isCore()) { - $name = $element->bindData($value)->getName(); + foreach ($values as $key => $value) + { + if (($element = $this->getElement($key)) && !$element->isCore()) + { + $name = $element->bindData($value)->getName(); $result[$name] = $element->getValue(true); } } @@ -691,12 +754,13 @@ public function getValues($values = []) } /** - * @param string $id + * @param string $id + * * @return array */ public function elementOptions($id) { - return (array)$this->get('selected.' . $id, []); + return (array) $this->get('selected.' . $id, []); } /** @@ -710,14 +774,18 @@ public function getTemplate() /** * Set protected property value. - * @param $template - property value - * @deprecated + * + * @param $template - property value + * * @return ElementJBPrice + * @deprecated */ public function setTemplate($template) { - if ($this->_template !== $template && !$this->_helper->isEmpty($template)) { - if ($this->_template !== null) { + if ($this->_template !== $template && !$this->_helper->isEmpty($template)) + { + if ($this->_template !== null) + { $this->_list = $this->hash = $this->_parameters = null; } $this->_template = $template; @@ -732,16 +800,18 @@ public function setTemplate($template) */ public function getIndexData() { - $variations = (array)$this->get('variations', []); - $indexData = []; - if (count($variations)) { + $variations = (array) $this->get('variations', []); + $indexData = []; + if (count($variations)) + { $list = $this->getList($variations); unset($variations); $oldKey = $this->defaultKey(); /** @type JBCartVariant $variant */ - foreach ($list->all() as $variant) { + foreach ($list->all() as $variant) + { $this->setDefault($variant->getId()); - $indexData = array_merge((array)$indexData, $this->getVariantData($variant)); + $indexData = array_merge((array) $indexData, $this->getVariantData($variant)); } $this->setDefault($oldKey); $list->current()->setId(-1); @@ -753,22 +823,27 @@ public function getIndexData() } /** - * @param JBCartVariant $variant + * @param JBCartVariant $variant + * * @return array */ protected function getVariantData(JBCartVariant $variant) { $vars = $this->app->jbvars; $data = []; - foreach ($variant->all() as $element) { + foreach ($variant->all() as $element) + { $value = $element->getSearchData(); $value = $this->_helper->getValue($value); - - if (NULL === $value) { $value = 0;} - $string = (string)$value; - + + if (null === $value) + { + $value = 0; + } + $string = (string) $value; + // $numeric = is_numeric($value) ? $vars->number($value) : null; - $numeric = is_numeric($value) ? $vars->number($value) : (float)$value; + $numeric = is_numeric($value) ? $vars->number($value) : (float) $value; // $date = $this->_helper->isDate($value) ?: null; // todoj4fix $date = $this->_helper->isDate($value) ?: '1970-01-01 00:00:00'; @@ -776,9 +851,10 @@ protected function getVariantData(JBCartVariant $variant) if ( (!$this->_helper->isEmpty($string) || (is_numeric($numeric) || !$this->_helper->isEmpty($date))) || ($element->id() == '_value') - ) { + ) + { $key = $this->_item->id . '__' . $this->identifier . '__' . $variant->getId() . '__' . $element->id(); - + $data[$key] = [ 'item_id' => $this->_item->id, 'element_id' => $this->identifier, @@ -796,8 +872,10 @@ protected function getVariantData(JBCartVariant $variant) /** * Get control name - * @param string $id - * @param bool $array + * + * @param string $id + * @param bool $array + * * @return string */ public function getControlName($id, $array = false) @@ -807,7 +885,9 @@ public function getControlName($id, $array = false) /** * Get element layout path and use override if exists - * @param string|null $layout + * + * @param string|null $layout + * * @return string */ public function getLayout($layout = null) @@ -816,31 +896,36 @@ public function getLayout($layout = null) $type = $this->getElementType(); // set default - if (!$layout) { + if (!$layout) + { $layout = $type . '.php'; } $path = $this->app->path->path('elements:' . $type . '/tmpl/' . $layout); - if (!$path) { + if (!$path) + { $parent = strtolower(str_replace('Element', '', get_parent_class($this))); - $path = $this->app->path->path('elements:' . $parent . '/tmpl/' . $layout); + $path = $this->app->path->path('elements:' . $parent . '/tmpl/' . $layout); } return $path; } /** - * @param string $id Identifier of element - * @param int|string $variant Variant index/key + * @param string $id Identifier of element + * @param int|string $variant Variant index/key + * * @return JBCartElementPrice */ public function getElement($id, $variant = self::BASIC_VARIANT) { // has element already been loaded? - if (!$element = ($this->_storage->hasElement($id) ? $this->_storage->getElement($id) : null)) { - if ($config = $this->getElementConfig($id)) { + if (!$element = ($this->_storage->hasElement($id) ? $this->_storage->getElement($id) : null)) + { + if ($config = $this->getElementConfig($id)) + { $group = $config->get('group'); - $type = $config->get('type'); + $type = $config->get('type'); if ($element = $this->_storage->create('element', [ 'app' => $this->app, @@ -851,16 +936,22 @@ public function getElement($id, $variant = self::BASIC_VARIANT) 'class' => 'JBCartElement' . $group . $type, 'variant' => $variant, ]) - ) { - } else { + ) + { + } + else + { return false; } - } else { + } + else + { return false; } } - if (!$element->canAccess()) { + if (!$element->canAccess()) + { return false; } @@ -882,36 +973,42 @@ public function getElement($id, $variant = self::BASIC_VARIANT) */ public function getElements() { - return $this->_getElements(array_keys(array_diff_key((array)$this->getConfigs(), - (array)$this->getParameters()))); + return $this->_getElements(array_keys(array_diff_key((array) $this->getConfigs(), + (array) $this->getParameters()))); } /** * @param $id + * * @return AppData|bool */ public function getElementConfig($id) { - if ($config = $this->_storage->getConfig($this->key('public'), $id)) { + if ($config = $this->_storage->getConfig($this->key('public'), $id)) + { return new AppData($config); } $parameter = $this->getParameter($id); - return (int)$parameter->get('system', 0) ? $parameter : false; + return (int) $parameter->get('system', 0) ? $parameter : false; } /** - * @param $ids - * @param int $key + * @param $ids + * @param int $key + * * @return array */ public function _getElements($ids, $key = self::BASIC_VARIANT) { $elements = []; - if ($ids) { - foreach ($ids as $id) { - if ($element = $this->getElement($id, $key)) { + if ($ids) + { + foreach ($ids as $id) + { + if ($element = $this->getElement($id, $key)) + { $elements[$id] = $element; } } @@ -929,23 +1026,27 @@ public function _getElements($ids, $key = self::BASIC_VARIANT) public function currency() { $default = JBCart::val()->cur(); - $params = $this->getParameter('_currency'); + $params = $this->getParameter('_currency'); $currencyList = $params->get('currency_list', []); - if (count($currencyList) == 1 && !in_array('all', $currencyList)) { + if (count($currencyList) == 1 && !in_array('all', $currencyList)) + { reset($currencyList); $key = current($currencyList); + return $key; } - if ((array)$params) { + if ((array) $params) + { return $params->get('currency_default', $default); } $variant = $this->getList()->current(); - if ($variant->has('_value')) { - return $variant->getValue(false, '_value')->cur(); + if ($variant->has('_value')) + { + return $variant->getValue('_value')?->cur(); } return $default; @@ -953,13 +1054,16 @@ public function currency() /** * Is in stock item. - * @param float|int $quantity - * @param int $variant + * + * @param float|int $quantity + * @param int $variant + * * @return bool */ public function inStock($quantity, $variant = self::BASIC_VARIANT) { - if ($this->getVariant($variant)->inStock($quantity)) { + if ($this->getVariant($variant)->inStock($quantity)) + { return true; } @@ -968,35 +1072,44 @@ public function inStock($quantity, $variant = self::BASIC_VARIANT) /** * Get balance from variant. + * * @param $key - * @return mixed + * + * @return JBCartValue + * @throws JBCartVariantListException + * @since 4.15.7 */ - public function getBalance($key) + public function getBalance($key): JBCartValue { - return $this->getVariant($key)->getValue(true, '_balance', false); + return $this->getVariant($key)->getValue('_balance', true, false); } /** * Bind and validate data. - * @param array $data + * + * @param array $data */ public function bindData($data = []) { - if ($this->_item !== null) { + if ($this->_item !== null) + { $hashTable = []; - if ($this->app->jbenv->isSite()) { + if ($this->app->jbenv->isSite()) + { $this->_item->elements->set($this->identifier, []); } - if (array_key_exists('variations', $data)) { + if (array_key_exists('variations', $data)) + { $list = $this->prepareList($data['variations']); unset($data['variations']); // generate hashes - $values = (array)$this->get('values', []); - if ($values) { + $values = (array) $this->get('values', []); + if ($values) + { $hashTable = array_map(function ($array) { asort($array); @@ -1017,76 +1130,87 @@ public function bindData($data = []) $list = array_intersect_key($list, $hashTable); //generate array values and selected - if (count($list)) { - foreach ($list as $key => $variant) { + if (count($list)) + { + foreach ($list as $key => $variant) + { $variant->setId($key)->bindData(); $this->bindVariant($variant); } } - if (isset($data['default_variant']) && !array_key_exists($data['default_variant'], $list)) { + if (isset($data['default_variant']) && !array_key_exists($data['default_variant'], $list)) + { unset($data['default_variant']); } } - if (count($data)) { + if (count($data)) + { $result = $this->_item->elements->get($this->identifier); - foreach ($data as $_id => $unknown) { + foreach ($data as $_id => $unknown) + { $result[$_id] = is_string($unknown) ? StringHelper::trim($unknown) : $unknown; } - $this->_item->elements->set($this->identifier, (array)$result); + $this->_item->elements->set($this->identifier, (array) $result); } } } /** - * @param JBCartVariant $variant + * @param JBCartVariant $variant + * * @return $this */ public function bindVariant(JBCartVariant $variant) { - if ($this->_item !== null) { + if ($this->_item !== null) + { $simple = $variant->getSimple(); - $data = $this->data(); + $data = $this->data(); - $values = (array)$data->get('values', []); - $selected = (array)$data->get('selected', []); - $variations = (array)$data->get('variations', []); + $values = (array) $data->get('values', []); + $selected = (array) $data->get('selected', []); + $variations = (array) $data->get('variations', []); $variations[$variant->getId()] = $variant->data(); - if (!$variant->isBasic()) { + if (!$variant->isBasic()) + { $values[$variant->getId()] = array_filter(array_map(function ($element) { - return StringHelper::strlen($element->getValue(true)) > 0 ? (array)$element->data() : null; + return StringHelper::strlen($element->getValue(true)) > 0 ? (array) $element->data() : null; }, $simple)); $_selected = array_filter(array_map(function ($element) { return StringHelper::strlen($element->getValue(true)) > 0 ? StringHelper::trim($element->getValue(true)) : null; }, $simple)); - if ($_selected) { - foreach ($_selected as $key => $value) { + if ($_selected) + { + foreach ($_selected as $key => $value) + { $selected[$key][$value] = $value; } } } // Reset keys $variations = array_values($variations); - $values = array_values($values); + $values = array_values($values); $data->set('variations', $variations); $data->set('selected', $selected); $data->set('values', $variant->isBasic() ? [self::BASIC_VARIANT => []] : $values); - $this->_item->elements->set($this->identifier, (array)$data); + $this->_item->elements->set($this->identifier, (array) $data); } return $this; } /** - * @param string $path + * @param string $path + * * @return bool */ public function toStorage($path) @@ -1110,7 +1234,8 @@ public function loadEditAssets() 'jbassets:less/admin.less', 'elements:jbprice/assets/less/edit.less', ]); - if ((int)$this->config->get('mode', 1)) { + if ((int) $this->config->get('mode', 1)) + { $jbAssets->js('jbassets:js/admin/validator.js'); } $jbAssets->js('elements:jbprice/assets/js/edit.js'); @@ -1133,11 +1258,15 @@ public function loadAssets() $jbAssets->fancybox(); //Add assets from cache - if ($this->cache) { + if ($this->cache) + { $assets = $this->_cache->get($this->hash, 'price_assets', true); - if (!empty($assets)) { - foreach ($assets as $asset) { - if ($ext = JFile::getExt($asset)) { + if (!empty($assets)) + { + foreach ($assets as $asset) + { + if ($ext = JFile::getExt($asset)) + { $jbAssets->$ext($asset); } } @@ -1157,16 +1286,20 @@ public function loadAssets() } /** - * @param int $id + * @param int $id + * * @return JBCartVariant|mixed * @throws JBCartVariantListException */ public function getVariant($id = self::BASIC_VARIANT) { - try { + try + { $variant = $this->getList()->get($id); - } catch (JBCartVariantListException $e) { - $ids = array_keys(array_merge((array)$this->getConfigs(), (array)$this->getParameters())); + } + catch (JBCartVariantListException $e) + { + $ids = array_keys(array_merge((array) $this->getConfigs(), (array) $this->getParameters())); $variant = $this->doVariant($ids, [ 'id' => $id, 'data' => $this->getData($id), @@ -1180,12 +1313,14 @@ public function getVariant($id = self::BASIC_VARIANT) /** * Load element renderer params. - * @param string $id Element id. + * + * @param string $id Element id. + * * @return AppData */ public function getParameter($id) { - $params = (array)$this->getParameters(); + $params = (array) $this->getParameters(); return new AppData(isset($params[$id]) ? $params[$id] : []); } @@ -1200,28 +1335,32 @@ protected function getConfigs() } /** - * @todo If template is not set && JDEBUG === 1 throw exception - * @param string|null $template + * @param string|null $template + * * @return array * @throws ElementJBPriceException + * @todo If template is not set && JDEBUG === 1 throw exception */ protected function getParameters($template = '') { - if ($this->getTemplate() === null) { + if ($this->getTemplate() === null) + { //throw new ElementJBPriceException('Template is not set.'); return []; } - if ($template === '') { + if ($template === '') + { $parameters = $this->loadParams(); - } else { - $access = $this->getPrivateKey($this->getTemplate()); + } + else + { + $access = $this->getPrivateKey($this->getTemplate()); $parameters = $this->_storage->get('parameters', $access, []); } - $parameters = $this->app->jbarray->index($parameters, 'identifier'); - return $parameters; + return $this->app->jbarray->index($parameters, 'identifier'); } /** @@ -1230,12 +1369,15 @@ protected function getParameters($template = '') */ protected function loadParams() { - if ($this->_parameters === null) { - $templates = (array)$this->_template; + if ($this->_parameters === null) + { + $templates = (array) $this->_template; $parameters = []; - foreach ($templates as $template) { + foreach ($templates as $template) + { $params = $this->_storage->get('parameters', $this->getPrivateKey($template), []); - if ($params) { + if ($params) + { array_walk($params, function ($parameter) use (&$parameters) { $parameters[] = $parameter; }); @@ -1249,26 +1391,28 @@ protected function loadParams() } /** - * @param array|AppData $params + * @param array|AppData $params + * * @return string */ protected function getHash($params = []) { - if ($this->hash !== null) { + if ($this->hash !== null) + { return $this->hash; } $this->hash = $this->encrypt([ $this->identifier, $this->getItem()->id, - (array)$params, - (array)$this->getItem()->elements->get($this->identifier), - (array)$this->getConfigs(), - (array)$this->getParameters($this->getTemplate()), - (array)$this->config, + (array) $params, + (array) $this->getItem()->elements->get($this->identifier), + $this->getConfigs(), + $this->getParameters($this->getTemplate()), + (array) $this->config, $this->_template, $this->_layout, - (array)JFactory::getUser()->groups, + JFactory::getUser()->groups, ]); return $this->hash; @@ -1277,8 +1421,8 @@ protected function getHash($params = []) /** * Create variant list object and set to the price property. * - * @param array $list Array of variant list objects* - * @param array $options By default item and price options + * @param array $list Array of variant list objects* + * @param array $options By default item and price options * * @return JBCartVariantList * @throws ElementJBPriceException @@ -1293,13 +1437,15 @@ protected function createList($list, $options) } /** - * @param JBCartVariantList $list + * @param JBCartVariantList $list + * * @return $this * @throws ElementJBPriceException */ protected function setList($list) { - if (!$list instanceof JBCartVariantList) { + if (!$list instanceof JBCartVariantList) + { throw new ElementJBPriceException('Type of variable $list in method - ' . __FUNCTION__ . ' is not correct.'); } @@ -1317,7 +1463,8 @@ protected function getPublicKey() } /** - * @param string $template + * @param string $template + * * @return string */ protected function getPrivateKey($template) @@ -1337,12 +1484,13 @@ protected function getSessionKey() } /** - * @param array $data + * @param array $data + * * @return string */ private function encrypt(array $data = []) { - return md5(serialize((array)$data)); + return md5(serialize((array) $data)); } /** @@ -1375,15 +1523,15 @@ class ElementJBPriceException extends ElementException protected $isAjax; /** - * @param string $message - * @param int $code - * @param Exception $previous + * @param string $message + * @param int $code + * @param Exception $previous */ public function __construct($message = '', $code = 0, Exception $previous = null) { parent::__construct($message, $code, $previous); - $this->app = App::getInstance('zoo'); + $this->app = App::getInstance('zoo'); $this->isAjax = $this->app->jbrequest->isAjax(); } @@ -1397,7 +1545,8 @@ public function __construct($message = '', $code = 0, Exception $previous = null public function __toString() { $message = $this->getMessage(); - if ($this->isAjax) { + if ($this->isAjax) + { $this->app->jbajax->send(['message' => $message], false); } diff --git a/src/packages/jbuniversal/jbuniversal/framework/classes/cart/jbvalue.php b/src/packages/jbuniversal/jbuniversal/framework/classes/cart/jbvalue.php index b9cd077f..0d7eba7c 100644 --- a/src/packages/jbuniversal/jbuniversal/framework/classes/cart/jbvalue.php +++ b/src/packages/jbuniversal/jbuniversal/framework/classes/cart/jbvalue.php @@ -904,7 +904,6 @@ protected function _parse($data, $forceCur = null) $currency = $matches[2]; } } - } if (is_null($value)) { @@ -1078,4 +1077,4 @@ public function __clone() class JBCartValueException extends AppException { -} \ No newline at end of file +} diff --git a/src/packages/jbuniversal/jbuniversal/framework/classes/cart/jbvariant.php b/src/packages/jbuniversal/jbuniversal/framework/classes/cart/jbvariant.php index 12bdc76b..5697e6d7 100644 --- a/src/packages/jbuniversal/jbuniversal/framework/classes/cart/jbvariant.php +++ b/src/packages/jbuniversal/jbuniversal/framework/classes/cart/jbvariant.php @@ -56,26 +56,30 @@ class JBCartVariant extends ArrayObject /** * Class constructor - * @param JBCartVariantList $list - * @param array $elements - * @param array $options + * + * @param JBCartVariantList $list + * @param array $elements + * @param array $options */ public function __construct(array $elements = array(), array $options = array(), JBCartVariantList $list = null) { // set variant id - if (isset($options['id'])) { + if (isset($options['id'])) + { $this->setId($options['id']); unset($options['id']); } // set variant list if exists - if ($list instanceof JBCartVariantList) { + if ($list instanceof JBCartVariantList) + { $this->setList($list); } //Bind elements - if ($elements) { + if ($elements) + { $data = new AppData(isset($options['data']) ? $options['data'] : array()); $this->add($elements, $data); @@ -84,7 +88,8 @@ public function __construct(array $elements = array(), array $options = array(), } //set elements data - if (isset($options)) { + if (isset($options)) + { $this->bindData($options); } @@ -93,23 +98,28 @@ public function __construct(array $elements = array(), array $options = array(), /** * Get element by identifier - * @param string $id - * @param mixed $default + * + * @param string $id + * @param mixed|null $default + * * @return JBCartElementPrice|mixed + * @since 4.15.7 */ - public function get($id, $default = null) + public function get(string $id, mixed $default = null): mixed { - return isset($this->elements[$id]) ? $this->elements[$id] : $default; + return $this->elements[$id] ?? $default; } /** - * @param integer $id Element identifier. - * @param ElementJBPrice $element + * @param integer $id Element identifier. + * @param ElementJBPrice $element + * * @throws JBCartVariantException */ public function set($id, $element) { - if (!$element instanceof JBCartElementPrice) { + if (!$element instanceof JBCartElementPrice) + { throw new JBCartVariantException('In Method: ' . __FUNCTION__ . ' values of array must be an instance of JBCartElementPrice.'); } @@ -117,19 +127,22 @@ public function set($id, $element) } /** - * @param array $elements - * @param AppData|array $options + * @param array $elements + * @param AppData|array $options */ public function add(array $elements, AppData $options) { - foreach ($elements as $element) { - $this->set($element->id(), $this->setElement($element, (array)$options->get($element->id()))); + foreach ($elements as $element) + { + $this->set($element->id(), $this->setElement($element, (array) $options->get($element->id()))); } } /** * Check if JBCartVariant exists. - * @param string $id Element identifier + * + * @param string $id Element identifier + * * @return bool */ public function has($id) @@ -147,7 +160,8 @@ public function all() } /** - * @param string $id + * @param string $id + * * @return bool */ public function isCore($id) @@ -156,13 +170,16 @@ public function isCore($id) } /** - * @param callable $p + * @param callable $p + * * @return bool */ public function exists(Closure $p) { - foreach ($this->elements as $key => $element) { - if ($p($key, $element)) { + foreach ($this->elements as $key => $element) + { + if ($p($key, $element)) + { return true; } } @@ -171,7 +188,8 @@ public function exists(Closure $p) } /** - * @param Closure $callback + * @param Closure $callback + * * @return static */ public function filter(Closure $callback) @@ -180,7 +198,8 @@ public function filter(Closure $callback) } /** - * @param Closure $callback + * @param Closure $callback + * * @return static */ public function map(Closure $callback) @@ -189,22 +208,28 @@ public function map(Closure $callback) } /** - * @param string $group Group of elements to count + * @param string $group Group of elements to count + * * @return int */ #[\ReturnTypeWillChange] public function count($group = 'all'): int { - if ($group === 'all') { + if ($group === 'all') + { $count = count($this->elements); - } elseif ($group === 'core') { + } + elseif ($group === 'core') + { $count = $this->filter(function ($element) { return $element->isCore(); })->count(); - } else { + } + else + { $count = $this->filter(function ($element) { return !$element->isCore(); })->count();; @@ -219,17 +244,19 @@ public function count($group = 'all'): int */ public function getId() { - return (int)$this->id; + return (int) $this->id; } /** - * @param int $id + * @param int $id + * * @return $this */ public function setId($id) { - if ($this->id !== (int)$id) { - $this->id = (int)$id; + if ($this->id !== (int) $id) + { + $this->id = (int) $id; } return $this; @@ -241,14 +268,15 @@ public function setId($id) */ public function hash() { - if ($this->hash !== null) { + if ($this->hash !== null) + { return $this->hash; } - $hash = (array)array_filter(array_map(function ($element) { + $hash = (array) array_filter(array_map(function ($element) { $value = $element->getValue(true); return (!$element->isCore() && ($value == '0' || !empty($value))) - ? (array)$element->data() + ? (array) $element->data() : null; }, $this->all())); asort($hash); @@ -272,13 +300,16 @@ public function clear() /** * Set link to list of variations. - * @param JBCartVariantList $list + * + * @param JBCartVariantList $list + * * @return JBCartVariant * @throws JBCartVariantException */ public function setList($list) { - if (!$list instanceof JBCartVariantList) { + if (!$list instanceof JBCartVariantList) + { throw new JBCartVariantException('In Method: ' . __FUNCTION__ . ' first argument must be an instance of JBCartVariantList.'); } $this->list = $list; @@ -289,7 +320,7 @@ public function setList($list) /** * @return ArrayIterator */ - + #[\ReturnTypeWillChange] public function getIterator(): Iterator { @@ -307,35 +338,39 @@ public function isBasic() /** * Check if variant key equals needle - * @param int $id Needle id + * + * @param int $id Needle id + * * @return bool */ public function is($id) { - return $this->id === (int)$id; + return $this->id === (int) $id; } /** - * @param bool $toString - * @param string $key - element identifier - * @param mixed $default - * @return JBCartElementPrice|JBCartValue + * @param string $key - element identifier + * @param bool $toString + * @param mixed|null $default + * + * @return JBCartValue + * + * @since 4.15.7 */ - - // public function getValue($toString = false, $key, $default = null) - - public function getValue($key, $toString = false, $default = null) + public function getValue(string $key, bool $toString = false, mixed $default = null): mixed { - if ($element = $this->get($key)) { + if ($element = $this->get($key)) + { return $element->getValue($toString); } - + return $default; } /** - * @param string $id Element identifier - * @param mixed $value Value to check + * @param string $id Element identifier + * @param mixed $value Value to check + * * @return bool */ public function hasOption($id, $value) @@ -343,7 +378,8 @@ public function hasOption($id, $value) $element = $this->get($id); $element->bindData($value); - if ($element->hasOption($element->getValue(true))) { + if ($element->hasOption($element->getValue(true))) + { return $value; } @@ -385,7 +421,9 @@ public function getRequired() /** * Get data from all elements - * @param null $isOverlay + * + * @param null $isOverlay + * * @return array */ public function data($isOverlay = null) @@ -394,15 +432,19 @@ public function data($isOverlay = null) $result = array_filter(array_map(function ($element) { $value = StringHelper::trim($element->getValue(true)); - return (!empty($value) || $value == '0') ? (array)$element->data() : null; + return (!empty($value) || $value == '0') ? (array) $element->data() : null; }, $elements)); // hack for unique props - if ($isOverlay === true) { + if ($isOverlay === true) + { $isFound = 0; - foreach ($result as $key => $item) { - if (strpos($key, '_') !== 0) { - if ($isFound != 0) { + foreach ($result as $key => $item) + { + if (strpos($key, '_') !== 0) + { + if ($isFound != 0) + { unset($result[$key]); } $isFound++; @@ -415,17 +457,20 @@ public function data($isOverlay = null) /** - * @param array $options + * @param array $options + * * @return $this */ public function bindData(array $options = array()) { $elements = new AppData(); - if (isset($options['data'])) { + if (isset($options['data'])) + { $elements->exchangeArray($options['data']); } - foreach ($this->elements as $key => $element) { + foreach ($this->elements as $key => $element) + { $this->set($key, $this->setElement($element, $elements->get($key))); } @@ -435,21 +480,26 @@ public function bindData(array $options = array()) /** * Get Total price for variant * @return JBCartValue - * @throws Exception + * + * @throws JBCartVariantException + * @since 4.15.7 */ - public function getTotal() + public function getTotal(): JBCartValue { - if ($this->total !== null) { + if ($this->total !== null) + { return $this->total; } - if (!$this->list->isOverlay) { - - $total = $this->getPrice()->minus($this->getValue(true, '_discount'), true); + if (!$this->list->isOverlay) + { + $total = $this->getPrice()->minus($this->getValue('_discount', true), true); - if ($this->list instanceof JBCartVariantList) { + if ($this->list instanceof JBCartVariantList) + { $total = $this->list->addModifiers($total, true); } + $this->total = $total; return $this->total; @@ -461,25 +511,32 @@ public function getTotal() /** * Get price for variant * @return JBCartValue + * + * @since 4.15.7 */ - public function getPrice() + public function getPrice(): JBCartValue { - if ($this->price === null) { + if ($this->price === null) + { $price = JBCart::val(); - if ($element = $this->get('_value')) { + if ($element = $this->get('_value')) + { $price->set($element->getValue(true)); - if ($this->list->isOverlay === false && $element->isModifier() && !$this->isBasic()) { - $price = $this->list->first()->getValue(false, '_value')->add($price); + if ($this->list->isOverlay === false && $element->isModifier() && !$this->isBasic()) + { + $price = $this->list->first()->getValue('_value')->add($price); } } $this->price = $price; - if ($this->list->isOverlay === false) { - $this->price = $price->add($this->getValue(true, '_margin'), true); + if ($this->list->isOverlay === false) + { + $this->price = $price->add($this->getValue('_margin', true), true); - if ($this->list instanceof JBCartVariantList) { - $this->price = $this->list->addModifiers($this->price, false); + if ($this->list instanceof JBCartVariantList) + { + $this->price = $this->list->addModifiers($this->price); } } } @@ -489,14 +546,18 @@ public function getPrice() /** * Check if item in stock - * @param integer $quantity + * + * @param integer $quantity + * * @return bool */ public function inStock($quantity) { /** @type JBCartElementPriceBalance $element */ - if ($element = $this->get('_balance')) { - if ($element->inStock($quantity)) { + if ($element = $this->get('_balance')) + { + if ($element->inStock($quantity)) + { return true; } @@ -512,33 +573,39 @@ public function inStock($quantity) */ public function __toString() { - return (string)$this->getId(); + return (string) $this->getId(); } /** - * @param JBCartElementPrice $element - * @param array|string elements data + * @param JBCartElementPrice $element + * @param array|string elements data + * * @return mixed */ protected function setElement($element, $data) { - if (is_array($data) && !empty($data)) { + if (is_array($data) && !empty($data)) + { $data = array_filter($data, function ($value) { ; + return ($value !== '' && $value !== null); }); } $element->setVariant($this->id); - if ($this->list instanceof JBCartVariantList && !$this->isBasic()) { - $basic = $this->list->first()->getValue(true, $element->identifier); + if ($this->list instanceof JBCartVariantList && !$this->isBasic()) + { + $basic = $this->list->first()->getValue($element->identifier, true); - if ($basic !== null && $basic !== '') { + if ($basic !== '') + { $data['_basic'] = $basic; } } - if (!empty($data)) { + if (!empty($data)) + { $element->bindData($data); } @@ -551,4 +618,4 @@ protected function setElement($element, $data) */ class JBCartVariantException extends AppException { -} \ No newline at end of file +} diff --git a/src/packages/jbuniversal/jbuniversal/framework/classes/cart/jbvariantlist.php b/src/packages/jbuniversal/jbuniversal/framework/classes/cart/jbvariantlist.php index 6abc1508..964e75da 100644 --- a/src/packages/jbuniversal/jbuniversal/framework/classes/cart/jbvariantlist.php +++ b/src/packages/jbuniversal/jbuniversal/framework/classes/cart/jbvariantlist.php @@ -223,17 +223,17 @@ public function byDefault() /** * @return int */ - + #[\ReturnTypeWillChange] public function count(): int { if ($this->variants === null) { return 0; } - + return count($this->variants); } - + /** * @param $key @@ -268,7 +268,7 @@ public function setOptions($options = array()) /** * @return ArrayIterator */ - + #[\ReturnTypeWillChange] public function getIterator(): Iterator { @@ -515,8 +515,8 @@ protected function _plainCartData() $jbPrice = $this->getJBPrice(); $total = $this->getTotal()->data(true); - $discount = $this->current()->getValue(true, '_discount'); - $margin = $this->current()->getValue(true, '_margin'); + $discount = $this->current()->getValue('_discount', true); + $margin = $this->current()->getValue('_margin', true); $elements = $this->defaultVariantCartData(); // bug, call only at the end! $elements['_discount'] = $discount; @@ -581,8 +581,9 @@ protected function _plainPrice() /** * @return JBCartValue + * @since 4.15.7 */ - protected function _calcPrice() + protected function _calcPrice(): JBCartValue { $first = $this->first(); $price = $first->getPrice(); @@ -597,10 +598,10 @@ protected function _calcPrice() } } $price = clone $price; - $margin = $first->getValue(false, '_margin', JBCart::val())->positive(); + $margin = $first->getValue('_margin', false, JBCart::val())->positive(); $price->add($margin); - return $this->addModifiers($price, false); + return $this->addModifiers($price); } /** @@ -615,11 +616,12 @@ protected function _plainTotal() /** * Get the total price for the variant element - ElementJBPriceCalc * @return JBCartValue + * @since 4.15.7 */ - protected function _calcTotal() + protected function _calcTotal(): JBCartValue { $first = $this->first(); - $price = clone $this->_calcPrice()->minus($first->getValue(false, '_discount', JBCart::val())->positive()); + $price = clone $this->_calcPrice()->minus($first->getValue('_discount', false, JBCart::val())->positive()); return $this->addModifiers($price, true); } @@ -655,4 +657,4 @@ public function remove($id) */ class JBCartVariantListException extends AppException { -} \ No newline at end of file +}