ArrayDebugger::offsetExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php 
2
namespace PHP;
3
4
class ArrayDebugger extends \ArrayObject { 
5
6
  const TYPE_GET    = 'GET';
7
  const TYPE_SET    = 'SET';
8
  const TYPE_EXISTS = 'EXISTS';
9
  const TYPE_UNSET  = 'UNSET';
10
11
  private $logger;
12
13
  public function __construct() 
14
  {
15
16
      $this->logger = function($data_log) {
17
        dump($data_log);
18
      };
19
20
      call_user_func_array('parent::__construct', func_get_args()); 
21
  }
22
23
  /**
24
   * Write log
25
   *
26
   * @param string $type
27
   * @param string $key
28
   * @param string $value
29
   * @param array  $backtrace
30
   */
31
  public function sendLog($type, $key, $value = null, array $backtrace ) 
32
  {
33
      $callbackLogger = $this->logger;
34
      return $callbackLogger(array(
35
          'type'  => $type,  
36
          'key'   => $key,
37
          'value' => $value,
38
          'file'  => isset($backtrace[0]['file']) ? $backtrace[0]['file'] : '',
39
          'line'  => isset($backtrace[0]['line']) ? $backtrace[0]['line'] : '',
40
      ));
41
  }
42
43
  /**
44
   * Defines logger
45
   *
46
   * @param \Closure $logger
47
   */
48
  public function setLogger(\Closure $logger) 
49
  {
50
      $this->logger = $logger;
51
  }
52
53
  /**
54
   * Called when getting a value from array
55
   *
56
   * @param mixed $name
57
   */
58
  public function offsetGet($name) 
59
  { 
60
      $this->sendLog(static::TYPE_GET, $name, null, debug_backtrace());
61
      return call_user_func_array('parent::offsetGet', func_get_args()); 
62
  } 
63
64
  /**
65
   * Called when setting a value from array
66
   *
67
   * @param mixed $name
68
   * @param mixed $value
69
   */
70
  public function offsetSet($name, $value) 
71
  { 
72
      $this->sendLog(static::TYPE_SET, $name, $value, debug_backtrace());
73
      return call_user_func_array('parent::offsetSet', func_get_args()); 
74
  } 
75
76
  /**
77
   * Called when checking a value from array 
78
   *
79
   * @param mixed $name
80
   */
81
  public function offsetExists($name) 
82
  { 
83
      $this->sendLog(static::TYPE_EXISTS, $name, null, debug_backtrace());
84
      return call_user_func_array('parent::offsetExists', func_get_args()); 
85
  } 
86
87
  /**
88
   * Called when removing a value from array
89
   *
90
   * @param mixed $name
91
   */
92
  public function offsetUnset($name) 
93
  { 
94
      $this->sendLog(static::TYPE_UNSET, $name, null, debug_backtrace());
95
      return call_user_func_array('parent::offsetUnset', func_get_args()); 
96
  } 
97
} 
98