Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit 5a01aa7

Browse files
committed
up
1 parent 990112a commit 5a01aa7

File tree

4 files changed

+128
-92
lines changed

4 files changed

+128
-92
lines changed

src/Collections/ActiveData.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
* Date: 2015/3/14
66
* Time: 19:44
77
* Use : 跟 \stdClass 一样,多的功能是 -- 提供数组方式访问属性
8-
* 与同文件夹下的 FixedData.php 区别是
9-
* 数据较为活跃,可随意添加属性,
10-
* 获取不存在的属性也并不报错(return null)
118
* File: ActiveData.php
129
*/
1310

@@ -53,11 +50,7 @@ public function load($data, $recursive = false)
5350
$name = trim($name);
5451

5552
if (is_numeric($name)) {
56-
$name = 'attr_' . $name;
57-
}
58-
59-
if (!$name) {
60-
$name = 'attr_';
53+
continue;
6154
}
6255

6356
$this->$name = $recursive && is_array($value) ? static::create($value, $recursive) : $value;

src/Collections/FixedData.php

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/Collections/JsonMessage.php

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,45 @@
2525
* }
2626
* }
2727
*/
28-
class JsonMessage extends ActiveData
28+
class JsonMessage
2929
{
3030
/**
3131
* @var int
3232
*/
33-
public $code = 0;
33+
public $code;
3434

3535
/**
3636
* @var string
3737
*/
38-
public $msg = 'success';
38+
public $msg;
3939

4040
/**
41-
* @var array
41+
* @var int|float
4242
*/
43-
public $data = [];
43+
public $time;
44+
45+
/**
46+
* @var array|string
47+
*/
48+
public $data;
49+
50+
public static function make($data = null, $msg = 'success', $code = 0)
51+
{
52+
return new static($data, $msg, $code);
53+
}
54+
55+
/**
56+
* JsonMessage constructor.
57+
* @param null $data
58+
* @param string $msg
59+
* @param int $code
60+
*/
61+
public function __construct($data = null, $msg = 'success', $code = 0)
62+
{
63+
$this->data = $data;
64+
$this->msg = $msg;
65+
$this->code = $code;
66+
}
4467

4568
/**
4669
* @return bool
@@ -81,17 +104,33 @@ public function msg($msg)
81104
}
82105

83106
/**
84-
* @param array $data
107+
* @param string $key
108+
* @param mixed $value
109+
*/
110+
public function add($key, $value)
111+
{
112+
if (null === $this->data) {
113+
$this->data = [];
114+
}
115+
116+
$this->data[$key] = $value;
117+
}
118+
119+
/**
120+
* @param array|string $data
85121
* @return $this
86122
*/
87-
public function data(array $data)
123+
public function data($data)
88124
{
89125
$this->data = $data;
90126

91127
return $this;
92128
}
93129

94-
public function all($toArray = true)
130+
/**
131+
* @return array
132+
*/
133+
public function all()
95134
{
96135
// add a new alert message
97136
return [
@@ -101,28 +140,51 @@ public function all($toArray = true)
101140
];
102141
}
103142

143+
/**
144+
* @return array
145+
*/
104146
public function toArray()
105147
{
106148
return $this->all();
107149
}
108150

109-
public function __set($name, $value)
151+
/**
152+
* @return string
153+
*/
154+
public function __toString()
110155
{
111-
$this->data[$name] = $value;
156+
return json_encode($this->all());
157+
}
158+
159+
/**
160+
* @param string $name
161+
* @return bool
162+
*/
163+
public function __isset($name)
164+
{
165+
return isset($this->data[$name]);
112166
}
113167

114-
// disable unset property
115-
public function offsetUnset($offset)
168+
/**
169+
* @param string $name
170+
* @param $value
171+
*/
172+
public function __set($name, $value)
116173
{
117-
//$this->$offset = null;
174+
$this->data[$name] = $value;
118175
}
119176

177+
/**
178+
* @param string $name
179+
* @return mixed
180+
* @throws PropertyException
181+
*/
120182
public function __get($name)
121183
{
122184
if (isset($this->data[$name])) {
123185
return $this->data[$name];
124186
}
125187

126-
throw new PropertyException(sprintf('获取不存在的属性 %s !', $name));
188+
throw new PropertyException(sprintf('the property is not exists: %s', $name));
127189
}
128190
}

src/Components/DataResult.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-10-25
6+
* Time: 17:27
7+
*/
8+
9+
namespace Inhere\Library\Components;
10+
11+
/**
12+
* Class DeferredResult
13+
* @package Inhere\Library\Components
14+
*/
15+
class DataResult
16+
{
17+
/**
18+
* @var mixed
19+
*/
20+
protected $value;
21+
22+
/**
23+
* DeferredResult constructor.
24+
* @param $value
25+
*/
26+
public function __construct($value)
27+
{
28+
$this->value = $value;
29+
}
30+
31+
public function get()
32+
{
33+
return $this->value;
34+
}
35+
36+
/**
37+
* @return mixed
38+
*/
39+
public function getValue()
40+
{
41+
return $this->value;
42+
}
43+
44+
/**
45+
* @param mixed $value
46+
*/
47+
public function setValue($value)
48+
{
49+
$this->value = $value;
50+
}
51+
}

0 commit comments

Comments
 (0)