Registry::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Logikos\Util;
4
5
/**
6
 * This is a php version of \Phalcon\Registry
7
 * NOTICE: \Phalcon\Registry will be much faster than this class and you are encouraged to use it
8
 *
9
 *<code>
10
 *  $registry = new \Phalcon\Registry();
11
 *
12
 *  // Set value
13
 *  $registry->something = 'something';
14
 *  $registry['something'] = 'something';
15
 *
16
 *  // Get value
17
 *  $value = $registry->something;
18
 *  $value = $registry['something'];
19
 *
20
 *  // Check if the key exists
21
 *  $exists = isset($registry->something);
22
 *  $exists = isset($registry['something']);
23
 *
24
 *  // Unset
25
 *  unset($registry->something);
26
 *  unset($registry['something']);
27
 *</code>
28
 */
29
class Registry implements \ArrayAccess, \Countable, \Iterator {
30
  private $values;
31
32 75
  public function __construct(array $data = []) {
33 75
    $this->values = $data;
34 75
  }
35
36
  # Countable
37 5
  public function count() {
38 5
    return count($this->values);
39
  }
40
41
  /**
42
   * @param $offset
43
   * @return bool
44
   */
45 1
  public function isset($offset) {
46 1
    return isset($this->values[$offset]);
47
  }
48
49
  # ArrayAccess
50
  public function offsetGet($offset)       { return $this->requireOffset($offset); }
51
  public function offsetExists($offset)    { return array_key_exists($offset, $this->values); }
52
  public function offsetSet($offset, $val) { $this->values[$offset] = $val;        }
53
  public function offsetUnset($offset)     { unset($this->values[$offset]);        }
54
55
  # Iterator
56
  public function rewind()  { return reset($this->values);        }
57
  public function key()     { return key($this->values);          }
58
  public function current() { return current($this->values);      }
59
  public function next()    { return next($this->values);         }
60
  public function valid()   { return key($this->values) !== null; }
61
62
  # Magic Property Access
63
  public function __set($offset, $value) { $this->offsetSet($offset, $value);   }
64
  public function __unset($offset)       { $this->offsetUnset($offset);         }
65
  public function __get($offset)         { return $this->offsetGet($offset);    }
66
  public function __isset($offset)       { return $this->offsetExists($offset); }
67
68 29
  protected function requireOffset($offset) {
69 29
    if (!$this->offsetExists($offset))
70 3
      throw new \OutOfBoundsException("offset '{$offset}' does not exist");
71
72 26
    return $this->values[$offset];
73
  }
74
75 9
  protected function rawValues() {
76 9
    return $this->values;
77
  }
78
}