+// {{{ class StoredUserFilterBuilder
+class StoredUserFilterBuilder
+{
+ // Possible stored types (currently only 'ufb' exists)
+ const TYPE_UFB = 'ufb';
+
+ protected $ufb;
+ protected $env;
+ protected $ufc;
+
+ public function __construct(UserFilterBuilder $ufb, PlFilterCondition $ufc = null, array $env = array())
+ {
+ $this->ufb = $ufb;
+ $this->ufc = $ufc;
+ $this->env = $env;
+ }
+
+ public function export()
+ {
+ $export = new PlDict();
+ $export->set('type', self::TYPE_UFB);
+ $export->set('condition', $this->ufc->export());
+ $export->set('env', $this->env);
+ return $export;
+ }
+
+ public function getEnv()
+ {
+ return $this->env;
+ }
+
+ public function fillFromExport($export)
+ {
+ $export = new PlDict($export);
+ if (!$export->has('type')) {
+ throw new Exception("Missing 'type' field in export.");
+ }
+ if ($export->s('type') != self::TYPE_UFB) {
+ throw new Exception("Unknown type '$type' in export.");
+ }
+ $this->ufc = UserFilterCondition::fromExport($export->v('condition'));
+ $this->env = $export->v('env', array());
+ }
+
+ public function updateFromEnv($env)
+ {
+ $this->ufb->setFakeEnv($env);
+ if ($this->ufb->isValid()) {
+ $this->env = $env;
+ $this->ufc = $this->ufb->getUFC();
+ return true;
+ } else {
+ $this->ufb->clearFakeEnv();
+ return false;
+ }
+ }
+
+ public function refresh()
+ {
+ if ($this->isValid()) {
+ $this->ufc = $this->ufb->getUFC();
+ }
+ }
+
+ public function getUFC()
+ {
+ return $this->ufc;
+ }
+
+ public function isValid()
+ {
+ $this->ufb->setFakeEnv($this->env);
+ return $this->ufb->isValid();
+ }
+
+ public function isEmpty()
+ {
+ $this->ufb->setFakeEnv($this->env);
+ return $this->ufb->isEmpty();
+ }
+}
+// }}}
+