1. Go to this page and download the library: Download brimshot/pattr library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
brimshot / pattr example snippets
use function brimshot\Pattr\has_attribute;
namespace brimshot\Pattr\examples;
use function brimshot\Pattr\has_attribute;
// Define some attributes
#[\Attribute]
class HasALightSaber
{
public function __construct(public string $color) {}
}
#[\Attribute]
class IsFromTatooine {}
// Define some classes
#[HasALightSaber(color: 'blue')]
class ObiWan {}
#[HasALightSaber(color: 'green')]
#[IsFromTatooine]
class Luke {}
// Check attributes
has_attribute(ObiWan::class, HasALightSaber::class); // returns true
has_attribute(Luke::class, 'HasALightSaber'); // returns true
has_attribute(ObiWan::class, [HasALightSaber::class, IsFromTatooine::class]); // returns false
has_attribute(Luke::class, [HasALightSaber::class, IsFromTatooine::class]); // returns true
use function brimshot\Pattr\does_not_have_attribute;
does_not_have_attribute(ObiWan::class, IsFromTatooine::class); // returns true
does_not_have_attribute(ObiWan::class, [IsFromTatooine::class, HasALightSaber::class]); // returns false
use function brimshot\Pattr\get_attribute_names;
/*
Array
(
[0] => Pattr\examples\HasALightSaber
[1] => Pattr\examples\IsFromTatooine
)
*/
print_r(get_attribute_names(Luke::class));
/*
Array
(
[0] => HasALightSaber
[1] => IsFromTatooine
)
*/
print_r(get_attribute_names(Luke::class, true)); // Pass true as the second argument to return short names
use function brimshot\Pattr\get_attribute;
$a = get_attribute(Luke::class, HasALightSaber::class);
// Pattr\examples\HasALightSaber
echo get_class($a) . "\n";
// "green"
echo $a->color . "\n";
namespace brimshot\Pattr\examples;
use function brimshot\Pattr\get_attributes;
#[HasALightSaber(color: 'green')]
#[IsFromTatooine]
class Luke
{
#[UsesTheForce]
#[JediPower]
public function useJediMindTrick() {}
}
/*
Array
(
[0] => brimshot\Pattr\examples\UsesTheForce Object
(
)
[1] => brimshot\Pattr\examples\JediPower Object
(
)
)
*/
print_r(get_attributes([Luke::class, 'useJediMindTrick'])); // Get the attributes of a class method
/*
Array
(
[0] => Pattr\examples\HasALightSaber Object
(
[color] => green
)
)
*/
print_r(get_attributes(Luke::class, [HasALightSaber::class])); // Get the attributes of a class, filtered to only return the desired results
print_r(get_attributes(Luke::class, ['HasALightSaber'])); // Same result
use function brimshot\Pattr\get_class_methods_with_attribute;
#[\Attribute]
class UsesTheForce {}
class Luke
{
function huntWompRats() {}
#[UsesTheForce]
function retrieveLightSaber() {}
#[UsesTheForce]
function tryToLiftXWing() {}
}
/**
Array
(
[0] => retrieveLightSaber
[1] => tryToLiftXWing
)
*/
print_r(get_class_methods_with_attribute(Luke::class, UsesTheForce::class));
use function brimshot\Pattr\get_class_properties_with_attribute_callback;
#[\Attribute]
class DoesNotUseTheForce
{
public function __construct(public string $location) {}
}
class Luke
{
#[DoesNotUseTheForce('anywhere')]
function huntWompRats() {}
#[DoesNotUseTheForce('Tatooine')]
function pickUpPowerConverters() {}
}
/**
Array
(
[0] => pickUpPowerConverters
)
*/
print_r(get_class_methods_with_attribute_callback(Luke::class, DoesNotUseTheForce::class, fn($a) => $a->location == 'Tatooine'));
use function brimshot\Pattr\get_object_properties_with_attribute;
#[\Attribute]
class Weapon {}
class XWing
{
#[Weapon]
public $laserCannons = 4;
#[Weapon]
public $protonTorpedoLaunchers = 2;
}
$ship = new XWing();
/*
Array
(
[laserCannons] => 4
[protonTorpedoLaunchers] => 2
)
*/
print_r(get_object_properties_with_attribute($ship, Weapon::class));
use function brimshot\Pattr\get_object_properties_with_attribute_callback;
#[\Attribute]
class PassengerData
{
public function __construct(public string $type) {}
}
class MilleniumFalcon
{
#[PassengerData('public')]
public $mainRoomCapacity = 30;
#[PassengerData('secret')]
public $floorCompartmentCapacity = 2;
}
$ship = new MilleniumFalcon();
/*
Array
(
[mainRoomCapacity] => 30
)
*/
print_r(get_object_properties_with_attribute_callback($ship, PassengerData::class, fn($a) => $a->type == 'public'));
use function brimshot\Pattr\get_class_properties_with_attribute;
/*
(
[0] => laserCannons
[1] => protonTorpedoLaunchers
)
*/
print_r(get_class_properties_with_attribute(XWing::class, Weapon::class));
use function brimshot\Pattr\get_class_properties_with_attribute_callback;
/*
Array
(
[0] => mainRoomCapacity
)
*/
print_r(get_class_properties_with_attribute_callback(MilleniumFalcon::class, PassengerData::class, fn($a) => $a->type == 'public'));
use function brimshot\Pattr\get_class_constants_with_attribute;
#[\Attribute]
class CrewData
{
public function __construct(public string $type) {}
}
class XWing
{
#[CrewData('human')]
const PILOTS = 1;
#[CrewData('droid')]
const R2_UNITS = 1;
}
/*
Array
(
[PILOTS] => 1
[R2_UNITS] => 1
)
*/
print_r(get_class_constants_with_attribute(XWing::class, CrewData::class));
use function brimshot\Pattr\get_object_properties_with_attribute_callback;
/*
Array
(
[R2_UNITS] => 1
)
*/
print_r(get_class_constants_with_attribute_callback(XWing::class, CrewData::class, fn($a) => $a->type == 'droid'));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.