PHPackages                             virtualcomplete/selector - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. virtualcomplete/selector

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

virtualcomplete/selector
========================

Selector pattern abstract class to manage polymorphism on the user level or run-time arguments.

06PHP

Since Jan 15Pushed 9y ago2 watchersCompare

[ Source](https://github.com/virtualcomplete/selector)[ Packagist](https://packagist.org/packages/virtualcomplete/selector)[ RSS](/packages/virtualcomplete-selector/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![Build Status](https://camo.githubusercontent.com/0a336f4461142aca38d7b2fead28a6f594c01e2b6d80e1c4ad22f7adc63bf673/68747470733a2f2f7472617669732d63692e6f72672f7669727475616c636f6d706c6574652f73656c6563746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/virtualcomplete/selector)

Selector
========

[](#selector)

This is an abstract class that allows you to easily implement the provided selector pattern.

Requirements: PHP &gt;5.4.0

What is the selector pattern?
-----------------------------

[](#what-is-the-selector-pattern)

This selector pattern provides an easy way to deal with polymorphism at the user, or run-time, level. What does that mean? Well, if you code to an interface, you should already be familiar with polymorphism. It allows you to plug in different services using the same interface. Many frameworks handle the dependency injection of these services by what you bind on an application level. However, what if you have multiple services that can be applicable based on user settings? That is where this pattern will come in handy. See the payment method example below.

Usage
=====

[](#usage)

Just extend the Selector class and fill in the abstract methods as documented. getKey() should return one of the keys in your setMappings() array based on what arguments your application passes to it.

Example
=======

[](#example)

```
class PaymentMethodSelector extends Selector
{
    protected function setInterface()
    {
        return 'VirtualComplete\Selector\Example\PaymentMethodInterface';
        // Easier on PHP >= 5.5
        // return PaymentMethodInterface::class;
    }

    protected function setMappings()
    {
        return [
            'cash' => 'VirtualComplete\Selector\Example\CashPaymentMethod',
            'check' => 'VirtualComplete\Selector\Example\CheckPaymentMethod',
            'credit' => 'VirtualComplete\Selector\Example\CreditPaymentMethod'
        ];
    }

    public function getKey($arguments)
    {
        $user = $arguments[0];
        return $user['paymentMethod'];
    }

    protected function defaultKey()
    {
        return 'cash';
    }
}
```

You'd then retrieve one of the classes by passing your $user data.

```
$user['paymentMethod'] = 'credit';
$method = $selector->selectFrom($user); // Returns CreditPaymentMethod instance
```

Use dependency injection with your selector for the best results.

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/e573911db1b4b1545a6f25ff34b3e16427327fcf702c59364a6b80dd68ddf26b?d=identicon)[virtualcomplete](/maintainers/virtualcomplete)

### Embed Badge

![Health badge](/badges/virtualcomplete-selector/health.svg)

```
[![Health](https://phpackages.com/badges/virtualcomplete-selector/health.svg)](https://phpackages.com/packages/virtualcomplete-selector)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
