public function postBrandAction() { try { $app = \Slim\Slim::getInstance(); $data = $app->request->getBody(); $brandPost = json_decode($data); $brand = new \Entity\Brand(); $brand->setName($brandPost->name); $this->validateBrand($brand); $brandsRep = new \Repository\BrandRepository(); if ($brandsRep->exists($brand)) { throw new \Exception('Brand already exists'); } $brandsRep->insert($brand); $returnBrands = $brandsRep->finByName($brand->getName()); if (count($returnBrands) > 1) { throw new Exception('Many brands'); } $resource = new \League\Fractal\Resource\Item($returnBrands[0], new \Transformer\BrandTransformer()); return $this->writeJson($resource, 200); } catch (\Exception $exc) { return $this->error($exc->getMessage()); } }
/** * * @param array $result * @return \Entity\Brand[] */ private function brandFactory($result) { $brands = array(); foreach ($result as $brand) { $brandObj = new \Entity\Brand(); $brandObj->setBrandId($brand['brand_id']); $brandObj->setName($brand['name']); $brands[] = $brandObj; } return $brands; }