dev-master
9999999-devContactable module for Nette framework and Doctrine
MIT
The Requires
The Development Requires
by David Kudera
nette contacts carrooi contactable
Wallogit.com
2017 © Pedro Peláez
Contactable module for Nette framework and Doctrine
Contactable module for Nette framework and Doctrine., (*2)
$ composer require carrooi/contactable $ composer update
extensions:
contactable: Carrooi\Contactable\DI\ContactableExtension
contactable:
contactItemClass: App\Model\Entities\ContactItem
associations:
App\Model\Entities\User: user
contactItemClass
Entity which implements Carrooi\Contactable\Model\Entities\IContactableEntity interface with these methods:, (*3)
getId(): returns identifiergetContacts(): returns array of Carrooi\Contactable\Model\Entities\IContactItem entitiesaddContact(): adds new Carrooi\Contactable\Model\Entities\IContactItem entity to collectionremoveContact(): removes Carrooi\Contactable\Model\Entities\IContactItem entity from collectionOf course you don't need to implement all required methods on your own (except for getId() method). Just use prepared trait Carrooi\Contactable\Model\Entities\TContactable., (*4)
namespace App\Model\Entities;
use Carrooi\Contactable\Model\Entities\IContactableEntity;
use Carrooi\Contactable\Model\Entities\TContactable;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @author David Kudera
*/
class User implements IContactableEntity
{
use TContactable;
// ...
/**
* @return int
*/
public function getId()
{
return $this->id;
}
}
Entity which holds the actual value of contact, eg. actual email address of user. This entity must implement Carrooi\Contactable\Model\Entities\IContactItem interface with these methods:, (*5)
getId(): returns identifiergetContactType() returns Carrooi\Contactable\Model\Entities\IContactType entitysetContactType() sets Carrooi\Contactable\Model\Entities\IContactType entitygetValue(): returns value of contactsetValue(): sets value of contactvalidateValue() validate value against pattern in contact typeAgain, you can use prepared trait Carrooi\Contactable\Model\Entities\TContactItem., (*6)
namespace App\Model\Entities;
use Carrooi\Contactable\Model\Entities\IContactItem;
use Carrooi\Contactable\Model\Entities\TContactItem;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @author David Kudera
*/
class ContactItem implements IContactItem
{
use TContactItem;
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @var int
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="\App\Model\Entities\User")
* @var \App\Model\Entities\User
*/
private $user;
// ... some getters and setters for all fields
}
As you can see, we've got user field which corresponding with associations setup in your configuration., (*7)
You can create as many types as you want, eg. mails, facebook, phone numbers etc., (*8)
There is already prepared service for that: Carrooi\Contactable\Model\Facades\ContactTypesFacade., (*9)
Create contact type:, (*10)
$type = $types->create('mail', 'Email', [
'pattern' => '[a-z]+@[a-z]+\.[a-z]{2,3}', // really naive mail regex
'url' => 'mail.org',
]);
arguments:, (*11)
name: required "system" nametitle: required "public" namevalues:
pattern: not required pattern for all contact values of this typeurl: not required url to contactUpdate contact type:, (*12)
$types->update($type, [
'name' => 'email',
'title' => 'Email address',
'pattern' => '.+', // no more naive, just stupid
'url' => 'mail.com',
]);
Remove contact type:, (*13)
$types->remove($type);
Get all contact types:, (*14)
foreach ($types->findAll() as $type) {
// ...
}
Get id => names pairs:, (*15)
foreach ($types->findAllNames() as $id => $name) {
// ...
}
Find contact type by id:, (*16)
$type = $types->findOneById($id);
Find contact type by name:, (*17)
$type = $types->findOneByName($name);
There is also service Carrooi\Contactable\Model\Facades\ContactItemsFacade which can be used for adding contacts to contactable entities., (*18)
Add contact:, (*19)
$item = $items->addContact($user, $type, 'lorem@ipsum.com');
Update contact:, (*20)
$items->update($item, [
'contactType' => $anotherType,
'value' => '999888777',
]);
Remove contact:, (*21)
$items->remove($item);
Find by id:, (*22)
$item = $items->findOneById($id);
Find all by entity:, (*23)
foreach ($items->findAllByEntity($user) as $item) {
// ...
}
Contactable module for Nette framework and Doctrine
MIT
nette contacts carrooi contactable