public function show($id) { try { $this->respondTo('html', function () use($id) { $product = Product::find($id); $topRelated = $product->getTopRelated(6); $ratingScalar = Rating::getScalar(); $data = ['product' => $product, 'top_related' => $topRelated, 'rating_scalar' => $ratingScalar, 'ratings' => []]; $count = []; foreach ($ratingScalar as $value) { $count[$value] = 0; } foreach ($product->ratings as $rating) { ++$count[$rating->value]; } foreach ($ratingScalar as $value) { $rating = array('value' => $value, 'count' => $count[$value]); $data['ratings'][] = $rating; } $data = array_merge($this->getCommonData(), $data); $this->render(new TwigView('product/show.html', $data)); }); } catch (ResourceNotFoundException $e) { $this->respondTo('html', function () { $this->getResponse()->redirect('App\\Store\\Controllers\\ProductController', 'index'); }); } }
function _validate() { $vm = self::createValidationManager(); $vm->register('value', new Validator\RequireValidator($this->value), 'Value of the rating is required'); $vm->register('value', new Validator\FunctionValidator(function () { return in_array((int) $this->value, Rating::getScalar()); }), 'Rating value is invalid'); $vm->doValidate(); }