<?php

class UserCategoriesWrapper {

  private $user;
  private $wrapper;

  function __construct($user) {
    $this->user = $user;
    $this->wrapper = entity_metadata_wrapper('user', $user);
  }

  function addCategory($tid) {
    if (!$this->isCategoryEnabled($tid)) {
      $this->wrapper->field_categories[] = (string)$tid;
      $this->wrapper->save();
    }
  }

  function removeCategory($tid) {
    $categories = $this->wrapper->field_categories->raw();
    $key = array_search($tid, $categories);
    if (is_numeric($key)) {
      $this->wrapper->field_categories[$key]->set(NULL);
      $this->wrapper->save();
    }
  }

  function isCategoryEnabled($tid) {
    $categories = $this->wrapper->field_categories->raw();
    return in_array($tid, $categories);
  }
}
