Initial import
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace app\services\api;
|
||||
|
||||
class Sync
|
||||
{
|
||||
public function container($action)
|
||||
{
|
||||
if (method_exists($this, $action))
|
||||
return $this->{$action}($this->parse());
|
||||
return $this->execError('Wrong action');
|
||||
}
|
||||
|
||||
private function parse()
|
||||
{
|
||||
$data = json_decode(\Yii::$app->request->getRawBody());
|
||||
if ($data) return $data;
|
||||
return $this->execError('Wrong data format');
|
||||
}
|
||||
|
||||
protected function required($strong_params, $params, $data) {
|
||||
foreach ($strong_params as $param) {
|
||||
if(empty($data->{$param}))
|
||||
if(empty($data->{$param}))
|
||||
return $this->execError("Missing parameter $param");
|
||||
}
|
||||
foreach ($params as $param) {
|
||||
if(!isset($data->{$param}))
|
||||
return $this->execError("Missing parameter $param");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected function exist($class, $key, $value) {
|
||||
/** @var \yii\db\ActiveRecord $class */
|
||||
if($class::find()->where([$key => $value])->exists()) {
|
||||
return $this->execError("Record already exists with $key:$value");
|
||||
}
|
||||
}
|
||||
protected function notExist($class, $key, $value) {
|
||||
/** @var \yii\db\ActiveRecord $class */
|
||||
if(!$class::find()->where([$key => $value])->exists()) {
|
||||
return $this->execError("Record does not exists $key:$value");
|
||||
}
|
||||
}
|
||||
|
||||
protected function execError($message)
|
||||
{
|
||||
return ['error' => 1, 'message' => $message];
|
||||
}
|
||||
|
||||
protected function execSuccess()
|
||||
{
|
||||
return['success' => 1];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user