Autodoc
  • Namespace
  • Class
  • Tree

Namespaces

  • BlueTihi
    • Context
  • Brickrouge
    • Element
      • Nodes
    • Renderer
    • Widget
  • ICanBoogie
    • ActiveRecord
    • AutoConfig
    • CLDR
    • Composer
    • Core
    • Event
    • Exception
    • HTTP
      • Dispatcher
      • Request
    • I18n
      • Translator
    • Mailer
    • Modules
      • Taxonomy
        • Support
      • Thumbnailer
        • Versions
    • Object
    • Operation
      • Dispatcher
    • Prototype
    • Routes
    • Routing
      • Dispatcher
    • Session
  • Icybee
    • ActiveRecord
      • Model
    • ConfigOperation
    • Document
    • EditBlock
    • Element
      • ActionbarContextual
      • ActionbarSearch
      • ActionbarToolbar
    • FormBlock
    • Installer
    • ManageBlock
    • Modules
      • Articles
      • Cache
        • Collection
        • ManageBlock
      • Comments
        • ManageBlock
      • Contents
        • ManageBlock
      • Dashboard
      • Editor
        • Collection
      • Files
        • File
        • ManageBlock
      • Forms
        • Form
        • ManageBlock
      • I18n
      • Images
        • ManageBlock
      • Members
      • Modules
        • ManageBlock
      • Nodes
        • ManageBlock
        • Module
      • Pages
        • BreadcrumbElement
        • LanguagesElement
        • ManageBlock
        • NavigationBranchElement
        • NavigationElement
        • Page
        • PageController
      • Registry
      • Search
      • Seo
      • Sites
        • ManageBlock
      • Taxonomy
        • Terms
          • ManageBlock
        • Vocabulary
          • ManageBlock
      • Users
        • ManageBlock
        • NonceLogin
        • Roles
      • Views
        • ActiveRecordProvider
        • Collection
        • View
    • Operation
      • ActiveRecord
      • Constructor
      • Module
      • Widget
    • Rendering
  • None
  • Patron
  • PHP

Classes

  • Entry
  • Hooks
  • MetasHandler
  • Model
  • Module
  1 <?php
  2 
  3 /*
  4  * This file is part of the Icybee package.
  5  *
  6  * (c) Olivier Laviale <olivier.laviale@gmail.com>
  7  *
  8  * For the full copyright and license information, please view the LICENSE
  9  * file that was distributed with this source code.
 10  */
 11 
 12 namespace Icybee\Modules\Registry;
 13 
 14 use ICanBoogie\Exception;
 15 use ICanBoogie\ToArray;
 16 
 17 /**
 18  * Manages the metadatas associated with a target object.
 19  */
 20 class MetasHandler implements \ArrayAccess, ToArray
 21 {
 22     static private $models;
 23 
 24     /**
 25      * Identifier of the target.
 26      *
 27      * @var int
 28      */
 29     protected $targetid;
 30 
 31     /**
 32      * Values associated with the target.
 33      *
 34      * @var array
 35      */
 36     protected $values;
 37 
 38     /**
 39      * Model managing the values.
 40      *
 41      * @var Model
 42      */
 43     protected $model;
 44 
 45     /**
 46      * Initializes the {@link $targetid} and {@link $model} properties.
 47      *
 48      * @param \ICanBoogie\ActiveRecord $target
 49      *
 50      * @throws Exception
 51      */
 52     public function __construct(\ICanBoogie\ActiveRecord $target)
 53     {
 54         if ($target instanceof \Icybee\Modules\Nodes\Node)
 55         {
 56             $this->targetid = $target->nid;
 57             $type = 'node';
 58         }
 59         else if ($target instanceof \Icybee\Modules\Users\User)
 60         {
 61             $this->targetid = $target->uid;
 62             $type = 'user';
 63         }
 64         else if ($target instanceof \Icybee\Modules\Sites\Site)
 65         {
 66             $this->targetid = $target->siteid;
 67             $type = 'site';
 68         }
 69         else
 70         {
 71             throw new Exception('Metadatas are not supported for instances of %class', [ '%class' => get_class($target) ]);
 72         }
 73 
 74         if (empty(self::$models[$type]))
 75         {
 76             self::$models[$type] = \ICanBoogie\ActiveRecord\get_model('registry/' . $type);
 77         }
 78 
 79         $this->model = self::$models[$type];
 80     }
 81 
 82     public function get($name, $default=null)
 83     {
 84         if ($this->values === null)
 85         {
 86             $this->values = $this->model
 87             ->select('name, value')
 88             ->filter_by_targetid($this->targetid)
 89             ->order('name')
 90             ->pairs;
 91         }
 92 
 93         if ($name == 'all')
 94         {
 95             return $this->values;
 96         }
 97 
 98         if (!isset($this->values[$name]))
 99         {
100             return $default;
101         }
102 
103         return $this->values[$name];
104     }
105 
106     public function set($name, $value)
107     {
108         $this->values[$name] = $value;
109 
110         if ($value === null)
111         {
112             $this->model->filter_by_targetid_and_name($this->targetid, $name)->delete();
113 
114             return;
115         }
116 
117         $this->model->insert([
118 
119             'targetid' => $this->targetid,
120             'name' => $name,
121             'value' => $value
122 
123         ], [ 'on duplicate' => true ]);
124     }
125 
126     public function to_array()
127     {
128         return $this->get('all');
129     }
130 
131     public function offsetSet($offset, $value)
132     {
133         $this->set($offset, $value);
134     }
135 
136     public function offsetExists($offset)
137     {
138         return $this->get($offset) !== null;
139     }
140 
141     public function offsetUnset($offset)
142     {
143         $this->set($offset, null);
144     }
145 
146     public function offsetGet($offset)
147     {
148         return $this->get($offset);
149     }
150 }
Autodoc API documentation generated by ApiGen 2.8.0