Segments batches acording to the specified rules

@param array $source Source input
@return array Batches
338924
protected function segmentBatches($source)
    {
        // Measure batch_size
        $batchSize = static::config()->get('batch_size');
        if ($batchSize === 0) {
            return array($source);
        }
        $softCap = static::config()->get('batch_soft_cap');

        // Clear batches
        $batches = array();
        $current = array();
        $currentSize = 0;

        // Build batches from data
        foreach ($source as $base => $statefulids) {
            if (!$statefulids) {
                continue;
            }

            foreach ($statefulids as $stateKey => $statefulid) {
                $state = $statefulid['state'];
                $ids = $statefulid['ids'];
                if (!$ids) {
                    continue;
                }

                // Extract items from $ids until empty
                while ($ids) {
                    // Estimate maximum number of items to take for this iteration, allowing for the soft cap
                    $take = $batchSize - $currentSize;
                    if (count($ids) <= $take + $softCap) {
                        $take += $softCap;
                    }
                    $items = array_slice($ids, 0, $take, true);
                    $ids = array_slice($ids, count($items), null, true);

                    // Update batch
                    $currentSize += count($items);
                    $merge = array(
                        $base => array(
                            $stateKey => array(
                                'state' => $state,
                                'ids' => $items
                            )
                        )
                    );
                    $current = $current ? array_merge_recursive($current, $merge) : $merge;
                    if ($currentSize >= $batchSize) {
                        $batches[] = $current;
                        $current = array();
                        $currentSize = 0;
                    }
                }
            }
        }
        // Add incomplete batch
        if ($currentSize) {
            $batches[] = $current;
        }

        return $batches;
    }
217846
protected function createSingle(array $source)
    {
        $data = [];
        /** @var FieldDescriptor $field */
        foreach ($this->getDescriptor()->getFields() as $field) {
            if (array_key_exists($field->getField(), $source)) {
                $data[$field->getName()] = $source[$field->getField()];
            }
        }
        $class = $this->getDescriptor()->className();
        return new $class($data);
    }
1373870
private function makeArrayFromOptionsAndValues(array $source, array &$destination): array
    {
        $destination = [];

        foreach ($source as $value) {
            list($k, $v) = explode("=", $value);
            $destination[trim($k, "- ")] = trim($v);
        }

        return $destination;
    }
398470
public function models(Source $source = null) {
		if ($source) {
			$this->applyStrategy($source);
		}
		return $this->_models;
	}
381626
private function niceMerge(array &$source, array &$destination): array
    {
        $merged = $source;

        foreach ($destination as $key => &$value) {
            if (
                    $this->mergeRecursive &&
                    isset($merged[$key]) &&
                    is_array($merged[$key])
            ) {
                $merged[$key] = $this->niceMerge($source[$key], $value);
                continue;
            }

            $merged[$key] = $value;
        }

        return $merged;
    }
1135920
public function batch(int $n = 50) : array
    {
        $batches = [];

        $samples = $this->samples;

        foreach (array_chunk($this->samples, $n) as $batch) {
            $batches[] = self::quick($batch);
        }

        return $batches;
    }
40734
public function includeDids(Source $source)
    {
        $dids = $source->dids;

        return $this->collection($dids, new DidTransformer);
    }
126590
protected function getSourcesFromFinder($source)
    {
        $array = explode(DIRECTORY_SEPARATOR, $source);
        $filename = array_pop($array);
        $directory = $source;
        $pattern = '*';
        
        // if a dot is present, the last part is the filename pattern
        if (strstr($filename, '.') !== false) {
            $pattern = $filename;
            $directory = implode('/', $array);
        }
        $finder = new Finder();
        $finder
            ->name($pattern)
            ->in($directory);
        
        $sources = [];
        
        foreach ($finder as $source) {
            $sources[] = $source;
        }
        
        return $sources;
    }
50362
private function createMapping(AbstractTable $source, AbstractTable $target)
    {
        $mapping = [];
        foreach ($target->getColumns() as $name => $column) {
            if ($source->hasColumn($name)) {
                $mapping[$name] = $column->getName();
            }
        }

        return $mapping;
    }
648959
public static function compact($source, $fields)
    {
        $data = array();
        foreach ($fields as $field) {
            if (isset($source[$field])) $data[$field] = $source[$field];
        }
        return $data;
    }
358524
public function getAttributes($source): array
    {
        return [
            self::CLIENT_MIME_TYPE => $source->getClientMimeType(),
            self::CLIENT_NAME => $source->getClientOriginalName(),
            self::TMP_PATH => $source->getPathname(),
        ];
    }
497585
public function Concat( $source )
	{

		$source = is_array($source) ? $source : ( $source instanceof Queryable ? $source->toList() : iterator_to_array($source) );

		return $this
					->getClone()
					->setRepository( array_merge( $this->repository , $source ) );

	}
234240
public static function arrayFilterRecursive(Array $source, $fn)
    {
        $result = [];
        foreach ($source as $key => $value) {
            if (is_array($value)) {
                $result[$key] = static::arrayFilterRecursive($value, $fn);
                continue;
            }
            if ($fn($key, $value)) {
                $result[$key] = $value; // KEEP
                continue;
            }
        }

        return $result;
    }
1056127
public static final function prepend(array $Source, array $Prepend): array {
		return array_merge($Prepend, self::only($Source, array_filter(array_keys($Source), function($value) use ($Prepend){
			return is_int($value) || !array_key_exists($value, $Prepend); })));
	}
545097
public function batch(int $n = 50) : array
    {
        $sChunks = array_chunk($this->samples, $n);
        $lChunks = array_chunk($this->labels, $n);

        $batches = [];

        foreach ($sChunks as $i => $samples) {
            $batches[] = self::quick($samples, $lChunks[$i]);
        }

        return $batches;
    }
687068
private function queryToArray(SS_Query $source) {
        $result=array();
        
        foreach($source as $row) {
            $result[]=$row;
        }
        
        return $result;
    }
48373
public static function objectToArray($source): array
    {
        $result = [];

        if (is_array($source)) {
            return $source;
        }

        if (!is_object($source)) {
            return $result;
        }

        $json = json_encode($source);
        if ($json === false) {
            return $result;
        }

        $array = json_decode($json, true);
        if ($array === null) {
            return $result;
        }
        $result = $array;

        return $result;
    }