Returns a collection of previous months

@param string $target
@param string $withCurrent Whether to include $target in resultset
@return array
481594
public static function getPreviousMonths($target, $withCurrent = true)
    {
        return self::getMonthSequence(array_keys(self::getMonths()), $target, $withCurrent);
    }
577098
private static function getNearestWeekday($currentYear, $currentMonth, $targetDay)
    {
        $tday = str_pad($targetDay, 2, '0', STR_PAD_LEFT);
        $target = DateTime::createFromFormat('Y-m-d', "$currentYear-$currentMonth-$tday");
        $currentWeekday = (int) $target->format('N');

        if ($currentWeekday < 6) {
            return $target;
        }

        $lastDayOfMonth = $target->format('t');

        foreach (array(-1, 1, -2, 2) as $i) {
            $adjusted = $targetDay + $i;
            if ($adjusted > 0 && $adjusted <= $lastDayOfMonth) {
                $target->setDate($currentYear, $currentMonth, $adjusted);
                if ($target->format('N') < 6 && $target->format('m') == $currentMonth) {
                    return $target;
                }
            }
        }
    }
902180
private static function getMonthSequence(array $months, $target, $withCurrent)
    {
        if (!in_array($target, $months)) {
            throw new LogicException(
                sprintf('Target month "%s" is out of range. The range must be from 01 up to 12', $target)
            );
        }

        $collection = array();

        foreach ($months as $month) {
            if ($month == $target) {
                if ($withCurrent === true) {
                    $collection[] = $month;
                }
                break;
            } else {
                $collection[] = $month;
            }
        }

        return $collection;
    }
388431
public static function data_get($target, $key, $default = null)
	{
		if (is_null($key)) {
			return $target;
		}
		$key = is_array($key) ? $key : explode('.', $key);
		while (! is_null($segment = array_shift($key))) {
			if ($segment === '*') {
				if (! is_array($target)) {
					return $default;
				}
				$result = static::pluck($target, $key);
				return in_array('*', $key) ? static::collapse($result) : $result;
			}
			if (static::accessible($target) && static::exists($target, $segment)) {
				$target = $target[$segment];
			} elseif (is_object($target) && isset($target->{$segment})) {
				$target = $target->{$segment};
			} else {
				return $default;
			}
		}
		return $target;
	}
252860
public function with($with)
    {
        foreach ($this->queries as $query) {
            $query->with($with);
        }

        return $this;
    }
495470
public function toArray($withNull = false)
    {
        $result = [];
        foreach (get_object_vars($this) as $property => $value) {
            $getter = 'get' . str_replace('_', '', $property);
            if (method_exists($this, $getter)) {
                $result[$property] = $this->{$getter}();
            } else {
                $result[$property] = $this->{$property};
            }
        }
        if ($withNull) {
            return $result;
        }

        return array_filter(
            $result, function ($value) {
            return isset($value);
        }
        );
    }
157892
public static function getScheduledAClassContentUpdates(EntityManager $em, $with_revision = false, $exclude_base_element = false)
    {
        $entities = array();
        foreach ($em->getUnitOfWork()->getScheduledEntityUpdates() as $entity) {
            if (false !== $tmp = self::getScheduledEntity($entity, $with_revision, $exclude_base_element)) {
                $entities[] = $tmp;
            }
        }

        return $entities;
    }
529401
protected function whereWith($find, $column, $with, $where_role = '=')
    {
        $this->where($column, $where_role, $find);
        $this->with($with);

        return $this->model;
    }
896499
protected function parseQuery(&$data, $with, $cache)
    {
        $result = $this->with($with)->cache($cache);

        if ((is_array($data) && key($data) !== 0) || $data instanceof Where) {
            $result = $result->where($data);
            $data   = null;
        } elseif ($data instanceof \Closure) {
            $data($result);
            $data = null;
        } elseif ($data instanceof Query) {
            $result = $data->with($with)->cache($cache);
            $data   = null;
        }

        return $result;
    }
321474
public function getBy(array $where = [], array $with = [])
    {
        $query = $this->make($with);

        $query = $this->applyWhere($query, $where);

        return $query->get();
    }
902461
public function getVotesList ($tag = null, bool $with = true) : array
    {
        return $this->_Votes->getVotesList(VoteUtil::tagsConvert($tag), $with);
    }
125150
public static function with( $with, $callback = null ) 
	{	
		if ( !is_array( $with ) )
		{
			$with = array( $with );
		}
		
		$settings = static::_model();
		
		$query = DB::select( $settings['table'] );
		
		// run the callback
		if ( !is_null( $callback ) )
		{
			call_user_func_array( $callback, array( &$query ) );
		}
		
		// alway group the result and fetch assoc
		$query->forward_key( $settings['primary_key'] );
		$query->fetch_arguments = array( 'assoc' );
		
		// get the main result set
		$results = static::assign( $query->run() );
		$singleton = false;
		
		if ( !is_array( $results ) )
		{
			$results = array( $results );
			$singleton = true;
		}
		
		$ref_object = reset( $results );
		
		// we have to sort the relationships to make sure that
		// select the relations in the right order.	
		asort( $with );
		
		foreach( $with as $relation => $callback )
		{
			if ( is_int( $relation ) && is_string( $callback ) )
			{
				$relation = $callback;
				$callback = null;
			}
			
			if ( strpos( $relation, '.' ) !== false )
			{	
				$relation_layers = explode( '.', $relation );
				
				$relation_name = array_pop( $relation_layers );
				
				$relation_collection = array();
				
				foreach( $results as $key => &$item )
				{
					$curr_item = $item;
					
					foreach( $relation_layers as $layer )
					{
						$curr_item = $curr_item->raw( $layer );
					}
					
					$relation_collection[] = $curr_item;
				}
				
				$ref_object = reset( $relation_collection );
				
				$relation_object = call_user_func( array( $ref_object, $relation_name ) );
				if ( $relation_object instanceof Model_Relation )
				{
					$relation_object->collection_assign( $relation_name, $relation_collection, $callback );
				}
			}
			else
			{
				$relation_object = call_user_func( array( $ref_object, $relation ) );
				if ( $relation_object instanceof Model_Relation )
				{
					$relation_object->collection_assign( $relation, $results, $callback );
				}
				
			}
		}
		
		if ( $singleton )
		{
			return reset( $results );
		}
		
		// and assign
		return $results;
	}
571798
public function populateTree($depth = null, $with = null)
    {
        /** @var ActiveRecord[]|static[] $nodes */
        $query = $this->getDescendants($depth);
        if ($with) {
            $query->with($with);
        }
        $nodes = $query->all();

        $relates = [];
        foreach ($nodes as $node) {
            $path = $node->getParentPath(true);
            $key = array_pop($path);
            if (!isset($relates[$key])) {
                $relates[$key] = [];
            }
            $relates[$key][] = $node;
        }

        $ownerDepth = $this->owner->getAttribute($this->depthAttribute);
        $nodes[] = $this->owner;
        foreach ($nodes as $node) {
            $key = $node->getAttribute($this->itemAttribute);
            if (isset($relates[$key])) {
                $node->populateRelation('children', $relates[$key]);
            } elseif ($depth === null || $ownerDepth + $depth > $node->getAttribute($this->depthAttribute)) {
                $node->populateRelation('children', []);
            }
        }

        return $this->owner;
    }
52826
public function allWith($with, $limit = null)
    {
        if ($limit) {
            return $this->model->with($with)->limit($limit)->get();
        }

        return $this->model->with($with)->get();

    }
860888
protected function mapJoinColumns(array $target = [], array $source = [])
    {
        $joinColumns = [];
        foreach ($target as $index => $joinColumn) {
            if (isset($source[$index])) {
                $diff = array_diff($joinColumn, $source[$index]);

                if (!empty($diff)) {
                    $joinColumns[] = $diff;
                }
            }
        }

        return $joinColumns;
    }
662945
public function getMessages($withWarning=false) {
		if ($withWarning) $this->messageList->allErrorOrWarningArray();
		return $this->messageList->allErrorArray();
	}
340797
public function allFiltered($columns = [], array $with = [])
    {
        $params = request()->all();
        $data = $this->with($with)->select($columns);

        $orderBy = $params['orderBy'] ?? null;
        $query = $params['query'] ?? null;
        $limit = $params['limit'] ?? null;
        $page = $params['page'] ?? 1;
        $ascending = $params['ascending'] ?? 1;
        $byColumn = $params['byColumn'] ?? 0;

        if ($query !== null) {
            $data = $byColumn == 1 ? $this->filterByColumn($data, $query) :
                                     $this->filter($data, $query, $columns);
        }

        $count = $data->count();

        if ($limit !== null) {
            $data->limit($limit)->skip($limit * ($page - 1));
        }
        if ($orderBy !== null) {
            $orderBy .= $this->translatableOperator($orderBy);
            $direction = $ascending == 1 ? 'ASC' : 'DESC';
            $data->orderBy($orderBy, $direction);
        }

        $results = $data->get()
            ->translate(config('typicms.content_locale'));

        return [
            'data' => $results,
            'count' => $count,
        ];
    }