2017 © Pedro Peláez
 

library laravel-registers

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

image

tmd/laravel-registers

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  • Tuesday, November 14, 2017
  • by antriver
  • Repository
  • 1 Watchers
  • 0 Stars
  • 46 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 23 Versions
  • 0 % Grown

The README.md

Laravel Registers

This package provides 'registers', which are simple lists of models 'belonging to' another model. For example, a list of Users who have liked a Post. Or a list of Achievements a User has earnt. The registers also take care of caching what items are on the list., (*1)

Terminology

The owner of a register is the model the list belongs to. In the case of a 'post likers' register, this would be the Post model., (*2)

The object is what you add to the register / check is on the register / delete from the register. In the case of a 'post likers' register, this would be the User model., (*3)

Installation

composer require antriver/laravel-registers

Integration

Create a class that extends either AbstractBooleanRegister or AbstractValueRegister., (*4)

AbstractBooleanRegister

This type of register serves as a simple 'is it here or not' list. Example use: Users who have liked a Post., (*5)

AbstractValueRegister

This type of register allows the same setting/checking as AbstractBooleanRegister but you can also set some data. Examples use: Users who have voted on a Post. The additional data would be the user's vote., (*6)

The only difference between the two is AbstractBooleanRegister's check() method will return a boolean. But AbstractValueRegister will return whatever data you stored about the entry., (*7)

In both cases you must implement these methods:, (*8)

create($object, array $data = [])

This should add an entry to the database to permanently store the action that has been performed (e.g. INSERT an entry in the post_likes or post_votes table). This should return an integer to show the result. * 1 = Object was added to the register. * 2 = Object was already on the register and so was modified. * 0 = Object was already on the register but the value was the same, so was not modified., (*9)

The easiest way to get these return values is to use a unique key on the 2 IDs on your database table (e.g. a unique key on postId + userId for a post likers register.) Then the query you run in create() should use some form of ON DUPLICATE KEY UPDATE and return the number of rows affected., (*10)

Hints:, (*11)

The post will be accessible as $this-owner, the user will be the passed in $object., (*12)

destroy($object)

This should delete the row in the database relating to the action (e.g. DELETE the entry from the post_likes table) and return the number of rows affected. If this returns 0 an exception will be thrown, as that meant the object was not on the register., (*13)

load()

This should return all of the items on the register (e.g. SELECT all the entries about this post from the post_likes table). This method should return an associative array where the keys are the IDs of the objects and the value is information about that object's entry., (*14)

For AbstractValueRegister this is straightforward - return whatever value you stored about the entry (e.g. which way the user voted, in the case of a post voters register.), (*15)

For AbstractBooleanRegister this may make less sense, but you can return anything as the value. It's recommended to just use something like true. The reason the array is this way round is that it can be much faster to use isset() (cheking against the key) than in_array() (checking against the value) on larger arrays (see http://maettig.com/1397246220)., (*16)

You can use the buildObjectsArrayFromLoadedData() helper method to provide the return value., (*17)

Examples

See ExamplePostLikesRegister.php and ExamplePostVotesRegister.php for example implementations., (*18)

Usage

Both AbstractBooleanRegister and AbstractValueRegister provide the same public methods., (*19)

add($object, array $data = [])

Add the object to the register. Ignore the $data property if using the AbstractBoolenRegister., (*20)

remove($object)

Remove the object from the register., (*21)

check($object)

For AbstractBooleanRegister, returns true or false if the object is on the register., (*22)

For AbstractValueRegister, returns the stored data if the object is on the register, otherwise returns null., (*23)

all()

Returns all the objects on the register., (*24)

keys()

Returns the primary keys of all the objects on the register., (*25)

count()

Returns the number of objects on the register., (*26)

refresh()

Updates the cache of objects on the register., (*27)

The Versions

14/11 2017

dev-master

9999999-dev

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

The Development Requires

14/11 2017

2.2.7

2.2.7.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

The Development Requires

14/11 2017

2.2.6

2.2.6.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

The Development Requires

14/11 2017

2.2.5

2.2.5.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

The Development Requires

14/11 2017

2.2.4

2.2.4.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

The Development Requires

14/11 2017

2.2.3

2.2.3.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

The Development Requires

14/11 2017

2.2.2

2.2.2.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

The Development Requires

14/11 2017

2.2.1

2.2.1.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

The Development Requires

13/11 2017

2.2.0

2.2.0.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

The Development Requires

11/11 2017

2.1.4

2.1.4.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

The Development Requires

04/08 2017

2.1.3

2.1.3.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

The Development Requires

10/07 2017

2.1.2

2.1.2.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

The Development Requires

05/03 2017

2.1.1

2.1.1.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

The Development Requires

05/03 2017

2.1.0

2.1.0.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

The Development Requires

05/03 2017

2.0.6

2.0.6.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

The Development Requires

01/03 2017

2.0.5

2.0.5.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

The Development Requires

27/02 2017

2.0.4

2.0.4.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

The Development Requires

27/02 2017

2.0.3

2.0.3.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

27/02 2017

2.0.2

2.0.2.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

08/12 2016

1.1.6

1.1.6.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

05/12 2016

1.1.5

1.1.5.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

05/12 2016

1.1.4

1.1.4.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires

 

24/10 2016

1.0.0

1.0.0.0

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

  Sources   Download

MIT

The Requires