Basic Usage

Create an In-Memory Repository

Pass the rows directly, keyed by their repository identifiers. String keys, numeric lists and sparse numeric keys are supported.

Each row is an array whose field names are strings. Numeric repository identifiers are valid; numeric field names inside a row raise TypeMismatchException when the repository loads its source. Field values, including nested arrays, are preserved without conversion. This applies to every entity class, including stdClass.

use Derafu\Repository\Entity;
use Derafu\Repository\Repository;

$rows = [
    'prod-001' => [
        'name' => 'Laptop XPS',
        'category' => 'computers',
        'price' => 1299.99,
    ],
    'prod-002' => [
        'name' => 'Keyboard',
        'category' => 'accessories',
        'price' => 59.90,
    ],
];

$repository = new Repository($rows, Entity::class, idAttribute: 'id');
$product = $repository->find('prod-001');
$name = $product?->getAttribute('name'); // 'Laptop XPS'.
$all = $repository->findAll();
$computers = $repository->findBy(['category' => 'computers']);
$laptop = $repository->findOneBy(['name' => 'Laptop XPS']);
$total = $repository->count(); // 2.

idAttribute fills a missing field from the row key. An existing field value is preserved. find() uses the row key, even if that field contains a different value.

The direct constructor accepts an array, supported array-access objects or a PHP file path returning rows. Each row must be an array. When no entity class is supplied, the direct Repository constructor returns stdClass objects.

Use the Manager

The manager delegates construction to an injected factory. A plain identifier such as products uses the default factory’s entity class, Derafu\Repository\Entity. With Symfony, import the complete service configuration. Without a container, assemble the dependencies explicitly:

use Derafu\Repository\Factory\DataProviderRepositoryFactory;
use Derafu\Repository\Service\ArrayRepositorySourceProvider;
use Derafu\Repository\Service\CriteriaEvaluator;
use Derafu\Repository\Service\DataProvider;
use Derafu\Repository\Service\DataSource\DataSourceResolverRegistry;
use Derafu\Repository\Service\DataSource\EnumDataSourceResolver;
use Derafu\Repository\Service\DataSource\FileDataSourceResolver;
use Derafu\Repository\Service\DataSource\FileFormat\FileFormatReaderRegistry;
use Derafu\Repository\Service\DataSource\FileFormat\JsonFileFormatReader;
use Derafu\Repository\Service\DataSource\FileFormat\PhpFileFormatReader;
use Derafu\Repository\Service\DataSource\FileFormat\YamlFileFormatReader;
use Derafu\Repository\Service\RepositoryManager;

$readers = new FileFormatReaderRegistry([
    new PhpFileFormatReader(),
    new JsonFileFormatReader(),
    new YamlFileFormatReader(),
]);
$resolvers = new DataSourceResolverRegistry([
    new EnumDataSourceResolver(),
    new FileDataSourceResolver($readers),
]);
$provider = new DataProvider(
    sourceProviders: [new ArrayRepositorySourceProvider([
        'products' => '/path/to/products.json',
    ])],
    sourceResolvers: $resolvers,
);
$factory = new DataProviderRepositoryFactory($provider, new CriteriaEvaluator());
$manager = new RepositoryManager(defaultFactory: $factory, dataProvider: $provider);

$available = $manager->getAvailableRepositories(); // ['products'].
$products = $manager->getRepository('products');
$product = $products->find('prod-001');

The file contains the rows themselves, without a surrounding products property. Its extension selects the reader. See data sources for supported formats.

Listing available repositories does not read their data or construct them. A successful getRepository() call is cached by identifier: subsequent calls return the same repository instance.

Typed Repositories

RepositoryInterface and Repository are generic over their entity (T, any object). getRepository() types the repository it returns by its argument:

$products = $manager->getRepository(Product::class); // RepositoryInterface<Product>.
$product = $products->find('prod-001');              // Product|null.
$computers = $products->findBy(['category' => 'computers']); // Product[].

$statuses = $manager->getRepository('statuses');     // RepositoryInterface<object>.

With an entity class or an entity interface, the query methods return instances of it. With any other identifier the repository is typed with object.

The types are PHPDoc for static analysis tools: they do not change what a repository returns at run time. The Repository constructor takes its entity class as a class-string<T>.

Query Contract and Raw Rows

RepositoryInterface exposes entity queries. It does not require database persistence or a particular constructor. It extends Doctrine Collections’ Selectable, so matching() accepts a Criteria.

The concrete Repository class also provides all(), which returns the stored rows keyed by identifier, whereas findAll() returns entity objects. The rows cannot be modified through the repository.

Depend explicitly on the concrete class when you need all(). A repository obtained from a custom factory is only required to implement the query contract.

On this page

Last updated on 09/11/2026 by Anonymous