/** * Function to handle persistance of the entity across the * remote API. Function will handle either a CREATE or UPDATE * * @return Boolean Success of the save operation */ public function save() { //get a request object $request = RequestFactory::build(); if ($this->getId() === false) { //make a CREATE request $request->createRequest(Config::get('request.base_uri'), UrlGenerator::getCreateUri($this), 'POST', [], Config::get('request.http_method_param')); } else { //make an UPDATE request $request->createRequest(Config::get('request.base_uri'), UrlGenerator::getDeleteUri($this, [':' . $this->getIdentityProperty() => $this->getId()]), 'PUT', [], Config::get('request.http_method_param')); } //add auth if it is needed if ($auth = AuthFactory::build()) { $request->authenticate($auth); } //set the property attributes on the request $request->setModelProperties($this); //actually send the request $response = $request->sendRequest(); //handle clean response with errors if (ResponseInterpreterFactory::build()->invalid($response)) { //get the errors and set them to our local collection $this->errors = ErrorHandlerFactory::build()->parseErrors($response); //do any needed cleanup $this->doPostRequestCleanUp(); return false; } //end if //get the response and inflate from that $data = $response->parseResponseToData(); $this->fill($data); //inflate the ID property that should be guarded //and thus not fillable $id = $this->getIdentityProperty(); if (array_key_exists($id, $data)) { $this->{$id} = $data[$id]; } $this->doPostRequestCleanUp(); return true; }
public function testCreateUri() { $x = new User(); $this->assertEquals('/users', UrlGenerator::getCreateUri($x)); }