PHP code example of reynholm / laravel-repositories

1. Go to this page and download the library: Download reynholm/laravel-repositories 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/ */

    

reynholm / laravel-repositories example snippets



use Reynholm\LaravelRepositories\Repository\LaravelRepository;

class UserArrayRepository extends LaravelRepository
{
    //defaults to laravel's default connection
	//protected $connection = 'mysql';

	//If no tableName is specified it will be guessed based on a snake_case version of the CamelCase
	//class name without the repository part and pluralized.
	//Examples: UserRepository => users, CustomerHistoryLog => customer_history_logs, etc...
    //protected $tableName  = 'users';
}

    class YourRepository extends LaravelRepository {
        protected $fetchMode = LaravelRepositoryInterface::FETCH_AS_LARAVEL_COLLECTION_OBJECTS;
    }


     /**
      * @param int $id
      * @param array $columns Restrict columns that you want to retrieve
      * @return array
      */
     public function find($id, array $columns = array());

     /**
      * @param int $id
      * @param array $columns Restrict columns that you want to retrieve
      * @return array
      * @throws EntityNotFoundException
      */
     public function findOrFail($id, array $columns = array());

     /**
      * @param array $criteria
      * Ex.:
      * array(
      *     array('name', '=', 'carlos'),
      *     array('age',  '>', 20),
      * )
      * @param array $columns Restrict columns that you want to retrieve
      * @return array
      */
     public function findOne(array $criteria, array $columns = array());

     /**
      * @param array $criteria
      * Ex.:
      * array(
      *     array('name', '=', 'carlos'),
      *     array('age',  '>', 20),
      * )
      * @param array $columns Restrict columns that you want to retrieve
      * @param integer $limit
      * @param array $orderBy
      * Ex.:
      * array(
      *     array('name', 'asc'),
      *     array('age', 'desc'),
      * )
      * @return array
      */
     public function findMany(array $criteria, array $columns = array(), $limit = 0, array $orderBy = array());

     /**
      * @param array $columns Restrict columns that you want to retrieve
      * @param int $limit
      * @param array $orderBy
      * Ex.: ['name' => 'asc', 'age' => 'desc']
      * @return array
      */
     public function findAll(array $columns = array(), $limit = 0, array $orderBy = array());

     /**
      * Get an array with the values of a given column.
      *
      * @param  string  $column
      * @param  string  $key
      * @return array
      */
     public function lists($column, $key = null);

     /**
      * @param array $data The resource that you want to create
      * @param bool $force If force is false and data is not valid error will be throwed
      * @return boolean
      * @throws DataNotValidException
      */
     public function create(array $data, $force = false);

     /**
      * @param array $data The resources that you want to create
      * @param bool $force If force is false and data is not valid error will be throwed
      * @return boolean
      * @throws DataNotValidException
      */
     public function createMany(array $data, $force = false);

     /**
      * @param array $criteria
      * Ex.:
      * array(
      *     array('name', '=', 'carlos'),
      *     array('age',  '>', 20),
      * )
      * @return int
      */
     public function count(array $criteria = array());

    /**
     * Update a resource by its id
     * @param int $id
     * @param array $data
     * @return boolean
     */
    public function update($id, array $data);

    /**
     * Update one or more resources
     * @param array $criteria
     * @param array $data
     * @return int Number of affected rows
     */
    public function updateMany(array $criteria, array $data);

     /**
      * Validates the input array and stores all the errors,
      * them, you can get them with the getErrors() method
      * @param array $data
      * @return boolean
      */
     public function validate(array $data);

     /**
      * Validates the input array and stores all the errors,
      * them, you can get them with the getErrors() method
      * Same af validate but specify the rules, instead of using the repository rules
      * @param array $data
      * @param array $rules
      * @return boolean
      */
     public function validateWithCustomRules(array $data, array $rules);

     /**
      * Validates the input array or throws exception
      * It also stores all the errors. Then you can retrieve them with the
      * getValidationErrors() method
      * @param array $data
      * @throws DataNotValidException
      * @return void
      */
     public function validateOrFail(array $data);

     /**
      * Validates the input array or throws exception
      * It also stores all the errors. Then you can retrieve them with the
      * getValidationErrors() method
      * @param array $data
      * @param array $rules
      * @throws DataNotValidException
      * @return void
      */
     public function validateWithCustomRulesOrFail(array $data, array $rules);

     /**
      * Validates a multidimensional
      * It also stores all the errors. Then you can retrieve them with the
      * getValidationErrors() method
      * @param array $data
      * @return boolean
      */
     public function validateMany(array $data);

     /**
      * Validates a multidimensional
      * It also stores all the errors. Then you can retrieve them with the
      * getValidationErrors() method
      * Same af validate but specify the rules, instead of using the repository rules
      * @param array $data
      * @param array $rules
      * @return boolean
      */
     public function validateManyWithCustomRules(array $data, array $rules);

     /**
      * Validates a multidimensional or throws exception
      * It also stores all the errors. Then you can retrieve them with the
      * getValidationErrors() method
      * @param array $data
      * @throws DataNotValidException
      */
     public function validateManyOrFail(array $data);

     /**
      * Validates a multidimensional or throws exception
      * It also stores all the errors. Then you can retrieve them with the
      * getValidationErrors() method
      * @param array $data
      * @param array $rules
      * @throws DataNotValidException
      */
     public function validateManyWithCustomRulesOrFail(array $data, array $rules);

    /**
     * Returns the errors generated by the validate methods
     * with the keys "messages" and "failed"
     * If you used validateMany it will be a multidimensional array
     * @return array
     */
    public function getValidationErrors();

    /**
    * Return the messages key from the getValidationErrors method
    * If used after validateMany it will be a multidimensional array
    * @return array
    */
    public function getValidationMessages();

    /**
    * Return the failed key from the getValidationErrors method
    * If used after validateMany it will be a multidimensional array
    * @return array
    */
    public function getValidationFailures();

     /**
      * @param int $id
      * @return boolean
      */
     public function delete($id);

     /**
      * @param int $id
      * @throw EntityNotFoundException
      */
     public function deleteOrFail($id);

     /**
      * Delete all the rows
      * @return int Number of deleted rows
      */
     public function deleteAll();


class DownloadRepository extends LaravelRepository
{
    protected $timestamps = true;
}

protected $stamp_create = 'created_at';
protected $stamp_update = 'updated_at';

class UserArrayRepository extends LaravelRepository {

    protected $connection = 'default';
    protected $tableName  = 'users';
    protected $rules      = array(
        'name' => '
 
if ($this->arrayRepository->validate($validData)) {
    //The data is valid
}
else {
    //The data is not valid
    //You can get the validation failed or messages this way:
    $errors = $this->arrayRepository->getValidationErrors()
    //$errors['messages'] Contains an array with the validation messages
    //$errors['failed'] Contains an array with the failed rules
}

$this->getBuilder()

class MyUserRepository extends LaravelRepository
{
    public function getActiveUsers()
    {
        $result = $this->getBuilder()->whereActive(true)->get();

        //Builder returns an objects array so you can use the following method
        //to convert an array of objets to array
        return $this->objectsToArray($result);
    }
}

interface MyUserRepositoryInterface
{
    /**
    * @return array
    */
    public function getActiveUsers();
}

class MyUserRepository extends LaravelRepository implements MyUserRepositoryInterface
{
    /**
    * {@inheritdoc}
    */
    public function getActiveUsers()
    {
        $result = $this->getBuilder()->whereActive(true)->get();

        //In order to return the data with the current fetcher use the getFetcher() method like this:
        return $this->getFetcher()->fetchMany($result);

        //If you are returning only one entity use fetch method instead
        return $this->getFetcher()->fetch();
    }
}