Skip to content

Commit

Permalink
Реализация метода delete()
Browse files Browse the repository at this point in the history
  • Loading branch information
kazartsev committed Dec 30, 2016
1 parent b300171 commit 9b2e90e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Sberned/RestORM/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,18 @@ class Builder
* @param string $method
* @param bool $json
* @param array $data
* @param bool $expect_result
*/
public function __construct(string $className, string $url, string $link, string $method = 'get', bool $json, array $data = [])
public function __construct(string $className, string $url, string $link, string $method = 'get', bool $json,
array $data = [], bool $expect_result = true)
{
$this->className = $className;
$this->url = $url;
$this->link = $link;
$this->method = $method;
$this->json = $json;
$this->data = $data;
$this->expect_result = $expect_result;
}

/**
Expand All @@ -120,9 +123,13 @@ public function send()
$result->setOption(CURLOPT_SSL_VERIFYPEER, false);
$res = $result->send();
if($res->statusCode < 400) {
$this->result = json_decode($res->body);
if($this->result->success) {
return $this->result->data;
if ($this->expect_result) {
$this->result = json_decode($res->body);
if($this->result->success) {
return $this->result->data;
}
} else {
return $this->result;
}
} else {
throw (new MassAssignmentException)->setError($this->url, $res->body);
Expand Down
24 changes: 24 additions & 0 deletions src/Sberned/RestORM/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ public function __construct()
$this->setClassName();
}

/**
* @return bool
*/
public function delete()
{
return $this->deleteThis();
}

/**
* @return bool
*/
Expand Down Expand Up @@ -156,6 +164,22 @@ private function insertThis()
return false;
}

/**
* @return bool
*/
private function deleteThis()
{
$objects = $this->all();
foreach ($objects as $object) {
if (property_exists($object, "id")) {
$newQuery = new Builder($this->className, $this->getUrl(), $this->getLink() . '/' . $object->id,
'DELETE', true, [], false);
$res = $newQuery->send();
}
}
return true;
}

/**
* @param array $columns
* @return stdClass
Expand Down

0 comments on commit 9b2e90e

Please sign in to comment.