2016-03-28 11:15:08 -07:00
|
|
|
<?php
|
2016-03-28 11:38:08 -07:00
|
|
|
namespace MichaelAChrisco\ReadOnly;
|
2017-03-31 11:28:20 -07:00
|
|
|
|
2016-12-09 07:19:04 -08:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
|
2017-03-31 11:28:20 -07:00
|
|
|
trait ReadOnlyTrait
|
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
protected static function isActive(): bool {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-11 14:10:25 -08:00
|
|
|
/**
|
|
|
|
* Throws ReadOnlyException on create
|
|
|
|
* @param array $attributes
|
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
|
|
|
public static function create(array $attributes = [])
|
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
return parent::create($attributes);
|
2017-12-11 14:10:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws ReadOnlyException on forceCreate
|
|
|
|
* @param array $attributes
|
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
|
|
|
public static function forceCreate(array $attributes)
|
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
return parent::forceCreate($attributes);
|
2017-12-11 14:10:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws ReadOnlyException on save
|
|
|
|
* @param array $options
|
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
|
|
|
public function save(array $options = [])
|
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
return parent::save($options);
|
2017-12-11 14:10:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws ReadOnlyException on update
|
|
|
|
* @param array $attributes
|
|
|
|
* @param array $options
|
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
|
|
|
public function update(array $attributes = [], array $options = [])
|
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
return parent::update($attributes, $options);
|
2017-12-11 14:10:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws ReadOnlyException on firstOrCreate
|
2018-10-22 14:00:30 -07:00
|
|
|
* @param array $attributes
|
|
|
|
* @param array $values
|
2017-12-11 14:10:25 -08:00
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
2018-10-22 14:00:30 -07:00
|
|
|
public static function firstOrCreate(array $attributes, array $values = [])
|
2017-12-11 14:10:25 -08:00
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
return parent::firstOrCreate( $attributes, $values);
|
2017-12-11 14:10:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws ReadOnlyException on firstOrNew
|
2018-10-22 14:00:30 -07:00
|
|
|
* @param array $attributes
|
|
|
|
* @param array $values
|
2017-12-11 14:10:25 -08:00
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
2018-10-22 14:00:30 -07:00
|
|
|
public static function firstOrNew(array $attributes, array $values = [])
|
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
return parent::firstOrNew( $attributes, $values);
|
2018-10-22 14:00:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws ReadOnlyException on updateOrCreate
|
|
|
|
* @param array $attributes
|
|
|
|
* @param array $values
|
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
|
|
|
public static function updateOrCreate(array $attributes, array $values = [])
|
2017-12-11 14:10:25 -08:00
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
return parent::updateOrCreate( $attributes, $values );
|
2017-12-11 14:10:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws ReadOnlyException on delete
|
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
|
|
|
public function delete()
|
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
return parent::delete();
|
2017-12-11 14:10:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws ReadOnlyException on destroy
|
|
|
|
* @param mixed $ids
|
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
|
|
|
public static function destroy($ids)
|
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
return parent::destroy($ids);
|
2017-12-11 14:10:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws ReadOnlyException on restore
|
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
|
|
|
public function restore()
|
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
return parent::restore();
|
2017-12-11 14:10:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws ReadOnlyException on forceDelete
|
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
|
|
|
public function forceDelete()
|
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
return parent::forceDelete();
|
2017-12-11 14:10:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws ReadOnlyException on performDeleteOnModel
|
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
2017-03-31 11:28:20 -07:00
|
|
|
public function performDeleteOnModel()
|
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
parent::performDeleteOnModel();
|
2016-12-09 06:37:13 -08:00
|
|
|
}
|
|
|
|
|
2017-12-11 14:10:25 -08:00
|
|
|
/**
|
|
|
|
* Throws ReadOnlyException on push
|
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
2017-03-31 11:28:20 -07:00
|
|
|
public function push()
|
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
return parent::push();
|
2016-12-09 06:37:13 -08:00
|
|
|
}
|
|
|
|
|
2017-12-11 14:10:25 -08:00
|
|
|
/**
|
|
|
|
* Throws ReadOnlyException on finishSave
|
|
|
|
* @param array $options
|
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
2017-03-31 11:28:20 -07:00
|
|
|
public function finishSave(array $options)
|
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
parent::finishSave($options);
|
2016-12-09 06:37:13 -08:00
|
|
|
}
|
|
|
|
|
2017-12-11 14:10:25 -08:00
|
|
|
/**
|
|
|
|
* Throws ReadOnlyException on performUpdate
|
|
|
|
* @param Builder $query
|
|
|
|
* @param array $options
|
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
2022-09-07 09:14:13 -07:00
|
|
|
public function performUpdate(Builder $query, array $options = []): bool
|
2017-03-31 11:28:20 -07:00
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
return parent::performUpdate($query, $options);
|
2016-12-09 06:37:13 -08:00
|
|
|
}
|
|
|
|
|
2017-12-11 14:10:25 -08:00
|
|
|
/**
|
|
|
|
* Throws ReadOnlyException on touch
|
2022-02-08 13:32:53 -08:00
|
|
|
* @param string|null $attribute
|
2017-12-11 14:10:25 -08:00
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
2022-02-08 13:32:53 -08:00
|
|
|
public function touch($attribute = null)
|
2017-03-31 11:28:20 -07:00
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
return parent::touch($attribute);
|
2016-12-09 06:37:13 -08:00
|
|
|
}
|
2017-03-31 10:58:44 -07:00
|
|
|
|
2017-12-11 14:10:25 -08:00
|
|
|
/**
|
|
|
|
* Throws ReadOnlyException on insert
|
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
2017-03-31 11:28:20 -07:00
|
|
|
public function insert()
|
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
return parent::insert();
|
2017-03-29 10:15:48 -07:00
|
|
|
}
|
2017-03-31 10:58:44 -07:00
|
|
|
|
2017-03-29 10:15:48 -07:00
|
|
|
/**
|
2017-12-11 14:10:25 -08:00
|
|
|
* Throws ReadOnlyException on truncate
|
|
|
|
* @throws ReadOnlyException
|
|
|
|
*/
|
2017-03-31 11:28:20 -07:00
|
|
|
public function truncate()
|
|
|
|
{
|
2022-09-07 09:14:13 -07:00
|
|
|
if (static::isActive()) {
|
|
|
|
throw new ReadOnlyException(__FUNCTION__, get_called_class());
|
|
|
|
}
|
|
|
|
return parent::truncate();
|
2017-03-29 10:15:48 -07:00
|
|
|
}
|
2018-10-22 14:00:30 -07:00
|
|
|
}
|