<?php
namespace library;

trait Orderable {
    
    public function moveUp() {
        if ( $this->position > 1 ) {
            $previous = self::findOne( [$this->getPrimaryKey(), 'position'], ['position' => $this->position - 1] );
            $previous->increment( 'position' );
            $this->decrement( 'position' );
        }
    }
    
    public function moveDown() {
        if ( $this->position < $this->maximum( 'position' ) ) {
            $next = self::findOne( [$this->getPrimaryKey(), 'position'], ['position' => $this->position + 1] );
            $next->decrement( 'position' );
            $this->increment( 'position' );
        }
    }
}
