Skip to content

Commit be20e93

Browse files
committed
Updated way of accessing to fields
1 parent 580d00a commit be20e93

5 files changed

+110
-120
lines changed

AttributeFinder.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
namespace omnilight\datetime;
3+
use yii\base\Model;
4+
use yii\base\InvalidParamException;
5+
6+
7+
/**
8+
* Trait AttributeFinder
9+
*/
10+
trait AttributeFinder
11+
{
12+
/**
13+
* @param Model $model
14+
* @param $attribute
15+
* @return DateTimeAttribute
16+
*/
17+
protected static function findAttribute(Model $model, $attribute)
18+
{
19+
foreach ($model->behaviors as $behavior) {
20+
if (!($behavior instanceof DateTimeBehavior)) {
21+
continue;
22+
}
23+
24+
if ($behavior->hasAttribute($attribute)) {
25+
return $behavior->getAttribute($attribute);
26+
}
27+
}
28+
29+
throw new InvalidParamException('Model '.get_class($model).' does not have attribute '.$attribute);
30+
}
31+
}

DatePickerConfig.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace omnilight\datetime;
44

5+
use yii\base\InvalidParamException;
6+
use yii\base\Model;
57
use yii\helpers\ArrayHelper;
68
use yii\helpers\FormatConverter;
79

@@ -11,14 +13,19 @@
1113
*/
1214
class DatePickerConfig
1315
{
16+
use AttributeFinder;
17+
1418
/**
15-
* @param DateTimeAttribute $attribute
19+
* @param Model $model
20+
* @param $attribute
1621
* @param array $options
1722
* @param string $datePickerClass
1823
* @return array
1924
*/
20-
public static function get(DateTimeAttribute $attribute, $options = [], $datePickerClass = 'yii\jui\DatePicker')
25+
public static function get(Model $model, $attribute, $options = [], $datePickerClass = 'yii\jui\DatePicker')
2126
{
27+
$attribute = self::findAttribute($model, $attribute);
28+
2229
$format = DateTimeBehavior::normalizeIcuFormat($attribute->targetFormat, $attribute->behavior->formatter);
2330
switch ($datePickerClass) {
2431
case 'yii\jui\DatePicker':

DateTimeAttribute.php

+1-42
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Class DateTimeAttribute
1212
* @property string $value
1313
*/
14-
class DateTimeAttribute extends Object implements Arrayable
14+
class DateTimeAttribute extends Object
1515
{
1616
/**
1717
* @var DateTimeBehavior
@@ -79,45 +79,4 @@ public function setValue($value)
7979
$value = date_create_from_format($phpFormat, $value);
8080
$this->behavior->owner->{$this->originalAttribute} = $this->behavior->formatter->format($value, $this->originalFormat);
8181
}
82-
83-
/**
84-
* @inheritdoc
85-
*/
86-
public function fields()
87-
{
88-
return [];
89-
}
90-
91-
/**
92-
* Returns the list of additional fields that can be returned by [[toArray()]] in addition to those listed in [[fields()]].
93-
*
94-
* This method is similar to [[fields()]] except that the list of fields declared
95-
* by this method are not returned by default by [[toArray()]]. Only when a field in the list
96-
* is explicitly requested, will it be included in the result of [[toArray()]].
97-
*
98-
* @return array the list of expandable field names or field definitions. Please refer
99-
* to [[fields()]] on the format of the return value.
100-
* @see toArray()
101-
* @see fields()
102-
*/
103-
public function extraFields()
104-
{
105-
return [];
106-
}
107-
108-
/**
109-
* Converts the object into an array.
110-
*
111-
* @param array $fields the fields that the output array should contain. Fields not specified
112-
* in [[fields()]] will be ignored. If this parameter is empty, all fields as specified in [[fields()]] will be returned.
113-
* @param array $expand the additional fields that the output array should contain.
114-
* Fields not specified in [[extraFields()]] will be ignored. If this parameter is empty, no extra fields
115-
* will be returned.
116-
* @param boolean $recursive whether to recursively return array representation of embedded objects.
117-
* @return array the array representation of the object
118-
*/
119-
public function toArray(array $fields = [], array $expand = [], $recursive = true)
120-
{
121-
return $this->getValue();
122-
}
12382
}

DateTimeBehavior.php

+68-75
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,6 @@ public function init()
7373
$this->prepareAttributes();
7474
}
7575

76-
public function events()
77-
{
78-
$events = [];
79-
if ($this->performValidation) {
80-
$events[BaseActiveRecord::EVENT_BEFORE_VALIDATE] = 'onBeforeValidate';
81-
}
82-
return $events;
83-
}
84-
85-
/**
86-
* Performs validation for all the attributes
87-
* @param Event $event
88-
*/
89-
public function onBeforeValidate($event)
90-
{
91-
foreach ($this->attributeValues as $targetAttribute => $value) {
92-
if ($value instanceof DateTimeAttribute) {
93-
$validator = \Yii::createObject([
94-
'class' => DateValidator::className(),
95-
'format' => self::normalizeIcuFormat($value->targetFormat, $this->formatter)[1],
96-
]);
97-
$validator->validateAttribute($this->owner, $targetAttribute);
98-
}
99-
}
100-
}
101-
10276
protected function prepareAttributes()
10377
{
10478
foreach ($this->attributes as $key => $value) {
@@ -132,61 +106,30 @@ protected function processTemplate($originalAttribute)
132106
]);
133107
}
134108

135-
public function canGetProperty($name, $checkVars = true)
136-
{
137-
if ($this->hasAttributeValue($name))
138-
return true;
139-
else
140-
return parent::canGetProperty($name, $checkVars);
141-
}
142-
143-
protected function hasAttributeValue($name)
144-
{
145-
return isset($this->attributeValues[$name]);
146-
}
147-
148-
public function canSetProperty($name, $checkVars = true)
149-
{
150-
if ($this->hasAttributeValue($name))
151-
return true;
152-
else
153-
return parent::canSetProperty($name, $checkVars);
154-
}
155-
156-
public function __get($name)
157-
{
158-
if ($this->hasAttributeValue($name))
159-
return $this->getAttributeValue($name);
160-
return parent::__get($name);
161-
}
162-
163-
public function __set($name, $value)
164-
{
165-
if ($this->hasAttributeValue($name)) {
166-
$this->setAttributeValue($name, $value);
167-
return;
168-
}
169-
parent::__set($name, $value);
170-
}
171-
172-
protected function getAttributeValue($name)
109+
public function events()
173110
{
174-
if (is_array($this->attributeValues[$name])) {
175-
$this->attributeValues[$name] = \Yii::createObject($this->attributeValues[$name]);
111+
$events = [];
112+
if ($this->performValidation) {
113+
$events[BaseActiveRecord::EVENT_BEFORE_VALIDATE] = 'onBeforeValidate';
176114
}
177-
return $this->attributeValues[$name];
115+
return $events;
178116
}
179117

180-
protected function setAttributeValue($name, $value)
118+
/**
119+
* Performs validation for all the attributes
120+
* @param Event $event
121+
*/
122+
public function onBeforeValidate($event)
181123
{
182-
if (is_array($this->attributeValues[$name])) {
183-
$this->attributeValues[$name] = \Yii::createObject($this->attributeValues[$name]);
124+
foreach ($this->attributeValues as $targetAttribute => $value) {
125+
if ($value instanceof DateTimeAttribute) {
126+
$validator = \Yii::createObject([
127+
'class' => DateValidator::className(),
128+
'format' => self::normalizeIcuFormat($value->targetFormat, $this->formatter)[1],
129+
]);
130+
$validator->validateAttribute($this->owner, $targetAttribute);
131+
}
184132
}
185-
186-
if ($value instanceof DateTimeAttribute)
187-
$this->attributeValues[$name] = $value;
188-
else
189-
$this->attributeValues[$name]->value = $value;
190133
}
191134

192135
/**
@@ -213,4 +156,54 @@ public static function normalizeIcuFormat($format, $formatter)
213156
}
214157
return $format;
215158
}
159+
160+
public function canGetProperty($name, $checkVars = true)
161+
{
162+
if ($this->hasAttribute($name)) {
163+
return true;
164+
}
165+
166+
return parent::canGetProperty($name, $checkVars);
167+
}
168+
169+
public function hasAttribute($name)
170+
{
171+
return isset($this->attributeValues[$name]);
172+
}
173+
174+
public function canSetProperty($name, $checkVars = true)
175+
{
176+
if ($this->hasAttribute($name)) {
177+
return true;
178+
}
179+
180+
return parent::canSetProperty($name, $checkVars);
181+
}
182+
183+
public function __get($name)
184+
{
185+
if ($this->hasAttribute($name)) {
186+
return $this->getAttribute($name)->getValue();
187+
}
188+
189+
return parent::__get($name);
190+
}
191+
192+
public function __set($name, $value)
193+
{
194+
if ($this->hasAttribute($name)) {
195+
$this->getAttribute($name)->setValue($value);
196+
return;
197+
}
198+
199+
parent::__set($name, $value);
200+
}
201+
202+
public function getAttribute($name)
203+
{
204+
if (is_array($this->attributeValues[$name])) {
205+
$this->attributeValues[$name] = \Yii::createObject($this->attributeValues[$name]);
206+
}
207+
return $this->attributeValues[$name];
208+
}
216209
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Post extends ActiveRecord
7575
Now in your view with the form:
7676
```php
7777
// $model has instance of Post
78-
<?= $form->field($model, 'posted_at_local')->widget(\yii\jui\DatePicker::className(), \omnilight\datetime\DatePickerConfig::get($model->posted_at_local)) ?>
78+
<?= $form->field($model, 'posted_at_local')->widget(\yii\jui\DatePicker::className(), \omnilight\datetime\DatePickerConfig::get($model, 'posted_at_local')) ?>
7979
// DatePickerConfig is used to properly configure widget. Currently it only supports DatePicker from the Jui extension
8080
```
8181

0 commit comments

Comments
 (0)