Download the PHP package brimshot/pattr without Composer
On this page you can find all versions of the php package brimshot/pattr. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download brimshot/pattr
More information about brimshot/pattr
Files in brimshot/pattr
Package pattr
Short Description An easy way to work with attributes in PHP
License MIT
Informations about the package pattr
Pattr
A straightforward and simple library for using attributes in PHP.
Installation
Install with composer:
Or download directly from:
https://github.com/brimshot/Pattr
Usage
Pattr provides functions meant to simplify working with attributes in PHP.
Make the functions accessible in your code by including vendor/autoload.php
(when using composer) or by including src/Pattr.php
directly.
Import each function you intend to use with a use
directive at the top of your source file, i.e.:
The functions in the library operate on classes (or instantiated objects), public class properties, class methods, class constants and globally available functions.
To get information about a class or object property or method, pass the details as an array i.e. [Object, MethodName]
Function list
has_attribute(mixed $item, string|array $attribute_or_array_of_attributes, bool $matchChildAttributes = true) : bool
Returns true or false whether the provided item has the provided attribute or all of the provided attributes if $attribute_or_array_of_attributes
is an array.
By default, child classes of provided attributes will be matched but this can be disabled (strict mode) by passing false as the third argument.
$attribute_or_array_of_attributes
can also be a string (or strings) in which case the unqualified classname will be matched.
Example:
has_attribute_callback(mixed $item, string $attribute, callable $callback, $matchChildAttributes = true) : bool
Returns true or false whether the provided item has the provided attribute and the attribute, when instantiated, satisfies the provided callback. By default, child attributes of the provided attribute will also be matched.
Example:
does_not_have_attribute(mixed $item, string|array $attribute_or_array_of_attributes, bool $matchChildAttributes = true) : bool
Returns true if the provided item does not have the provided attribute or if $attribute_or_array_of_attributes
is an array, returns true if the item does not have any of the provided attributes.
Example:
get_attribute_names(mixed $item, bool $shortNames = false) : array
Returns the names of the provided items attributes. Default is the fully qualified class name, but names can be shortened by passing true for $shortNames
.
Example:
get_attribute(mixed $item, string $attribute, bool $matchChildAttributes = true, int $index = 0) : ?object
Returns an instance of the requested attribute from the provided item, or null if item does not have the desired attribute.
An optional index parameter can be provided for use with repeated attributes to select which to return (index begins at 0).
Example:
get_attributes(mixed $item, array $attribute_list = [], bool $matchChildAttributes = true) : array
Returns an array of instances of the provided items attributes. Can be filtered to a desired subset by passing in $attribute_list
. By default, children of attributes provided in the filter list are included in the result set.
Example:
get_attributes_callback(mixed $item, string $attribute, callable $callback, $matchChildAttributes = true) : array
Returns an array of instances of the requested attribute (array contains multiple results on repeated attributes) from the provided item when those attribute(s) satisfy the provided callback, or an empty array when none match.
get_class_methods_with_attribute(object|string $object_or_class, string|array $attribute_or_array_of_attributes, bool $matchChildAttributes = true) : array
Returns an array of method names from the given class or object that have the provided attribute or array of attributes. By default, children of attributes provided in search list are included in the result set.
Example:
get_class_methods_with_attribute_callback(object|string $object_or_class, string $attribute, callable $callback, bool $matchChildAttributes = true) : array
Returns an array of method names from the given class or object that have the provided attribute and that attribute satisfies the provided callback. By default, children of attributes provided in search list are included in the result set.
Example:
get_object_properties_with_attribute(object $object, string|array $attribute_or_array_of_attributes, bool $matchChildAttributes = true) : array
Returns an array of property names and their values from an instantiated object that have the provided attributes or array of attributes. By default, children of attributes provided in search list are included in the result set.
get_object_properties_with_attribute_callback(object $object, string $attribute, callable $callback, $matchChildAttributes = true) : array
Returns an array of property names and their values from an instantiated object that have the provided attribute and that attribute satisfies the provided callback.
Example:
get_class_properties_with_attribute(string $class, string|array $attribute_or_array_of_attributes, bool $matchChildAttributes = true) : array
Returns an array of property names from a class (not instantiated) that have the provided attributes or array of attributes. By default, children of attributes provided in search list are included in the result set.
Example:
get_class_properties_with_attribute_callback(string $class, string $attribute, callable $callback) : array
Returns an array of property names from a class (not instantiated) that have the provided attribute and that attribute satisfies the provided callback.
Example:
get_class_constants_with_attribute(string|object $class_or_object, string|array $attribute_or_array_of_attributes) : array
Returns an array of constant names and their values from a class or object that have the provided attributes or array of attributes. By default, children of attributes provided in search list are included in the result set.
Example:
get_class_constants_with_attribute_callback(string|object $class_or_object, string $attribute, callable $callback, bool $matchAttributeChildren = true) : array
Returns an array of constant names and their values from a class or object that have the provided attribute and that attribute satisfies the provided callback. By default, children of attributes provided in search list are included in the result set.
Example:
Copyright (c) Chris Brim 2022, see LICENSE file