forked from noumo/easyii
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiObject.php
81 lines (71 loc) · 2.04 KB
/
ApiObject.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace yii\easyii\components;
use Yii;
use yii\easyii\helpers\Image;
/**
* Class ApiObject
* @package yii\easyii\components
* @var integer $id
* @var string $image
*/
class ApiObject extends \yii\base\Object
{
/** @var \yii\base\Model */
public $model;
/**
* Generates ApiObject, attaching all settable properties to the child object
* @param \yii\base\Model $model
*/
public function __construct($model){
if($model) {
$this->model = $model;
foreach ($model->attributes as $attribute => $value) {
if ($this->canSetProperty($attribute)) {
$this->{$attribute} = $value;
}
}
} else {
$this->model = new \stdClass();
}
$this->init();
}
/**
* calls after __construct
*/
public function init(){}
/**
* Returns object id
* @return int
*/
public function getId($debug = false){
return $this->model->primaryKey;
}
/**
* Creates thumb from model->image attribute with specified width and height.
* @param int|null $width
* @param int|null $height
* @param bool $crop if false image will be resize instead of cropping
* @return string
*/
public function thumb($width = null, $height = null)
{
return !empty($this->model->image_file) ? Image::thumb($this->model->image_file, $width, $height) : '';
}
/**
* Returns web path to image.
* @return string
*/
public function getImage()
{
return !empty($this->model->image_file) ? $this->model->image : '';
}
/**
* Get seo text attached to object
* @param string $attribute name of seo attribute can be h1, title, description, keywords
* @param string $default default string applied if seo text not found
* @return string
*/
public function seo($attribute, $default = ''){
return !empty($this->model->seo->{$attribute}) ? $this->model->seo->{$attribute} : $default;
}
}