<commit_before><?php namespace Zeropingheroes\Lanager;

use InvalidArgumentException, ReflectionClass;

abstract class NestedResourceService extends BaseResourceService {

	protected $models;

	public function __construct( ResourceServiceListenerContract $listener, array $models )
	{
		foreach( $models as $model )
		{
			if( ! ($model instanceof BaseModel) ) throw new InvalidArgumentException('Models must be instances of BaseModel');
		}

		$this->models = $models;
		parent::__construct($listener);
	}

	private function nestedFindOrFail(array $ids)
	{
		$models = $this->models;

		// Only accept one less ID than the number of models in the nest
		if( (count($models) - count($ids)) > 1 )
		{
			throw new InvalidArgumentException('Expected a minimum of ' . count($models)-1 . ' IDs but ' . count($ids) . ' given' );
		}

		$i = 0;
		foreach( $ids as $id )
		{
			// initially, verify the first model of given id exists (and fetch it)
			if( $i == 0 ) $model = $models[$i]->findOrFail($id);

			// after 1st loop iteration, verify the current model of given id exists (and fetch it)
			if( $i > 0 ) $model = $model->findOrFail($id);

			// if we have not yet reached the bottom of the nest 
			if( ($i+1) < count($models) )
			{
				// get method name of the next model in the nest
				$children = str_plural((new ReflectionClass($models[$i+1]))->getShortName());

				// fetch all items of the next model, which belong to the current model
				$model = $model->{$children}();
			}

			$i++;
		}
		return $model;
	}

	public function all( array $ids )
	{
		return $this->nestedFindOrFail( $ids )->get();
	}

	public function single( array $ids )
	{
		return $this->nestedFindOrFail( $ids );
	}

	public function store( array $ids, $input)
	{
		$parent = $this->nestedFindOrFail( $ids );

		// get an instance of the model we want to create
		$child = end($this->models);

		$child = $child->fill($input);

		// find the name of the foreign key field for the parent of this new model 
		$foreignKeyField = substr($parent->getForeignKey(), strrpos($parent->getForeignKey(), '.') + 1);
		
		// set this new model's foreign key field for the parent to the last id
		$child->{$foreignKeyField} = end($ids);

		$validator = new $child->validator( $child->toArray() );

		if ( $validator->fails() )<commit_msg>Coding style fix<commit_after><?php namespace Zeropingheroes\Lanager;

use InvalidArgumentException, ReflectionClass;

abstract class NestedResourceService extends BaseResourceService {

	protected $models;

	public function __construct( ResourceServiceListenerContract $listener, array $models )
	{
		foreach( $models as $model )
		{
			if( ! ($model instanceof BaseModel) ) throw new InvalidArgumentException('Models must be instances of BaseModel');
		}

		$this->models = $models;
		parent::__construct($listener);
	}

	private function nestedFindOrFail(array $ids)
	{
		$models = $this->models;

		// Only accept one less ID than the number of models in the nest
		if( (count($models) - count($ids)) > 1 )
		{
			throw new InvalidArgumentException('Expected a minimum of ' . count($models)-1 . ' IDs but ' . count($ids) . ' given' );
		}

		$i = 0;
		foreach( $ids as $id )
		{
			// initially, verify the first model of given id exists (and fetch it)
			if( $i == 0 ) $model = $models[$i]->findOrFail($id);

			// after 1st loop iteration, verify the current model of given id exists (and fetch it)
			if( $i > 0 ) $model = $model->findOrFail($id);

			// if we have not yet reached the bottom of the nest 
			if( ($i+1) < count($models) )
			{
				// get method name of the next model in the nest
				$children = str_plural((new ReflectionClass($models[$i+1]))->getShortName());

				// fetch all items of the next model, which belong to the current model
				$model = $model->{$children}();
			}

			$i++;
		}
		return $model;
	}

	public function all( array $ids )
	{
		return $this->nestedFindOrFail( $ids )->get();
	}

	public function single( array $ids )
	{
		return $this->nestedFindOrFail( $ids );
	}

	public function store( array $ids, $input )
	{
		$parent = $this->nestedFindOrFail( $ids );

		// get an instance of the model we want to create
		$child = end($this->models);

		$child = $child->fill($input);

		// find the name of the foreign key field for the parent of this new model 
		$foreignKeyField = substr($parent->getForeignKey(), strrpos($parent->getForeignKey(), '.') + 1);
		
		// set this new model's foreign key field for the parent to the last id
		$child->{$foreignKeyField} = end($ids);

		$validator = new $child->validator( $child->toArray() );

		if ( $validator->fails() )
{'fraction_non_alphanumeric': 0.1379757785467128, 'fraction_numerical': 0.0038927335640138406, 'mean_word_length': 3.606573705179283, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 43, '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': '7654689', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1407, 'n_words': 486}