Model


Source

File: Model/Model.php

abstract class Model {

	public $timestamp = true;
	protected $columns = [];

	/**
	 * The table associated with the model.
	 *
	 * @var string
	 */
	protected $table;


	/**
	 * The primary key for the model.
	 *
	 * @var string
	 */

	protected $primaryKey = 'ID';

	/**
	 * The number of models to return for pagination.
	 *
	 * @var int
	 */
	protected $per_page;

	public function __construct( $model_id = 0 ) {
		global $wpdb;

		$this->table = $wpdb->prefix . $this->table;

		if ( ! empty( $model_id ) ) {
			$model_raw = $wpdb->get_row( " SELECT * FROM {$this->table} WHERE {$this->primaryKey} = {$model_id}  ",
				ARRAY_A );
			if ( $model_raw ) {
				$this->syncModel( $model_raw );
			}
		}

	}

	/**
	 * Save the model to the database.
	 *
	 * @param  array  $options
	 *
	 * @return bool|mixed
	 */
	public function save( array $options = [] ) {
		global $wpdb;

		$columns = $this->db_columns( $options );

		$is_insert = empty( $columns[ $this->primaryKey ] );

		if ( $is_insert ) {

			if ( $this->timestamp ) {
				$columns['created_at'] = dozent_mysql_time();
			}

			$wpdb->insert( $this->table, $columns );

			$insert_id = (int) $wpdb->insert_id;

		} else {

			$updated_columns = dozent_array_only( get_object_vars( $this ), array_keys( $columns ) );
			unset( $updated_columns[ $this->primaryKey ] );

			$wpdb->update( $this->table, $updated_columns, [ $this->primaryKey => $columns[ $this->primaryKey ] ] );
			$insert_id = (int) $columns[ $this->primaryKey ];
			$columns = array_merge( $columns, $updated_columns );
		}

		if ( $insert_id ) {
			$columns[ $this->primaryKey ] = $insert_id;

			$this->syncModel( $columns );

			return $this;
		}


		return new \WP_Error( 'dozent_model_saving_error', __( ' Something went wrong ', 'dozent' ) );
	}

	/**
	 * Modal valid database column
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 *
	 * @param $options
	 *
	 * @return array Database Column
	 */

	public function db_columns( $options ) {

		$columns = array_merge( get_object_vars( $this ), $this->columns );

		/**
		 * Filter default model properties as DB column which not required or not available in the actual DB table
		 *
		 * @since DozentLMS 1.0.0
		 */

		$db_column_except = apply_filters( 'dozent_db_columns_except',
			[ 'table', 'timestamp', 'columns', 'primaryKey', 'per_page' ] );

		$columns = dozent_array_except( $columns, $db_column_except );

		if ( dozent_count( $options ) ) {
			$columns = $options;
		}

		$columns = array_filter( $columns );

		return $columns;
	}

	protected function syncModel( $columns = [] ) {
		$this->columns = array_merge( $this->columns, $columns );

		$columns = $this->columns;

		if ( ! empty( $columns[ $this->primaryKey ] ) ) {
			$primary_key = $columns[ $this->primaryKey ];
			unset( $columns[ $this->primaryKey ] );

			$columns = array_merge( [ $this->primaryKey => $primary_key ], $columns );
		}

		foreach ( $columns as $property => $value ) {
			$this->{$property} = $value;
		}

		$this->columns = $columns;
	}

	/**
	 * Dynamically retrieve attributes on the model.
	 *
	 * @param  string  $key
	 *
	 * @return mixed
	 */
	public function __get( $key ) {
		return $this->getAttribute( $key );
	}

	public function getAttribute( $key ) {
		$value = dozent_array_get( $key, $this->columns );

		return $value;
	}

	/**
	 * Dynamically set attributes on the model.
	 *
	 * @param  string  $key
	 * @param  mixed  $value
	 *
	 * @return void
	 */

	public function __set( $key, $value ) {
		$this->columns[ $key ] = $value;
	}

	/**
	 * Convert the model to its string representation.
	 *
	 * @return string
	 */
	public function __toString() {
		return $this->toJson();
	}

	/**
	 * Convert the model to array
	 *
	 *
	 * @return array
	 */

	public function toArray() {
		return (array) $this;
	}

	public function toJson( $options = 0 ) {

		$json = json_encode( $this->toArray(), $options );

		if ( JSON_ERROR_NONE !== json_last_error() ) {

			throw new \Exception( 'Error encoding model [' . get_class( $this ) . '] to JSON: '
			                      . json_last_error_msg() );

		}

		return $json;
	}

}

Methods