|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System PHP Framework package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
|
13
|
|
|
|
|
14
|
|
|
namespace O2System\Reactor\Models\Files; |
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
|
17
|
|
|
|
|
18
|
|
|
use O2System\Filesystem\Files\JsonFile; |
|
19
|
|
|
use O2System\Filesystem\Files\XmlFile; |
|
20
|
|
|
use O2System\Reactor\Models\Files\Traits\FinderTrait; |
|
21
|
|
|
use O2System\Psr\Patterns\Structural\Repository\AbstractRepository; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class Model |
|
25
|
|
|
* @package O2System\Reactor\Models\Files |
|
26
|
|
|
*/ |
|
27
|
|
|
class Model extends AbstractRepository |
|
28
|
|
|
{ |
|
29
|
|
|
use FinderTrait; |
|
30
|
|
|
|
|
31
|
|
|
public $file; |
|
32
|
|
|
public $result; |
|
33
|
|
|
public $primaryKey = 'id'; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct() |
|
36
|
|
|
{ |
|
37
|
|
|
if ( ! empty($this->file)) { |
|
38
|
|
|
$extension = pathinfo($this->file, PATHINFO_EXTENSION); |
|
39
|
|
|
|
|
40
|
|
|
switch ($extension) { |
|
41
|
|
|
case 'json': |
|
42
|
|
|
$jsonFile = new JsonFile($this->file); |
|
43
|
|
|
$this->storage = $jsonFile->readFile()->getArrayCopy(); |
|
44
|
|
|
break; |
|
45
|
|
|
case 'xml': |
|
46
|
|
|
$xmlFile = new XmlFile($this->file); |
|
47
|
|
|
$this->storage = $xmlFile->readFile()->getArrayCopy(); |
|
48
|
|
|
break; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$first = reset($this->storage); |
|
52
|
|
|
if ( ! isset($first[ $this->primaryKey ])) { |
|
53
|
|
|
$keys = $first->getKeys(); |
|
54
|
|
|
$this->primaryKey = reset($keys); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |