Memory::write()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * FileSystem\Memory
5
 *
6
 * Temp Memory Filesystem
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2015 - http://caffeina.it
11
 */
12
13
namespace FileSystem;
14
15
class Memory implements Adapter {
16
17
  protected $storage = [];
18
19
  public function exists($path){
20
    return isset($this->storage[$path]);
21
  }
22
23
  public function read($path){
24
    return $this->exists($path) ? $this->storage[$path] : false;
25
  }
26
27
  public function write($path, $data){
28
    $this->storage[$path] = $data;
29
  }
30
31
  public function append($path, $data){
32
    @$this->storage[$path] .= $data;
33
  }
34
35
  public function move($old, $new){
36
    if($this->exists($old)){
37
      $this->storage[$new] = $this->storage[$old];
38
      unset($this->storage[$old]);
39
      return true;
40
    } else return false;
41
  }
42
43
  public function delete($path){
44
    unset($this->storage[$path]);
45
    return true;
46
  }
47
48
  public function search($pattern, $recursive=true){
49
    $results = [];
50
    $rx_pattern = '('.strtr($pattern,['.'=>'\.','*'=>'.*','?'=>'.']).')Ai';
51
    foreach (array_keys($this->storage) as $path) {
52
      if (preg_match($rx_pattern,$path)) $results[] = $path;
53
    }
54
    return $results;
55
  }
56
}
57