/** * @api {post} /brands POST /brands * @apiExample Example usage: * curl -i -X POST "http://apibeta.compargo.com/v1/brands/?countryCode=ph&language=en" * -H "X-COMPARE-REST-API-KEY: 1234567890" * -d "companyId=4c8367bc-319a-11e4-988c-7d9574853fac&name=BDO&language=en&status=1" * * @apiDescription Create a new Brand * @apiName Post * @apiGroup Brands * * @apiHeader {String} X-COMPARE-REST-API-KEY Brand unique access-key. * * @apiParam {String} language Mandatory Language. * @apiParam {String} countryCode Mandatory Country code. * @apiParam {String} name Mandatory Name of the Brand. * @apiParam {String} alias Mandatory Alias of the Brand. * @apiParam {String} [logo] Optional Logo of the Brand. * @apiParam {String} [link] Optional Link of the Brand. * @apiParam {String} [description] Optional Description of the Brand. * @apiParam {String} [revenueValue] Optional Revenue Value of the Brand. * @apiParam {String} [alias] Optional Alias of the Brand. * @apiParam {String} [status=0] Optional Status of the Brand. * @apiParam {String} [createdBy] Optional ID of the User who created the Vertical. * @apiParam {String} [modifiedBy] Optional ID of the User who modified the Vertical. * * @apiSuccess {String} id The new Brand-ID. * * @apiSuccessExample Success-Response: * HTTP/1.1 200 OK * { * "id": "dec7a1fe-370a-11e4-b18a-fe7344fb1ea4" * } * * @apiError BadInputParameter The request cannot be fulfilled due to bad syntax. * * @apiErrorExample Error-Response: * HTTP/1.1 400 * { * "error": "BadInputParameter" * } * * @apiError InvalidAccessToken The access token is invalid. * * @apiErrorExample Error-Response: * HTTP/1.1 401 Unauthorized * { * "error": "InvalidAccessToken" * } * * @apiError MissingAuthenticationCredentials The authentication credentials are missing. * * @apiErrorExample Error-Response: * HTTP/1.1 401 Unauthorized * { * "error": "MissingAuthenticationCredentials" * } * * @apiError RouteNotFound That route was not found on the server. * * @apiErrorExample Error-Response: * HTTP/1.1 404 * { * "error": "RouteNotFound" * } */ public function post() { $results = $params = array(); $request = $this->di->get('request'); $data = $request->getPost(); if (!empty($data)) { $brand = new Brands(); $data['id'] = $brand->id; $data['companyId'] = isset($data['companyId']) ? $data['companyId'] : ''; $data['name'] = isset($data['name']) ? $data['name'] : ''; $data['alias'] = isset($data['alias']) ? $data['alias'] : ''; $data['logo'] = isset($data['logo']) ? $data['logo'] : ''; $data['link'] = isset($data['link']) ? $data['link'] : ''; $data['description'] = isset($data['description']) ? $data['description'] : ''; $data['language'] = isset($data['language']) ? $data['language'] : ''; $data['revenueValue'] = isset($data['revenueValue']) ? $data['revenueValue'] : '0.00'; $data['status'] = isset($data['status']) ? $data['status'] : 0; // iterate each field on the json schemaj // and fill parameters to feed on validator later // required/mandatory fields w/o values will trigger a validation error $schemaFile = $this->schemaDir . $this->getDI()->getConfig()->application->jsonSchema->brands; $output = readJsonFromFile($schemaFile, $this); if (!empty($output)) { $properties = $output['properties']; foreach ($properties as $key => $value) { if ($key == 'options') { continue; } $params[$key] = !empty($data[$key]) ? $data[$key] : ''; } } // $params['name'] = $data['name']; // $params['logo'] = $data['logo']; // $params['link'] = $data['link']; // $params['description'] = $data['description']; // $params['revenueValue'] = $data['revenueValue']; $valid = $brand->validateProperty($params, $schemaFile); if (!empty($valid)) { $errors = implode(", ", $valid); throw new HTTPException("Request unable to be followed due to semantic errors.", 422, array('dev' => ucfirst($errors), 'internalCode' => 'P1000', 'more' => '')); } if (isset($data['status'])) { $data['active'] = $data['status'] != Brands::ACTIVE ? 0 : 1; } $data['createdBy'] = isset($data['createdBy']) ? $data['createdBy'] : ''; $data['modifiedBy'] = isset($data['modifiedBy']) ? $data['modifiedBy'] : ''; if ($brand->save($data)) { $results['id'] = $brand->id; } else { throw new HTTPException("Request unable to be followed due to semantic errors", 422, array('dev' => $brand->getMessages(), 'internalCode' => 'P1000', 'more' => '')); } } else { throw new HTTPException("The request cannot be fulfilled due to bad syntax.", 400, array('dev' => 'A required field is missing.', 'internalCode' => 'P1000', 'more' => '')); } return $results; }