File: //home/aliazzsr/api.crm.vqode.com/models/core/CompanyType.php
<?php
namespace app\models\core;
use app\components\BaseModel;
use yii\web\ForbiddenHttpException;
/**
* Class CompanyType
* @package app\models\core
* @property integer $id
* @property string $name
* @property string $description
*/
class CompanyType extends BaseModel
{
const ARCHITECT = 3;
const MAIN_CONTRACTOR = 4;
const SUB_CONTRACTOR = 5;
protected static $systemItems = [
self::ARCHITECT,
self::MAIN_CONTRACTOR,
self::SUB_CONTRACTOR,
];
public static function tableName()
{
return 'company_type';
}
public function rules()
{
return [
[['name', ], 'required'],
[['name', ], 'string', 'max' => 255],
[['name', ], 'unique'],
[['name', 'description'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function beforeSave($insert)
{
$this->checkIsSystemItem();
return parent::beforeSave($insert);
}
/**
* @inheritdoc
*/
public function beforeDelete()
{
$this->checkIsSystemItem();
return parent::beforeDelete();
}
/**
* @throws ForbiddenHttpException
*/
protected function checkIsSystemItem()
{
$isSystemItem = in_array($this->id, self::$systemItems);
if ($isSystemItem) {
throw new ForbiddenHttpException('A system company type, so can\'t be changed or deleted');
}
}
}
/**
* @SWG\Definition(
* definition="CompanyType",
* type="object",
* description="CompanyType model",
* allOf={
* @SWG\Schema(ref="#/definitions/CompanyType"),
* @SWG\Schema(
* required={"name"},
* @SWG\Property(property="id", type="integer", example=7),
* @SWG\Property(property="name", type="string", example="Architect"),
* @SWG\Property(property="description", type="string"),
* )
* }
* )
*/