IComeFromTheNet.

Released QuickTag - A Tag Library for Silex

  • Tags: php, projects,
  • Written on: December 28, 2012


I wrote this component to provide a composer available tag library that uses doctrine DBAL as its back-end. It can be downloaded from github.

A Tag has the following properties:

  • Title (45 character) name. no default case upper or lower fine.
  • Weight (float) used to order a set of tags.
  • Created (DateTime) used to sort old and new tags
  • UserContext (integer) restrict tags to a given user id.

Features of the library:

  • Written using Doctrine DBAL.
  • Optional Restful Silex API Supports GET/POST/PUT/DELETE.
  • Can be used with Zend/Tag/Cloud, doing tag clouds is easy.
  • PHP 5.3 and up.

API Methods

Store a tag Create/Update

<?php
    use DateTime;
    use QuickTag\Model\StoredTag;
 
    $storedTag = new StoredTag();
    $storedTag->setTitle('mytitle');
    $storedTag->setWeight(1);
    $storedTag->setTagCreated(new DateTime());
    $storedTag->setUserContext(3);
 
    # fetch service from the provider
    $result = $tagService->storeTag($storeTag);
 
    if($result) {
        echo 'tag has been stored at id '. $storedTag->getTagId();
    }
?>

During an update the id must be set and only the title and weight and user context can be changed.

Lookup a tag by id.

<?php

    use QuickTag\Model\StoredTag;
 
    # fetch service from the provider
    $storeTag = $tagService->lookupTag($id);
 
    if($storedTag instanceOf StoredTag ) {
        echo 'tag has been gound at id '. $storedTag->getTagId();
    }
?>

Searching for a tag

<?php
    # Search for tags started with titte `my` and belong to user 3
    $tagCollection = $tagService->findTag()
            ->start()
                ->limit($limit)
                ->offset($offset)
                ->orderByTitle('asc')
                ->filterByNameStartsWith('my')
                ->filterByUserContext(3)
            ->end()
        ->find();
 
    if(count($tagCollection) > 0 ) {
        echo sprintf('found %s number of tags',count($tagCollection));
    }
?>

Removing a Tag.

<?php
    use QuickTag\Model\StoredTag;
 
    $id = 1;
 
    # fetch service from the provider
    $storeTag = $tagService->lookupTag($id);
 
    $result = $tagService->removeTag($storeTag);
 
    if($result) {
        echo 'tag has been removed at id '. $storedTag->getTagId();
    }
?>

The API under QuickTag\Silex\Provider\TagServiceProvider has basic examples on how to use the library.

Using Zend Tag Cloud

<?php
    use Zend\Tag\Cloud;
    use QuickTag\Model\StoredTag;
 
    $tagA = new StoredTag();
    $tagB = new StoredTag();
    $tagC = new StoredTag();
 
    $cloud = new Cloud(array(
        'tags' => array(
           $tagA,$tagB,$tagC
        )
    ));
 
    // Render the cloud
    echo $cloud;
?>