Tribe__Repository::by( $key, $value = null )
{@inheritdoc}
Source
File: src/Tribe/Repository.php
public function by( $key, $value ) {
if ( $this->void_query ) {
// No point in doing more computations if the query is void.
return $this;
}
$call_args = func_get_args();
$this->current_filters[ $key ] = $value;
try {
$query_modifier = $this->modify_query( $key, $call_args );
/**
* Here we allow the repository to call one of its own methods and return `null`.
* A repository might have a `where` or `by` that is just building
* a more complex query using a base `where` or `by`.
*/
if ( null === $query_modifier ) {
return $this;
}
/**
* Primitives are just merged in.
* Since we are using `array_merge_recursive` we expect them to be arrays.
*/
if ( ! ( is_object( $query_modifier ) || is_callable( $query_modifier ) ) ) {
if ( ! is_array( $query_modifier ) ) {
throw new InvalidArgumentException( 'Query modifier should be an array!' );
}
$replace_modifiers = in_array( $key, $this->replacing_modifiers(), true );
if ( $replace_modifiers ) {
/**
* We do a merge to make sure new values will override and replace the old
* ones.
*/
$this->query_args = array_merge( $this->query_args, $query_modifier );
} else {
/**
* We do a recursive merge to allow "stacking" of same kind of queries;
* e.g. two or more `tax_query`.
*/
$this->query_args = array_merge_recursive( $this->query_args, $query_modifier );
}
} else {
/**
* If we get back something that is not an array then we add it to
* the stack of query modifying callbacks we'll call on the query
* after building it.
*/
$this->query_modifiers[] = $query_modifier;
}
} catch ( Tribe__Repository__Void_Query_Exception $e ) {
/**
* We allow for the `apply` method to orderly fail to micro-optimize.
* If applying one parameter would yield no results then let's immediately bail.
* Schema should throw t
* his Exception if a light-weight on the filters would already
* deem a query as yielding nothing.
*/
$this->void_query = true;
return $this;
}
/**
* Catching other type of exceptions is something the client code should handle!
*/
return $this;
}