Issues (56)

src/Contract/Specification/Specification.php (1 issue)

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sheldon
5
 * Date: 18-6-7
6
 * Time: 下午5:49.
7
 */
8
9
namespace MiotApi\Contract\Specification;
10
11
use MiotApi\Contract\Interfaces\Specification as SpecificationInterface;
12
use MiotApi\Contract\RemoteSpec;
13
use MiotApi\Contract\Urn;
14
use MiotApi\Util\Collection\Collection;
15
16
abstract class Specification implements SpecificationInterface
17
{
18
    protected $collection;
19
20
    protected $urn;
21
22
    /**
23
     * 描述: 纯文本字段.
24
     *
25
     * @var
26
     */
27
    protected $description;
28
29
    /**
30
     * Specification constructor.
31
     *
32
     * @param $urn
33
     *
34
     * @throws \MiotApi\Exception\SpecificationErrorException
35
     */
36
    public function __construct($urn)
37
    {
38
        $this->urn = new Urn($urn);
39
        $this->init();
40
    }
41
42
    public function init()
43
    {
44
        $instanceType = $this->urn->getType();
45
        $items = RemoteSpec::{$instanceType}($this->urn->getBaseUrn());
46
        $this->collection = new Collection($items);
47
    }
48
49
    public function getUrn()
50
    {
51
        return $this->urn;
52
    }
53
54
    public function getType()
55
    {
56
        return $this->type;
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist on MiotApi\Contract\Specification\Specification. Since you implemented __get, consider adding a @property annotation.
Loading history...
57
    }
58
59
    public function getDescription()
60
    {
61
        return $this->description;
62
    }
63
64
    public function toContext()
65
    {
66
        return $this->toJson();
67
    }
68
69
    public function toCollection()
70
    {
71
        return $this->collection;
72
    }
73
74
    public function toJson()
75
    {
76
        return $this->collection->toJson();
77
    }
78
79
    public function toArray()
80
    {
81
        return $this->collection->toArray();
82
    }
83
84
    public function __get($key)
85
    {
86
        return $this->collection->offsetGet($key);
87
    }
88
89
    /**
90
     * Proxy a method call onto the collection items.
91
     *
92
     * @param string $method
93
     * @param array  $parameters
94
     *
95
     * @return mixed
96
     */
97
    public function __call($method, $parameters)
98
    {
99
        return $this->collection->{$method}(...$parameters);
100
    }
101
}
102