2017 © Pedro Peláez
 

library repository-manager

Manage your entities and create simple repositories using this abstract vendor

image

internetpixels/repository-manager

Manage your entities and create simple repositories using this abstract vendor

  • Wednesday, May 2, 2018
  • by Petervw
  • Repository
  • 1 Watchers
  • 1 Stars
  • 91 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 469 % Grown

The README.md

Repository manager with simple SQL Query builder

Manage your entities and repositories with this simple repository manager. An all in one solution for building repositories and entities., (*1)

License Build Status Maintainability, (*2)

Basic examples

Create your first entity

The entity will contain the data for a specific object (Also known as DAO) and will mediate between the application and the database. An entity has to use getters and setters., (*3)

<?php

namespace YourProject\People;

use InternetPixels\RepositoryManager\Entities\AbstractEntity;
use InternetPixels\RepositoryManager\Entities\EntityInterface;

class PersonEntity extends AbstractEntity implements EntityInterface
{

    /**
     * @var string
     */
    private $name;

    /**
     * @var int
     */
    private $age;

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName(string $name)
    {
        $this->name = $name;
    }

    /**
     * @return int
     */
    public function getAge(): int
    {
        return $this->age;
    }

    /**
     * @param int $age
     */
    public function setAge(int $age)
    {
        $this->age = $age;
    }
}

Create your first repository

A repository will handle all data transfers between an entity and your database. The repository will build queries to execute those actions., (*4)

Note: The entityName needs to map to your database table name., (*5)

<?php

namespace YourProject\People;

use InternetPixels\RepositoryManager\Entities\AbstractEntity;
use InternetPixels\RepositoryManager\Factories\EntityFactory;
use InternetPixels\RepositoryManager\Repositories\AbstractRepository;

class PeopleRepository extends AbstractRepository
{

    protected $entityName = 'people';

    public function update(AbstractEntity $entity)
    {
        $query = $this->queryBuilder->new($this->entityName)
            ->update([
                'name' => $this->dataManager->sanitize($entity->getName()),
                'age' => $this->dataManager->sanitize($entity->getAge()),
            ])
            ->where(['id' => $entity->getId()])
            ->limit(1)
            ->get();

        return $this->executeQuery($query);
    }

    /**
     * @param array $data
     * @return PersonEntity
     */
    protected function dataToEntity(array $data): PersonEntity
    {    
        /** @var PersonEntity $entity */
        $entity = EntityFactory::create('people');

        $entity->setName($data['name']);
        $entity->setAge($data['age']);

        return $entity;
    }
}

Register the Data manager

You only need to register the RepositoryDataManager and the new entity in the EntityFactory. The Data manager needs an (existing) Mysqli connection., (*6)

$mysqliConnection = new \Mysqli(
    $config['mysql.host'],
    $config['mysql.user'],
    $config['mysql.password'],
    $config['mysql.database']
);

$repositoryDataManager = new \InternetPixels\RepositoryManager\Managers\RepositoryDataManager($mysqliConnection);

// Add all your entities:
\InternetPixels\RepositoryManager\Factories\EntityFactory::register('people', new PersonEntity());

$peopleRepository = new PeopleRepository($repositoryDataManager);

Usage of the repository

In a service of your application you can implement the PeopleRepository and use them for basic CRUD actions or your custom implementations., (*7)

$peopleRepository = new PeopleRepository($repositoryDataManager);

// Get all records:
$people = $peopleRepository->read();

// Update a person
$person = EntityFactory::create('people');
$person->setId(1);
$person->setName('Person name');
$person->setAge(26); // update the age

$peopleRepository->update($person);

// Delete a person
$person = EntityFactory::create('people');
$person->setId(1);

$peopleRepository->delete($person);

Usage of the SQL Query builder

This package contains a simple query builder. Please use the functionality to prevent basic SQL issues., (*8)

// Sanitize input in a repository before pushing to the database:
$safe = $this->dataManager->sanitize($entity->getName());

// Build a new select query in a repository
$query = $this->queryBuilder->new($this->entityName)
            ->select()
            ->get();

// Build a new select query in a repository with a limit
$query = $this->queryBuilder->new($this->entityName)
            ->select()
            ->limit(2)
            ->get();

// Build a new select query in a repository with a where clause
$query = $this->queryBuilder->new($this->entityName)
            ->select()
            ->where(['age' => 25])
            ->get();

The Versions

02/05 2018

dev-master

9999999-dev

Manage your entities and create simple repositories using this abstract vendor

  Sources   Download

The Requires

  • php >=7.1

 

The Development Requires

by Peter van Wilderen

02/05 2018

0.1.1

0.1.1.0

Manage your entities and create simple repositories using this abstract vendor

  Sources   Download

The Requires

  • php >=7.1

 

The Development Requires

by Peter van Wilderen

30/04 2018

0.1.0

0.1.0.0

Manage your entities and create simple repositories using this abstract vendor

  Sources   Download

The Requires

  • php >=7.1

 

The Development Requires

by Peter van Wilderen