<?php
namespace SabaiApps\Directories\Component\Field\Type;

use SabaiApps\Directories\Component\Field\IField;
use SabaiApps\Directories\Component\Field\Query;
use SabaiApps\Directories\Component\Entity\Model\Bundle;

trait QueryableRangeTrait {

	public function fieldQueryableInfo( IField $field, $inAdmin = false ) {
		return array(
			'example' => '1,10',
			'tip'     => __( 'Enter a single number for exact match, two numbers separated with a comma for range search.', 'directories-pro' ),
		);
	}

	public function fieldQueryableQuery( Query $query, $fieldName, $paramStr, Bundle $bundle ) {
		$params = $this->_queryableParams( $paramStr, false );
		switch ( count( $params ) ) {
			case 0:
				break;
			case 1:
				if ( strlen( $params[0] )
					&& ( false !== $params[0] = $this->_getQueryableQueryValue( $params[0] ) )
				) {
					$query->fieldIsOrGreaterThan( $fieldName, $params[0], 'min' )
						->fieldIsOrSmallerThan( $fieldName, $params[0], 'max' );
				}
				break;
			default:
				if ( strlen( $params[0] )
					&& ( false !== $params[0] = $this->_getQueryableQueryValue( $params[0] ) )
				) {
					$query->fieldIsOrGreaterThan( $fieldName, $params[0], 'min' );
				}
				if ( strlen( $params[1] )
					&& ( false !== $params[1] = $this->_getQueryableQueryValue( $params[1] ) )
				) {
					$query->fieldIsOrSmallerThan( $fieldName, $params[1], 'max' );
				}
		}
	}

	/**
	 * @return mixed|false
	 */
	abstract protected function _getQueryableQueryValue( $value );
}
