<?php
namespace SabaiApps\Directories\Component\WordPressUser\Helper;

use SabaiApps\Directories\Application;
use SabaiApps\Framework\Criteria\CompositeCriteria;
use SabaiApps\Framework\Criteria\ContainsCriteria;

class InRolesCriteriaHelper {

	protected static $_count = 0;

	public function help( Application $application, array $roles ) {
		global $wpdb;
		$alias    = 'wp_usermeta_role' . ++self::$_count;
		$target   = array(
			'tables'      => array(
				$wpdb->prefix . 'usermeta' => array(
					'alias' => $alias,
					'on'    => 'meta_key = \'' . esc_sql( $wpdb->prefix . 'capabilities' ) . '\'',
				),
			),
			'column'      => $alias . '.meta_value',
			'column_type' => Application::COLUMN_TEXT,
		);
		$criteria = new CompositeCriteria();
		foreach ( $roles as $role ) {
			$criteria->addOr( new ContainsCriteria( $target, '"' . $role . '"' ) );
		}
		return $criteria;
	}
}
