2017-07-01 20:29:49 +00:00
< ? php
2017-09-26 02:43:01 +00:00
/**
2017-07-01 20:29:49 +00:00
* Pterodactyl - Panel
* Copyright ( c ) 2015 - 2017 Dane Everitt < dane @ daneeveritt . com >.
*
2017-09-26 02:43:01 +00:00
* This software is licensed under the terms of the MIT license .
* https :// opensource . org / licenses / MIT
2017-07-01 20:29:49 +00:00
*/
namespace Pterodactyl\Repositories\Eloquent ;
2017-08-12 20:29:01 +00:00
use Webmozart\Assert\Assert ;
2017-11-18 01:01:42 +00:00
use Pterodactyl\Repositories\Repository ;
2017-08-12 20:29:01 +00:00
use Illuminate\Database\Query\Expression ;
2017-07-01 20:29:49 +00:00
use Pterodactyl\Contracts\Repository\RepositoryInterface ;
2017-07-08 19:07:51 +00:00
use Pterodactyl\Exceptions\Model\DataValidationException ;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException ;
2017-09-16 03:13:33 +00:00
use Pterodactyl\Contracts\Repository\Attributes\SearchableInterface ;
2017-07-01 20:29:49 +00:00
abstract class EloquentRepository extends Repository implements RepositoryInterface
{
/**
2017-07-08 19:07:51 +00:00
* { @ inheritdoc }
2017-07-01 20:29:49 +00:00
* @ return \Illuminate\Database\Eloquent\Builder
*/
public function getBuilder ()
{
return $this -> getModel () -> newQuery ();
}
/**
2017-07-08 19:07:51 +00:00
* { @ inheritdoc }
2017-08-22 03:10:48 +00:00
* @ param bool $force
2017-07-08 19:07:51 +00:00
* @ return \Illuminate\Database\Eloquent\Model | bool
2017-07-01 20:29:49 +00:00
*/
2017-07-08 19:07:51 +00:00
public function create ( array $fields , $validate = true , $force = false )
2017-07-01 20:29:49 +00:00
{
2017-08-16 03:21:47 +00:00
Assert :: boolean ( $validate , 'Second argument passed to create must be boolean, recieved %s.' );
Assert :: boolean ( $force , 'Third argument passed to create must be boolean, received %s.' );
2017-08-12 20:29:01 +00:00
2017-07-01 20:29:49 +00:00
$instance = $this -> getBuilder () -> newModelInstance ();
if ( $force ) {
$instance -> forceFill ( $fields );
} else {
$instance -> fill ( $fields );
}
if ( ! $validate ) {
$saved = $instance -> skipValidation () -> save ();
} else {
if ( ! $saved = $instance -> save ()) {
throw new DataValidationException ( $instance -> getValidator ());
}
}
return ( $this -> withFresh ) ? $instance -> fresh () : $saved ;
}
/**
2017-07-08 19:07:51 +00:00
* { @ inheritdoc }
2017-07-01 20:29:49 +00:00
* @ return \Illuminate\Database\Eloquent\Builder | \Illuminate\Database\Eloquent\Model
*/
public function find ( $id )
{
2017-08-16 03:21:47 +00:00
Assert :: numeric ( $id , 'First argument passed to find must be numeric, received %s.' );
2017-08-12 20:29:01 +00:00
2017-07-01 20:29:49 +00:00
$instance = $this -> getBuilder () -> find ( $id , $this -> getColumns ());
if ( ! $instance ) {
throw new RecordNotFoundException ();
}
return $instance ;
}
2017-07-08 19:07:51 +00:00
/**
* { @ inheritdoc }
2017-07-08 20:04:59 +00:00
* @ return \Illuminate\Database\Eloquent\Collection
2017-07-08 19:07:51 +00:00
*/
public function findWhere ( array $fields )
2017-07-01 20:29:49 +00:00
{
2017-07-08 20:04:59 +00:00
return $this -> getBuilder () -> where ( $fields ) -> get ( $this -> getColumns ());
}
/**
* { @ inheritdoc }
* @ return \Illuminate\Database\Eloquent\Model
*/
public function findFirstWhere ( array $fields )
{
$instance = $this -> getBuilder () -> where ( $fields ) -> first ( $this -> getColumns ());
if ( ! $instance ) {
2017-09-16 03:13:33 +00:00
throw new RecordNotFoundException ;
2017-07-08 20:04:59 +00:00
}
return $instance ;
2017-07-01 20:29:49 +00:00
}
2017-08-05 22:20:07 +00:00
/**
* { @ inheritdoc } .
*/
public function findCountWhere ( array $fields )
{
return $this -> getBuilder () -> where ( $fields ) -> count ( $this -> getColumns ());
}
2017-07-01 20:29:49 +00:00
/**
2017-07-08 19:07:51 +00:00
* { @ inheritdoc }
2017-07-01 20:29:49 +00:00
*/
public function delete ( $id , $destroy = false )
{
2017-08-16 03:21:47 +00:00
Assert :: numeric ( $id , 'First argument passed to delete must be numeric, received %s.' );
Assert :: boolean ( $destroy , 'Second argument passed to delete must be boolean, received %s.' );
2017-08-12 20:29:01 +00:00
2017-08-09 02:21:10 +00:00
$instance = $this -> getBuilder () -> where ( $this -> getModel () -> getKeyName (), $id );
return ( $destroy ) ? $instance -> forceDelete () : $instance -> delete ();
}
/**
* { @ inheritdoc }
*/
public function deleteWhere ( array $attributes , $force = false )
{
2017-08-16 03:21:47 +00:00
Assert :: boolean ( $force , 'Second argument passed to deleteWhere must be boolean, received %s.' );
2017-08-12 20:29:01 +00:00
2017-08-09 02:21:10 +00:00
$instance = $this -> getBuilder () -> where ( $attributes );
2017-07-01 20:29:49 +00:00
2017-08-09 02:21:10 +00:00
return ( $force ) ? $instance -> forceDelete () : $instance -> delete ();
2017-07-01 20:29:49 +00:00
}
/**
2017-07-08 19:07:51 +00:00
* { @ inheritdoc }
2017-07-01 20:29:49 +00:00
*/
2017-07-08 19:07:51 +00:00
public function update ( $id , array $fields , $validate = true , $force = false )
2017-07-01 20:29:49 +00:00
{
2017-08-16 03:21:47 +00:00
Assert :: numeric ( $id , 'First argument passed to update must be numeric, received %s.' );
Assert :: boolean ( $validate , 'Third argument passed to update must be boolean, received %s.' );
Assert :: boolean ( $force , 'Fourth argument passed to update must be boolean, received %s.' );
2017-08-12 20:29:01 +00:00
2017-07-01 20:29:49 +00:00
$instance = $this -> getBuilder () -> where ( 'id' , $id ) -> first ();
if ( ! $instance ) {
throw new RecordNotFoundException ();
}
if ( $force ) {
$instance -> forceFill ( $fields );
} else {
$instance -> fill ( $fields );
}
if ( ! $validate ) {
$saved = $instance -> skipValidation () -> save ();
} else {
if ( ! $saved = $instance -> save ()) {
throw new DataValidationException ( $instance -> getValidator ());
}
}
2017-07-08 19:17:07 +00:00
return ( $this -> withFresh ) ? $instance -> fresh () : $saved ;
2017-07-01 20:29:49 +00:00
}
2017-07-24 00:57:43 +00:00
/**
* { @ inheritdoc }
*/
public function updateWhereIn ( $column , array $values , array $fields )
{
2017-08-16 03:21:47 +00:00
Assert :: stringNotEmpty ( $column , 'First argument passed to updateWhereIn must be a non-empty string, received %s.' );
2017-08-12 20:29:01 +00:00
2017-07-24 00:57:43 +00:00
return $this -> getBuilder () -> whereIn ( $column , $values ) -> update ( $fields );
}
2017-07-08 19:07:51 +00:00
/**
* { @ inheritdoc }
*/
public function massUpdate ( array $where , array $fields )
2017-07-01 20:29:49 +00:00
{
// TODO: Implement massUpdate() method.
}
2017-07-15 16:52:34 +00:00
/**
* { @ inheritdoc }
*/
public function all ()
{
2017-09-16 03:13:33 +00:00
$instance = $this -> getBuilder ();
2017-09-30 17:06:16 +00:00
if ( is_subclass_of ( get_called_class (), SearchableInterface :: class )) {
2017-09-16 03:13:33 +00:00
$instance = $instance -> search ( $this -> searchTerm );
}
return $instance -> get ( $this -> getColumns ());
2017-07-15 16:52:34 +00:00
}
2017-07-20 01:49:41 +00:00
/**
* { @ inheritdoc }
*/
public function insert ( array $data )
{
return $this -> getBuilder () -> insert ( $data );
}
2017-07-25 02:34:10 +00:00
2017-08-06 02:10:32 +00:00
/**
* Insert multiple records into the database and ignore duplicates .
*
2017-08-22 03:10:48 +00:00
* @ param array $values
2017-08-06 02:10:32 +00:00
* @ return bool
*/
public function insertIgnore ( array $values )
{
if ( empty ( $values )) {
return true ;
}
if ( ! is_array ( reset ( $values ))) {
$values = [ $values ];
} else {
foreach ( $values as $key => $value ) {
ksort ( $value );
$values [ $key ] = $value ;
}
}
$bindings = array_values ( array_filter ( array_flatten ( $values , 1 ), function ( $binding ) {
return ! $binding instanceof Expression ;
}));
$grammar = $this -> getBuilder () -> toBase () -> getGrammar ();
$table = $grammar -> wrapTable ( $this -> getModel () -> getTable ());
$columns = $grammar -> columnize ( array_keys ( reset ( $values )));
$parameters = collect ( $values ) -> map ( function ( $record ) use ( $grammar ) {
return sprintf ( '(%s)' , $grammar -> parameterize ( $record ));
}) -> implode ( ', ' );
$statement = " insert ignore into $table ( $columns ) values $parameters " ;
return $this -> getBuilder () -> getConnection () -> statement ( $statement , $bindings );
}
2017-07-25 02:34:10 +00:00
/**
* { @ inheritdoc }
* @ return bool | \Illuminate\Database\Eloquent\Model
*/
public function updateOrCreate ( array $where , array $fields , $validate = true , $force = false )
{
2017-08-16 03:21:47 +00:00
Assert :: boolean ( $validate , 'Third argument passed to updateOrCreate must be boolean, received %s.' );
Assert :: boolean ( $force , 'Fourth argument passed to updateOrCreate must be boolean, received %s.' );
2017-08-12 20:29:01 +00:00
2017-10-09 04:44:27 +00:00
foreach ( $where as $item ) {
Assert :: true ( is_scalar ( $item ) || is_null ( $item ), 'First argument passed to updateOrCreate should be an array of scalar or null values, received an array value of %s.' );
}
2017-07-25 02:34:10 +00:00
$instance = $this -> withColumns ( 'id' ) -> findWhere ( $where ) -> first ();
if ( ! $instance ) {
return $this -> create ( array_merge ( $where , $fields ), $validate , $force );
}
return $this -> update ( $instance -> id , $fields , $validate , $force );
}
2017-07-01 20:29:49 +00:00
}