<commit_before><?php namespace CoandaCMS\Coanda\Urls\Repositories\Eloquent;

use Coanda;

use CoandaCMS\Coanda\Urls\Exceptions\UrlAlreadyExists;
use CoandaCMS\Coanda\Urls\Exceptions\InvalidSlug;
use CoandaCMS\Coanda\Urls\Exceptions\UrlNotFound;

use CoandaCMS\Coanda\Urls\Repositories\Eloquent\Models\Url as UrlModel;

class EloquentUrlRepository implements \CoandaCMS\Coanda\Urls\Repositories\UrlRepositoryInterface {

	private $model;
	private $slugifier;

	public function __construct(UrlModel $model, \CoandaCMS\Coanda\Urls\Slugifier $slugifier)
	{
		$this->model = $model;
		$this->slugifier = $slugifier;
	}

	public function findFor($for, $for_id)
	{
		$url = $this->model->whereUrlableType($for)->whereUrlableId($for_id)->first();

		if ($url)
		{
			return $url;
		}

		throw new UrlNotFound('Url not found');		
	}

	/**
	 * Tries to find the Eloquent URL model by the id
	 * @param  integer $id
	 * @return Array
	 */
	public function findById($id)
	{
		$url = $this->model->find($id);

		if (!$url)
		{
			throw new UrlNotFound('Url #' . $id . ' not found');
		}
		
		return $url;
	}

	/**
	 * Tries to find the Eloquent URL model by the slug
	 * @param  integer $id
	 * @return Array
	 */
	public function findBySlug($slug)
	{
		// Can we match this slug directly?
		$url = $this->model->whereSlug($slug)->first();

		if ($url)
		{
			return $url;
		}

		$slug_parts = explode('/', $slug);

		foreach ($slug_parts as $slug_part)
		{
			$url = $this->model->whereSlug($slug_part)->whereUrlableType('wildcard')->first();

			if ($url)
			{
				return $url;
			}
		}

		throw new UrlNotFound('Url for /' . $slug . ' not found');
	}

	public function register($slug, $for, $for_id)
	{
		// Is this a valid slug?
		if (!$this->slugifier->validate($slug))<commit_msg>Only run the wildcard match if we have a /<commit_after><?php namespace CoandaCMS\Coanda\Urls\Repositories\Eloquent;

use Coanda;

use CoandaCMS\Coanda\Urls\Exceptions\UrlAlreadyExists;
use CoandaCMS\Coanda\Urls\Exceptions\InvalidSlug;
use CoandaCMS\Coanda\Urls\Exceptions\UrlNotFound;

use CoandaCMS\Coanda\Urls\Repositories\Eloquent\Models\Url as UrlModel;

class EloquentUrlRepository implements \CoandaCMS\Coanda\Urls\Repositories\UrlRepositoryInterface {

	private $model;
	private $slugifier;

	public function __construct(UrlModel $model, \CoandaCMS\Coanda\Urls\Slugifier $slugifier)
	{
		$this->model = $model;
		$this->slugifier = $slugifier;
	}

	public function findFor($for, $for_id)
	{
		$url = $this->model->whereUrlableType($for)->whereUrlableId($for_id)->first();

		if ($url)
		{
			return $url;
		}

		throw new UrlNotFound('Url not found');		
	}

	/**
	 * Tries to find the Eloquent URL model by the id
	 * @param  integer $id
	 * @return Array
	 */
	public function findById($id)
	{
		$url = $this->model->find($id);

		if (!$url)
		{
			throw new UrlNotFound('Url #' . $id . ' not found');
		}
		
		return $url;
	}

	/**
	 * Tries to find the Eloquent URL model by the slug
	 * @param  integer $id
	 * @return Array
	 */
	public function findBySlug($slug)
	{
		// Can we match this slug directly?
		$url = $this->model->whereSlug($slug)->first();

		if ($url)
		{
			return $url;
		}

		if (strpos($slug, '/'))
		{
			$slug_parts = explode('/', $slug);

			foreach ($slug_parts as $slug_part)
			{
				$url = $this->model->whereSlug($slug_part)->whereUrlableType('wildcard')->first();

				if ($url)
				{
					return $url;
				}
			}			
		}

		throw new UrlNotFound('Url for /' . $slug . ' not found');
	}

	public function register($slug, $for, $for_id)
	{
		// Is this a valid slug?
		if (!$this->slugifier->validate($slug))
{'fraction_non_alphanumeric': 0.15189174261253796, 'fraction_numerical': 0.0, 'mean_word_length': 3.5559748427672955, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '420555', 'n_tokens_mistral': 1475, 'n_tokens_neox': 1333, 'n_words': 290}