carrooi / contactable
Nette 框架和 Doctrine 的 Contactable 模块
Requires
- kdyby/doctrine: ~2.2
- kdyby/events: ~2.3
- nette/application: ~2.2
- nette/di: ~2.2
- nette/utils: ~2.2
Requires (Dev)
- nette/bootstrap: ~2.2
- nette/mail: ~2.2
- nette/safe-stream: ~2.2
- nette/tester: ~1.3.0
This package is auto-updated.
Last update: 2024-09-06 03:33:48 UTC
README
Nette 框架和 Doctrine 的 Contactable 模块。
安装
$ 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:实体,具有联系类型和用户实体等之间的关联
- associations:具有在
contactItemClass
中定义的 manyToOne 关联的字段名称的 contactable 实体列表
联系实体
实现 Carrooi\Contactable\Model\Entities\IContactableEntity
接口并具有以下方法的实体
getId()
:返回标识符getContacts()
:返回Carrooi\Contactable\Model\Entities\IContactItem
实体的数组addContact()
:将新的Carrooi\Contactable\Model\Entities\IContactItem
实体添加到集合中removeContact()
:从集合中删除Carrooi\Contactable\Model\Entities\IContactItem
实体
当然,您不需要自己实现所有必需的方法(除了 getId()
方法)。只需使用预定义的特性 Carrooi\Contactable\Model\Entities\TContactable
。
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; } }
联系项实体
包含实际联系值的实体,例如用户的实际电子邮件地址。此实体必须实现 Carrooi\Contactable\Model\Entities\IContactItem
接口,并具有以下方法
getId()
:返回标识符getContactType()
返回Carrooi\Contactable\Model\Entities\IContactType
实体setContactType()
设置Carrooi\Contactable\Model\Entities\IContactType
实体getValue()
:返回联系值setValue()
:设置联系值validateValue()
:验证值是否与联系类型的模式匹配
同样,您可以使用预定义的特性 Carrooi\Contactable\Model\Entities\TContactItem
。
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 }
如您所见,我们有一个 user
字段,它对应于您在配置中设置的 associations
。
联系类型
您可以根据需要创建任意多的类型,例如电子邮件、Facebook、电话号码等。
为此已经准备了一个服务:Carrooi\Contactable\Model\Facades\ContactTypesFacade
。
创建联系类型:
$type = $types->create('mail', 'Email', [ 'pattern' => '[a-z]+@[a-z]+\.[a-z]{2,3}', // really naive mail regex 'url' => 'mail.org', ]);
参数
name
:必需的 "system" 名称title
:必需的 "public" 名称值
:- 非必需的其他值列表
pattern
:非必需的模式,用于此类型的所有联系值url
:非必需的联系 URL
更新联系类型:
$types->update($type, [ 'name' => 'email', 'title' => 'Email address', 'pattern' => '.+', // no more naive, just stupid 'url' => 'mail.com', ]);
删除联系类型:
$types->remove($type);
获取所有联系类型:
foreach ($types->findAll() as $type) { // ... }
获取 id => 名称对:
foreach ($types->findAllNames() as $id => $name) { // ... }
通过 id 查找联系类型:
$type = $types->findOneById($id);
通过名称查找联系类型:
$type = $types->findOneByName($name);
联系项外观
还有一个服务 Carrooi\Contactable\Model\Facades\ContactItemsFacade
,可用于将联系添加到 contactable 实体。
添加联系:
$item = $items->addContact($user, $type, 'lorem@ipsum.com');
更新联系:
$items->update($item, [ 'contactType' => $anotherType, 'value' => '999888777', ]);
删除联系:
$items->remove($item);
通过 id 查找:
$item = $items->findOneById($id);
通过实体查找所有:
foreach ($items->findAllByEntity($user) as $item) { // ... }
变更日志
- 1.0.0
- 初始版本