PHPackages                             tobento/service-user - 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. tobento/service-user

ActiveLibrary

tobento/service-user
====================

A user interface for PHP applications.

2.0.2(3mo ago)0123MITPHPPHP &gt;=8.4

Since Apr 29Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/tobento-ch/service-user)[ Packagist](https://packagist.org/packages/tobento/service-user)[ Docs](https://www.tobento.ch)[ RSS](/packages/tobento-service-user/feed)WikiDiscussions 2.x Synced today

READMEChangelog (7)Dependencies (4)Versions (9)Used By (0)

User Service
============

[](#user-service)

A user and address interface for PHP applications.

Table of Contents
-----------------

[](#table-of-contents)

- [Getting started](#getting-started)
    - [Requirements](#requirements)
    - [Highlights](#highlights)
- [Documentation](#documentation)
    - [User](#user)
        - [Create User](#create-user)
        - [User Factory](#user-factory)
        - [User Interface](#user-interface)
    - [Address](#address)
        - [Create Address](#create-address)
        - [Address Factory](#address-factory)
        - [Address Interface](#address-interface)
    - [Addresses](#addresses)
        - [Create Addresses](#create-addresses)
        - [Addresses Factory](#addresses-factory)
        - [Addresses Interface](#addresses-interface)
    - [Addressable](#addressable)
    - [Address Aware Interface](#address-aware-interface)
    - [User Aware Interface](#user-aware-interface)
- [Credits](#credits)

---

Getting started
===============

[](#getting-started)

Add the latest version of the user service project running this command.

```
composer require tobento/service-user

```

Requirements
------------

[](#requirements)

- PHP 8.4 or greater

Highlights
----------

[](#highlights)

- Framework-agnostic, will work with any project
- Decoupled design

Documentation
=============

[](#documentation)

User
----

[](#user)

### Create User

[](#create-user)

```
use Tobento\Service\User\User;
use Tobento\Service\User\UserInterface;
use Tobento\Service\User\Addressable;

$user = new User(username: 'username');

var_dump($user instanceof UserInterface);
// bool(true)

var_dump($user instanceof Addressable);
// bool(true)
```

Check out the [User Interface](#user-interface) to learn more about the interface.

Check out [Addressable](#addressable) to learn more about the interface.

### User Factory

[](#user-factory)

Easily create a user with the provided user factory:

**createUser**

```
use Tobento\Service\User\UserFactory;
use Tobento\Service\User\UserFactoryInterface;
use Tobento\Service\User\UserInterface;
use Tobento\Service\User\AddressesFactoryInterface;

$userFactory = new UserFactory(
    addressesFactory: null, // null|AddressesFactoryInterface
);

var_dump($userFactory instanceof UserFactoryInterface);
// bool(true)

$user = $userFactory->createUser(username: 'username');

var_dump($user instanceof UserInterface);
// bool(true)
```

**Parameters:**

```
use Tobento\Service\User\UserFactory;
use Tobento\Service\User\AddressesInterface;

$userFactory = new UserFactory();

$user = $userFactory->createUser(
    id: 1,
    number: 'user1',
    active: true,
    type: 'private',
    password: '',
    username: '',
    email: 'user@example.com',
    smartphone: '',
    locale: 'de-CH',
    birthday: '',
    dateCreated: '',
    dateUpdated: '',
    dateLastVisited: '',
    image: [],
    newsletter: false,
    meta: [],
    addresses: null, // null|AddressesInterface
);
```

**createUserFromArray**

```
use Tobento\Service\User\UserFactory;

$userFactory = new UserFactory();

$user = $userFactory->createUserFromArray([
    'username' => 'username',
]);
```

If you want to create a user with addresses, you need to set an AddressesFactory, otherwise addresses gets not created.

```
use Tobento\Service\User\UserFactory;
use Tobento\Service\User\AddressesFactory;

$userFactory = new UserFactory(new AddressesFactory());

$user = $userFactory->createUserFromArray([
    'username' => 'username',
    'addresses' => [
        ['key' => 'payment'],
        ['key' => 'shipping'],
    ],
]);
```

### User Interface

[](#user-interface)

```
use Tobento\Service\User\UserFactory;
use Tobento\Service\User\UserInterface;

$userFactory = new UserFactory();

$user = $userFactory->createUser(username: 'username');

var_dump($user instanceof UserInterface);
// bool(true)

var_dump($user->id());
// int(0)

var_dump($user->number());
// string(0) ""

var_dump($user->active());
// bool(true)

var_dump($user->type());
// string(0) ""

var_dump($user->password());
// string(0) ""

var_dump($user->username());
// string(8) "username"

var_dump($user->email());
// string(0) ""

var_dump($user->smartphone());
// string(0) ""

var_dump($user->locale());
// string(0) ""

var_dump($user->birthday());
// string(0) ""

var_dump($user->dateCreated());
// string(0) ""

var_dump($user->dateUpdated());
// string(0) ""

var_dump($user->dateLastVisited());
// string(0) ""

var_dump($user->image());
// array(0) { }

var_dump($user->newsletter());
// bool(false)

var_dump($user->getMeta());
// array(0) { }

var_dump($user->meta(key: 'key', default: 'default'));
// NULL

var_dump($user->greetingSalutation());
// string(5) "greet"

var_dump($user->greeting());
// string(8) "username"
```

Address
-------

[](#address)

### Create Address

[](#create-address)

```
use Tobento\Service\User\Address;
use Tobento\Service\User\AddressInterface;

$address = new Address(key: 'shipping');

var_dump($address instanceof AddressInterface);
// bool(true)
```

### Address Factory

[](#address-factory)

Easily create an address with the provided address factory:

**createAddress**

```
use Tobento\Service\User\AddressFactory;
use Tobento\Service\User\AddressFactoryInterface;
use Tobento\Service\User\AddressInterface;

$addressFactory = new AddressFactory();

var_dump($addressFactory instanceof AddressFactoryInterface);
// bool(true)

$address = $addressFactory->createAddress(key: 'shipping');

var_dump($address instanceof AddressInterface);
// bool(true)
```

**Parameters:**

```
use Tobento\Service\User\AddressFactory;

$addressFactory = new AddressFactory();

$address = $addressFactory->createAddress(
    key: 'payment',
    id: 0,
    userId: 0,
    group: '',
    salutation: 'mr',
    name: '',
    firstname: 'Adam',
    lastname: 'Smith',
    company: '',
    address1: 'Musterstrasse',
    address2: '',
    address3: '',
    postcode: '34',
    city: 'Bern',
    state: '',
    countryKey: 'CH',
    country: 'Schweiz',
    email: '',
    telephone: '',
    smartphone: '',
    fax: '',
    website: '',
    locale: 'de',
    birthday: '',
    notice: '',
    info: '',
    selectable: false,
    meta: [],
);
```

**createAddressFromArray**

```
use Tobento\Service\User\AddressFactory;

$addressFactory = new AddressFactory();

$address = $addressFactory->createAddressFromArray([
    'key' => 'payment',
]);
```

### Address Interface

[](#address-interface)

**getters**

```
use Tobento\Service\User\AddressFactory;
use Tobento\Service\User\AddressInterface;

$addressFactory = new AddressFactory();

$address = $addressFactory->createAddress(key: 'shipping');

var_dump($address instanceof AddressInterface);
// bool(true)

var_dump($address->key());
// string(8) "shipping"

var_dump($address->isPrimary());
// bool(false)

var_dump($address->id());
// int(0)

var_dump($address->userId());
// int(0)

var_dump($address->group());
// string(0) ""

var_dump($address->salutation());
// string(0) ""

var_dump($address->name());
// string(0) ""

var_dump($address->firstname());
// string(0) ""

var_dump($address->lastname());
// string(0) ""

var_dump($address->hasFullname());
// bool(false)

var_dump($address->fullname());
// string(0) ""

var_dump($address->company());
// string(0) ""

var_dump($address->address1());
// string(0) ""

var_dump($address->address2());
// string(0) ""

var_dump($address->address3());
// string(0) ""

var_dump($address->postcode());
// string(0) ""

var_dump($address->city());
// string(0) ""

var_dump($address->state());
// string(0) ""

var_dump($address->hasPostcodeCity());
// bool(false)

var_dump($address->postcodeCity());
// string(0) ""

var_dump($address->countryKey());
// string(0) ""

var_dump($address->country());
// string(0) ""

var_dump($address->hasContact());
// bool(false)

var_dump($address->email());
// string(0) ""

var_dump($address->telephone());
// string(0) ""

var_dump($address->smartphone());
// string(0) ""

var_dump($address->fax());
// string(0) ""

var_dump($address->website());
// string(0) ""

var_dump($address->locale());
// string(0) ""

var_dump($address->birthday());
// string(0) ""

var_dump($address->notice());
// string(0) ""

var_dump($address->info());
// string(0) ""

var_dump($address->selectable());
// bool(false)

var_dump($address->getMeta());
// array(0) { }

var_dump($address->meta(key: 'key', default: 'default'));
// NULL

var_dump($address->greetingSalutation());
// string(5) "greet"

var_dump($address->greeting());
// string(0) ""
```

**with methods**

The with methods will return a new instance.

```
use Tobento\Service\User\AddressFactory;

$addressFactory = new AddressFactory();

$address = $addressFactory->createAddress(key: 'shipping');

$address = $address->withGroup('addressbook');

$address = $address->withSalutation('mr');

$address = $address->withName('Name');

$address = $address->withFirstname('John');

$address = $address->withLastname('Smith');

$address = $address->withCompany('Name of Company');

$address = $address->withAddress1('Address Line 1');

$address = $address->withAddress2('Address Line 2');

$address = $address->withAddress3('Address Line 3');

$address = $address->withPostcode('3000');

$address = $address->withCity('Bern');

$address = $address->withState('BE');

$address = $address->withCountryKey('CH');

$address = $address->withCountry('Schweiz');

$address = $address->withEmail('user@example.com');

$address = $address->withTelephone('');

$address = $address->withSmartphone('');

$address = $address->withFax('');

$address = $address->withWebsite('example.com');

$address = $address->withLocale('de-CH');

$address = $address->withBirthday('');

$address = $address->withNotice('Some message');

$address = $address->withInfo('Some message');

$address = $address->withSelectable(false);

$address = $address->withMeta(['key' => 'value']);

$address = $address->withGreeting(
    greeting: 'John Smith',
    salutation: 'mr'
);
```

Addresses
---------

[](#addresses)

### Create Addresses

[](#create-addresses)

```
use Tobento\Service\User\Addresses;
use Tobento\Service\User\AddressesInterface;
use Tobento\Service\User\AddressFactoryInterface;
use Tobento\Service\User\AddressInterface;
use Tobento\Service\User\Address;

$addresses = new Addresses(
    null, // addressFactory: null|AddressFactoryInterface
    // addresses: ...AddressInterface
    new Address(key: 'shipping'),
    new Address(key: 'payment'),
);

var_dump($addresses instanceof AddressesInterface);
// bool(true)
```

Check out [Addresses Interface](#addresses-interface) to learn more about the interface.

### Addresses Factory

[](#addresses-factory)

Easily create an addresses object with the provided addresses factory:

**createAddresses**

```
use Tobento\Service\User\AddressesFactory;
use Tobento\Service\User\AddressesFactoryInterface;
use Tobento\Service\User\AddressesInterface;
use Tobento\Service\User\AddressFactoryInterface;
use Tobento\Service\User\AddressInterface;
use Tobento\Service\User\Address;

$addressesFactory = new AddressesFactory(
    addressFactory: null, // null|AddressFactoryInterface
);

var_dump($addressesFactory instanceof AddressesFactoryInterface);
// bool(true)

$addresses = $addressesFactory->createAddresses(
    // addresses: ...AddressInterface
    new Address(key: 'shipping'),
);

var_dump($addresses instanceof AddressesInterface);
// bool(true)
```

**createAddressesFromArray**

```
use Tobento\Service\User\AddressesFactory;
use Tobento\Service\User\Address;

$addresses = new AddressesFactory()->createAddressesFromArray([
    ['key' => 'payment'],
    new Address(key: 'shipping'),
]);
```

### Addresses Interface

[](#addresses-interface)

```
use Tobento\Service\User\AddressesFactory;
use Tobento\Service\User\AddressesInterface;

$addresses = new AddressesFactory()->createAddresses();

var_dump($addresses instanceof AddressesInterface);
// bool(true)
```

**addressFactory**

```
use Tobento\Service\User\AddressesFactory;
use Tobento\Service\User\AddressFactoryInterface;

$addresses = new AddressesFactory()->createAddresses();

var_dump($addresses->addressFactory() instanceof AddressFactoryInterface);
// bool(true)
```

**get**

The get method returns the address by the specified key. If the address does not exist yet, it will create and add it to the addresses.

```
use Tobento\Service\User\AddressesFactory;
use Tobento\Service\User\AddressInterface;

$addresses = new AddressesFactory()->createAddresses();

$address = $addresses->get(key: 'payment');

var_dump($address instanceof AddressInterface);
// bool(true)
```

**add**

```
use Tobento\Service\User\AddressesFactory;

$addresses = new AddressesFactory()->createAddresses();

$address = $addresses->addressFactory()->createAddress(key: 'payment');

$addresses->add($address);
```

**create**

The create method creates an address with the specified parameters, but does it not add to the addresses.

```
use Tobento\Service\User\AddressesFactory;
use Tobento\Service\User\AddressInterface;

$addresses = new AddressesFactory()->createAddresses();

$address = $addresses->create(['key' => 'payment']);

var_dump($address instanceof AddressInterface);
// bool(true)

// you might add it to the addresses
$addresses->add($address);
```

**address**

The address method creates an address and adds it to the addresses.

```
use Tobento\Service\User\AddressesFactory;
use Tobento\Service\User\AddressInterface;

$addresses = new AddressesFactory()->createAddresses();

$address = $addresses->address(['key' => 'payment']);

var_dump($address instanceof AddressInterface);
// bool(true)
```

**has**

Check if an address exists.

```
use Tobento\Service\User\AddressesFactory;

$addresses = new AddressesFactory()->createAddresses();

var_dump($addresses->has(key: 'payment'));
// bool(false)

$addresses->address(['key' => 'payment', 'firstname' => 'John']);

var_dump($addresses->has(key: 'payment'));
// bool(true)

// you might check for each address parameter
var_dump($addresses->has(
    key: 'payment',
    with: ['firstname', 'lastname'],
));
// bool(false)

var_dump($addresses->has(
    key: 'payment',
    with: ['firstname'],
));
// bool(true)
```

**delete**

```
use Tobento\Service\User\AddressesFactory;

$addresses = new AddressesFactory()->createAddresses();

$addresses->delete(key: 'payment');
```

**all**

```
use Tobento\Service\User\AddressesFactory;
use Tobento\Service\User\AddressInterface;

$addresses = new AddressesFactory()->createAddresses();

$addresses->address(['key' => 'payment']);

foreach($addresses->all() as $address) {
    var_dump($address instanceof AddressInterface);
    // bool(true)
}

// or just
foreach($addresses as $address) {
    var_dump($address instanceof AddressInterface);
    // bool(true)
}
```

**filter**

You might filter addresses returning a new instance.

```
use Tobento\Service\User\AddressesFactory;
use Tobento\Service\User\AddressInterface;

$addresses = new AddressesFactory()->createAddresses();

$addresses = $addresses->filter(
    fn(AddressInterface $a): bool => $a->countryKey() === 'CH'
);
```

**group**

The group method filters addresses by the specified group.

```
use Tobento\Service\User\AddressesFactory;

$addresses = new AddressesFactory()->createAddresses();

$addresses = $addresses->group('addressbook');
```

Addressable
-----------

[](#addressable)

```
use Tobento\Service\User\User;
use Tobento\Service\User\Addressable;

$user = new User(username: 'username');

var_dump($user instanceof Addressable);
// bool(true)
```

**addresses**

```
use Tobento\Service\User\User;
use Tobento\Service\User\AddressesInterface;

$user = new User(username: 'username');

var_dump($user->addresses() instanceof AddressesInterface);
// bool(true)
```

Check out the [Addresses Interface](#addresses-interface) to learn more about the interface.

**address**

The address method returns the address if exists or creates a new address for the specified key.

```
use Tobento\Service\User\User;
use Tobento\Service\User\AddressInterface;

$user = new User(username: 'username');

// returns primary address
var_dump($user->address() instanceof AddressInterface);
// bool(true)

var_dump($user->address(key: 'payment') instanceof AddressInterface);
// bool(true)
```

**hasAddress**

```
use Tobento\Service\User\User;

$user = new User(username: 'username');

var_dump($user->hasAddress(key: 'payment'));
// bool(false)

// you might check for each address parameter
var_dump($user->hasAddress(
    key: 'payment',
    with: ['firstname', 'lastname'],
));
// bool(false)
```

Address Aware Interface
-----------------------

[](#address-aware-interface)

```
use Tobento\Service\User\AddressAwareInterface;
use Tobento\Service\User\AddressInterface;

interface AddressAwareInterface
{
    /**
     * Create an address that represents the object.
     *
     * @return AddressInterface
     */
    public function toAddress(): AddressInterface;
}
```

The user has implemented the interface returning the `primary` address:

```
use Tobento\Service\User\AddressAwareInterface;
use Tobento\Service\User\User;

$user = new User(username: 'username');

var_dump($user instanceof AddressAwareInterface);
// bool(true)

var_dump($user->toAddress()->key());
// string(7) "primary"
```

User Aware Interface
--------------------

[](#user-aware-interface)

```
use Tobento\Service\User\UserAwareInterface;
use Tobento\Service\User\UserInterface;

interface UserAwareInterface
{
    /**
     * Create an user that represents the object.
     *
     * @return UserInterface
     */
    public function toUser(): UserInterface;
}
```

Credits
=======

[](#credits)

- [Tobias Strub](https://www.tobento.ch)
- [All Contributors](../../contributors)

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance79

Regular maintenance activity

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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.

###  Release Activity

Cadence

Every ~164 days

Recently: every ~22 days

Total

9

Last Release

110d ago

Major Versions

1.x-dev → 2.02025-09-30

PHP version history (2 changes)1.0.0PHP &gt;=8.0

2.0PHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/055d6a1b5c2384bb179c75ab0b55914231d898fdc4dffeb30770f81200e52206?d=identicon)[TOBENTOch](/maintainers/TOBENTOch)

---

Top Contributors

[![tobento-ch](https://avatars.githubusercontent.com/u/16684832?v=4)](https://github.com/tobento-ch "tobento-ch (17 commits)")

---

Tags

phpaddresspackageusertobento

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tobento-service-user/health.svg)

```
[![Health](https://phpackages.com/badges/tobento-service-user/health.svg)](https://phpackages.com/packages/tobento-service-user)
```

PHPackages © 2026

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