Return a list of elements based on a set of contexts
@param array|string $contexts The parameter can be the name of an context, a comma delimited list of contexts or an array of contexts
@throws Exception
@return InstanceElementsFilter
964962
public function ElementsByContexts( $contexts )
	{
		if ( is_string( $contexts ) )
			$contexts = array_map( 'trim', explode( ",", $contexts ) );

		if ( is_a( $contexts, 'ContextsFilter' ) )
			$contexts = $contexts->Keys();

		if ( ! is_array( $contexts ) )
			throw new Exception( 'InstanceElementsFilter::ElementsByName $contexts must be a string or array' );

		$contexts = array_flip( array_map( 'strtolower', $contexts ) );

		// In PHP 7.0 $this is passed to closure functions automatically but not in earlier versions
		$elements = &$this->elements;

		$filtered = array_reduce( array_keys( $this->elements ), function( $carry, $elementName ) use( $contexts, &$elements ) {

			$contextElements = array_filter( $elements[ $elementName ], function( $element ) use( &$contexts, &$elements ) {

				if ( isset( $element['contextRef'] ) )
				{
					return isset( $contexts[ strtolower( $element['contextRef'] ) ] );
				}

				// Could be a tuple in which case the tuple members should be checked
				return array_reduce( $element['tuple_elements'], function( $carry, $tuple_element ) use( $contexts ) {

					if ( $carry ) return true; // Once one valid member is found there's no need to look for any others
					return array_reduce( $tuple_element, function( $carry, $entry ) use( $contexts ) {

						if ( $carry ) return true; // Once one valid member is found there's no need to look for any others
						return isset( $contexts[ strtolower( $entry['contextRef'] ) ] );

					} );

				}, false );

			} );

			if ( ! $contextElements ) return $carry;

			$carry[ $elementName ] = $contextElements;

			return $carry;

		}, array() );

		return new InstanceElementsFilter( $this->instance, $filtered );
	}
222489
public function NoSegmentContexts()
	{
		$filtered = array_filter( $this->contexts, function( $context ) {
			// The context may be invalid in which case exclude
			return ( ! isset( $context['entity']['segment']['explicitMember'] ) || count( $context['entity']['segment']['explicitMember'] ) == 0 ) &&
				   ( ! isset( $context['entity']['scenario']['explicitMember'] ) || count( $context['entity']['scenario']['explicitMember'] ) == 0 ) &&
				   ( ! isset( $context['segment']['explicitMember'] ) || count( $context['segment']['explicitMember'] ) == 0 ) &&
				   ( ! isset( $context['scenario']['explicitMember'] ) || count( $context['scenario']['explicitMember'] ) == 0 ) &&
				   ( ! isset( $context['entity']['segment']['typedMember'] ) || count( $context['entity']['segment']['typedMember'] ) == 0 ) &&
				   ( ! isset( $context['entity']['scenario']['typedMember'] ) || count( $context['entity']['scenario']['typedMember'] ) == 0 ) &&
				   ( ! isset( $context['segment']['typedMember'] ) || count( $context['segment']['typedMember'] ) == 0 ) &&
				   ( ! isset( $context['scenario']['typedMember'] ) || count( $context['scenario']['typedMember'] ) == 0 );		} );

		return new ContextsFilter( $this->instance, $filtered );
	}
1188023
public function setContext($context)
    {
        if (!is_array($context) && !$context instanceof \ArrayObject && !$context instanceof \Iterator) {
            throw new EventException('Unexpected value type for context', EventException::INVALID_CONTEXT);
        }

        $context = Collection::cast($context);

        $this->context = $context;

        return $this;
    }
122264
public function setContexts($contexts)
    {
        if ($contexts) {
            foreach ((array) $contexts as $name => $value) {
                $this->getContext()->set($name, $value);
            }
        }
    }
1285029
private static function parseContext($context)
    {
        if(!isset($context['trace']))
        {
            $context['trace'] = static::getTrace();
        }
        if(!isset($context['errorFile']))
        {
            list($file, $line) = static::getErrorFile();
            $context['errorFile'] = $file;
            $context['errorLine'] = $line;
        }
        return $context;
    }
962536
public function contextFactory(ContextFactory $instance = null, array $contexts = [])
    {
        if ($instance === null) {
            if ($this->_contextFactory === null) {
                $this->_contextFactory = ContextFactory::createWithDefaults($contexts);
            }

            return $this->_contextFactory;
        }
        $this->_contextFactory = $instance;

        return $this->_contextFactory;
    }
644693
public function findFiltersForUserAndContext($context, $userId, $locale)
    {
        $filters = $this->filterRepository->findByUserAndContextAndLocale($locale, $context, $userId);
        array_walk(
            $filters,
            function(&$filter) use ($locale) {
                $filter = new Filter($filter, $locale);
            }
        );

        return $filters;
    }
282011
public function ElementsByItemType( $types )
	{
		if ( is_string( $types ) )
			$types = array_map( 'trim', explode( ",", $types ) );

		if ( ! is_array( $types ) )
			throw new Exception( 'InstanceElementsFilter::ElementsByName $types must be a string or array' );

		$types = array_flip( array_map( 'strtolower', $types ) );

		// In PHP 7.0 $this is passed to closure functions automatically but not in earlier versions
		$elements = &$this->elements;

		$filtered = array_reduce( array_keys( $this->elements ), function( $carry, $elementName ) use( &$elements, $types  ) {

			$namespaceElements = array_filter( $elements[ $elementName ], function( $entry ) use( $types ) {
				$type = $entry['taxonomy_element']['type'];
				return isset( $types[ strtolower( $type ) ] );
			} );

			if ( ! $namespaceElements ) return $carry;

			$carry[ $elementName ] = $namespaceElements;

			return $carry;

		}, array() );

		return new InstanceElementsFilter( $this->instance, $filtered );
	}
1081232
public function getContextDimensions( $context )
	{
		if ( isset( $context['entity']['segment'] ) )
			return $this->getComponentDimensions( $context['entity']['segment'] );
		elseif ( isset( $context['segment'] ) )
			return $this->getComponentDimensions( $context['segment'] );
		elseif ( isset( $context['entity']['scenario'] ) )
			return $this->getComponentDimensions( $context['entity']['scenario'] );
		elseif ( isset( $context['scenario'] ) )
			return $this->getComponentDimensions( $context['scenario'] );
		else
			return array();
	}
71075
public function addContexts($contexts)
    {
        if (is_array($contexts)) {
            foreach ($contexts as $context) {
                $this->insertContextInAllowedExtensions($context);
            }
        } elseif ($contexts !== false) {
            $this->insertContextInAllowedExtensions($contexts);
        }

        return $this;
    }
772614
public function from(GeneratedValueSingle $elements, $exception)
    {
        $branches = [];

        $shrink = function ($elements) use (&$elementsAfterShrink, &$branches) {
            $branches = [];
            $elementsAfterShrink = $this->generator->shrink($elements);
            if ($elementsAfterShrink instanceof GeneratedValueOptions) {
                foreach ($elementsAfterShrink as $each) {
                    $branches[] = $each;
                }
            } else {
                $branches[] = $elementsAfterShrink;
            }
            return $branches;
        };

        $onGoodShrink = function ($elementsAfterShrink, $exceptionAfterShrink) use (&$elements, &$exception, &$branches, $shrink) {
            $elements = $elementsAfterShrink;
            $exception = $exceptionAfterShrink;
            $branches = $shrink($elements);
        };

        $this->timeLimit->start();
        $shrink($elements);
        while ($elementsAfterShrink = array_shift($branches)) {
            if ($this->timeLimit->hasBeenReached()) {
                throw new \RuntimeException(
                    "Eris has reached the time limit for shrinking ($this->timeLimit), here it is presenting the simplest failure case." . PHP_EOL
                    . "If you can afford to spend more time to find a simpler failing input, increase it with the annotation \'@eris-shrink {seconds}\'.",
                    -1,
                    $exception
                );
            }
            // TODO: maybe not necessary
            // when Generator start returning emtpy options instead of the
            // element itself upon no shrinking
            // For now leave in for BC
            if ($elementsAfterShrink == $elements) {
                continue;
            }

            if (!$this->checkGoodShrinkConditions($elementsAfterShrink)) {
                continue;
            }

            foreach ($this->onAttempt as $onAttempt) {
                $onAttempt($elementsAfterShrink);
            }

            Evaluation::of($this->assertion)
                ->with($elementsAfterShrink)
                ->onFailure($onGoodShrink)
                ->execute();
        }

        throw $exception;
    }
609912
public function getPluginNodesWithViewDefinitions(ContentContext $context)
    {
        $pluginNodes = [];
        foreach ($this->getPluginNodes($context) as $pluginNode) {
            /** @var NodeInterface $pluginNode */
            if ($this->getPluginViewDefinitionsByPluginNodeType($pluginNode->getNodeType()) !== []) {
                $pluginNodes[] = $pluginNode;
            }
        }
        return $pluginNodes;
    }
731116
private function parseTagBlock(string $tags, Types\Context $context): array
    {
        $tags = $this->filterTagBlock($tags);
        if (!$tags) {
            return [];
        }

        $result = $this->splitTagBlockIntoTagLines($tags);
        foreach ($result as $key => $tagLine) {
            $result[$key] = $this->tagFactory->create(trim($tagLine), $context);
        }

        return $result;
    }
786486
private function loadTypes(&$tags, \DOMXPath $xpath, \DOMNode $context = null)
    {
        $result = [];

        /** @var \DOMElement $node */
        foreach ($xpath->query('x:types/x:type', $context) as $node) {
            $value = $this->loadType($xpath, $node, $tags);
            $result[$value['name']] = $value;
        }

        return $result;
    }
532541
public static function get_editable_types($context = null) {

        if (empty($context)) {
            $context = context_system::instance();
        }

        $types= repository::get_types(true);
        $editabletypes = array();
        foreach ($types as $type) {
            $instanceoptionnames = repository::static_function($type->get_typename(), 'get_instance_option_names');
            if (!empty($instanceoptionnames)) {
                if ($type->get_contextvisibility($context)) {
                    $editabletypes[]=$type;
                }
             }
        }
        return $editabletypes;
    }
1070605
public static function get_available_in_context($contexts) {
        $params = self::validate_parameters(self::get_available_in_context_parameters(), array('contexts' => $contexts));
        $filters = $warnings = array();

        foreach ($params['contexts'] as $contextinfo) {
            try {
                $context = self::get_context_from_params($contextinfo);
                self::validate_context($context);
                $contextinfo['contextid'] = $context->id;
            } catch (Exception $e) {
                $warnings[] = array(
                    'item' => 'context',
                    'itemid' => $context['instanceid'],
                    'warningcode' => $e->getCode(),
                    'message' => $e->getMessage(),
                );
                continue;
            }
            $contextfilters = filter_get_available_in_context($context);

            foreach ($contextfilters as $filter) {
                $filters[] = array_merge($contextinfo, (array) $filter);
            }
        }

        return array(
            'filters' => $filters,
            'warnings' => $warnings,
        );
    }
1314983
public static function Collections( $context, $provider, $collection )
	{
		if ( ! ExtFuncs::validateUri( $collection ) || ! isset( $context->availableCollections[ $collection ] ) )
		{
			throw XPath2Exception::withErrorCodeAndParam( "FODC0004", Resources::FODC0004, $collection );
		}

		return $context->availableCollections[ $collection ];
	}