<commit_before><?php

namespace prgTW\BaseCRM\Resource\Partial;

use prgTW\BaseCRM\Resource\DetachedResource;
use prgTW\BaseCRM\Resource\InstanceResource;
use prgTW\BaseCRM\Resource\ListResource;
use prgTW\BaseCRM\Resource\ResourceCollection;

trait CreateResource
{
	/**
	 * @param DetachedResource $resource
	 * @param bool             $useKey
	 *
	 * @throws \InvalidArgumentException on resource scopes mismatch
	 * @return InstanceResource|ListResource
	 */
	public function create(DetachedResource $resource, $useKey = true)
	{
		$newResourceName   = $resource->getResourceName();
		$childResourceName = $this->getChildResourceName();
		if ($newResourceName !== $childResourceName)
		{
			//@codeCoverageIgnoreStart
			throw new \InvalidArgumentException(sprintf('Cannot create resource "%s" under resource "%s"', $newResourceName, $this->getResourceName()));
			//@codeCoverageIgnoreEnd
		}

		$uri        = $this->getFullUri();
		$options    = [];
		$dehydrated = $resource->dehydrate();
		if (true === $useKey)
		{
			$options['query'] = [$childResourceName => $dehydrated];
		}
		else
		{
			$options['query'] = $dehydrated;
		}
		$data = $this->transport->post($uri, null, $options);

		if (array_key_exists($childResourceName, $data))
		{
			$data = $this->getObjectFromArray($data[$childResourceName]);
		}
		else
		{
			$data = array_map([$this, 'getObjectFromArray'], $data);

			return new ResourceCollection($data);
		}

		return $data;
	}
}<commit_msg>minor fix<commit_after><?php

namespace prgTW\BaseCRM\Resource\Partial;

use prgTW\BaseCRM\Resource\DetachedResource;
use prgTW\BaseCRM\Resource\InstanceResource;
use prgTW\BaseCRM\Resource\ListResource;
use prgTW\BaseCRM\Resource\ResourceCollection;
use prgTW\BaseCRM\Transport\Transport;

/**
 * @property Transport transport
 */
trait CreateResource
{
	/**
	 * @param DetachedResource $resource
	 * @param bool             $useKey
	 *
	 * @throws \InvalidArgumentException on resource scopes mismatch
	 * @return InstanceResource|ListResource
	 */
	public function create(DetachedResource $resource, $useKey = true)
	{
		$newResourceName   = $resource->getResourceName();
		$childResourceName = $this->getChildResourceName();
		if ($newResourceName !== $childResourceName)
		{
			//@codeCoverageIgnoreStart
			throw new \InvalidArgumentException(sprintf('Cannot create resource "%s" under resource "%s"', $newResourceName, $this->getResourceName()));
			//@codeCoverageIgnoreEnd
		}

		$uri        = $this->getFullUri();
		$options    = [];
		$dehydrated = $resource->dehydrate();
		if (true === $useKey)
		{
			$options['query'] = [$childResourceName => $dehydrated];
		}
		else
		{
			$options['query'] = $dehydrated;
		}
		$data = $this->transport->post($uri, null, $options);

		if (array_key_exists($childResourceName, $data))
		{
			$data = $this->getObjectFromArray($data[$childResourceName]);
		}
		else
		{
			$data = array_map([$this, 'getObjectFromArray'], $data);

			return new ResourceCollection($data);
		}

		return $data;
	}

	abstract function getResourceName($resourceClassName = null);

	abstract function getChildResourceName($resourceClassName = null);

	abstract function getObjectFromArray(array $params, $instanceClassName = null, $idParam = 'id');

	abstract function getFullUri($suffix = '');
}
{'fraction_non_alphanumeric': 0.14207650273224043, 'fraction_numerical': 0.0, 'mean_word_length': 4.642123287671233, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '6869117', 'n_tokens_mistral': 1148, 'n_tokens_neox': 1021, 'n_words': 197}