Example #1
0
 /**
  * @param string   $content
  * @param string[] $attributes
  *
  * @return static
  */
 public static function make($content = null, $attributes = [])
 {
     $tag = new static();
     $tag->setContents($content);
     $tag->setAttributes($attributes);
     return $tag;
 }
Example #2
0
 /**
  * @param Option[] $options
  * @param string[] $attributes
  *
  * @return static
  */
 public static function make(array $options = [], $attributes = [])
 {
     $tag = new static();
     $tag->addOptions($options);
     $tag->setAttributes($attributes);
     return $tag;
 }
Example #3
0
 /**
  * @param Li[]     $listItems
  * @param string[] $attributes
  *
  * @return Ul|Ol
  */
 public static function make($listItems = [], $attributes = [])
 {
     $tag = new static();
     $tag->addListItems($listItems);
     $tag->setAttributes($attributes);
     return $tag;
 }
Example #4
0
 public static function createTag($tag, array $attributes = [], $content = null)
 {
     $html = new static();
     $html->setTag($tag);
     $html->setAttributes($attributes);
     $html->setContent($content);
     return $html;
 }
 /**
  * @inheritdoc
  */
 public static function model($params = [])
 {
     $model = new static();
     if ($params) {
         $model->setAttributes($params);
     }
     return $model;
 }
Example #6
0
 /**
  * 替换指定名称数据
  *
  * @param string $name 名称
  * @param string $value 值
  * @param int $status 状态
  * @return bool
  */
 public static function replaceByName($name, $value, $status)
 {
     $obj = static::find()->findByName($name)->one();
     if (!$obj) {
         $obj = new static();
     }
     $obj->setAttributes(['name' => $name, 'value' => $value, 'status' => $status]);
     return $obj->save();
 }
 public static function factory(RendererInterface $renderer, DataObject $object, $urlType, array $attributes = array())
 {
     $element = new static();
     $element->setView($renderer);
     $element->setObject($object);
     $element->setAttributes($attributes);
     $element->setUrlType($urlType);
     return $element->render();
 }
 /**
  * @param integer $account_id
  * @param string $key
  * @param mixed $value
  * @return static
  * @throw \InvalidArgumentException
  */
 public static function push($account_id, $key, $value)
 {
     $Property = static::find()->byAccountId($account_id)->byKey($key)->one();
     if (empty($Property)) {
         $Property = new static();
     }
     $Property->setAttributes(['account_id' => $account_id, 'key' => $key, 'value' => (string) $value]);
     $Property->validate() && $Property->save();
     return $Property;
 }
Example #9
0
 public static function findOne($cond)
 {
     $class = static::getStorageClass();
     $store = $class::findOne($cond);
     if (!$store) {
         return null;
     }
     $model = new static();
     $model->setAttributes($store->getAttributes($model->attributes()));
     return $model;
 }
Example #10
0
 /**
  * 
  * @param array $attributes
  * @return static
  * @throws Exception
  */
 public static function upsert($attributes)
 {
     $model = static::findOne(['ip_start' => $attributes['ip_start'], 'ip_end' => $attributes['ip_end']]);
     if (!$model) {
         $model = new static($attributes);
     } else {
         $model->setAttributes($attributes);
     }
     $model->save(false);
     return $model;
 }
Example #11
0
 /**
  * Creates a new user
  *
  * @param  array       $attributes the attributes given by field => value
  * @return static|null the newly created model, or null on failure
  */
 public static function create($attributes)
 {
     /** @var User $user */
     $user = new static();
     $user->setAttributes($attributes);
     $user->setPassword($attributes['password']);
     $user->generateAuthKey();
     if ($user->save()) {
         return $user;
     } else {
         return null;
     }
 }
Example #12
0
 /**
  * @param integer $account_id
  * @param string $key
  * @param mixed $value
  * @param bool $replace
  * @return static
  * @throw \InvalidArgumentException
  */
 public static function push($account_id, $key, $value, $replace = true)
 {
     /** @var static $Property */
     $Property = static::find()->byAccountId($account_id)->byKey($key)->one();
     if (empty($Property)) {
         $Property = new static();
     }
     if ($replace || $Property->isNewRecord) {
         $Property->setAttributes(['account_id' => $account_id, 'key' => $key, 'value' => (string) $value]);
         $Property->validate() && $Property->save();
     }
     return $Property;
 }
Example #13
0
 public static function find($id = null)
 {
     if (!empty($id)) {
         $domains = \yii\easyii\helpers\Multisite::getDomains();
         $key = array_search($id, array_column($domains, 'crc'));
         if (isset($domains[$key]) && !empty($domains[$key])) {
             $model = new static();
             $model->setAttributes($domains[$key]);
             return $model;
         }
         return null;
     }
     return \yii\easyii\helpers\Multisite::getDomains();
 }
Example #14
0
 /**
  * @param integer $account_id
  * @param string $social_type
  * @param integer|string $social_id
  * @param array|null $token
  * @return static
  */
 public static function push($account_id, $social_type, $social_id, $token = null)
 {
     /** @var static $Auth */
     $Auth = static::find()->byAccountId($account_id)->bySocialType($social_type)->bySocialId($social_id)->one();
     if (empty($Auth)) {
         $Auth = new static();
         $Auth->setAttributes(['account_id' => (int) $account_id, 'social_type' => (string) $social_type, 'social_id' => (string) $social_id]);
     }
     if (!empty($token)) {
         $Auth->token = Json::encode($token);
     }
     $Auth->validate() && $Auth->save(false);
     return $Auth;
 }
Example #15
0
 /**
  * Static factory method to create child objects
  *
  * @param  array $c
  * @return \Pop\Dom\Child
  */
 public static function factory(array $c)
 {
     $nodeName = $c['nodeName'];
     $nodeValue = isset($c['nodeValue']) ? $c['nodeValue'] : null;
     $childFirst = isset($c['childrenFirst']) ? $c['childrenFirst'] : false;
     $indent = isset($c['indent']) ? $c['indent'] : null;
     $child = new static($nodeName, $nodeValue, null, $childFirst, $indent);
     if (isset($c['attributes'])) {
         $child->setAttributes($c['attributes']);
     }
     if (isset($c['childNodes'])) {
         $child->addChildren($c['childNodes']);
     }
     return $child;
 }
Example #16
0
 /**
  * @param integer $account_id
  * @param integer $type
  * @param string $message
  * @param string|null $detail
  * @param integer|null $threshold
  * @return bool|null|Model
  */
 public static function push($account_id, $type, $message, $detail = null, $threshold = null)
 {
     $Alert = false;
     if (!empty($threshold)) {
         $Alert = static::find()->byMessage($message)->createdAfter(strtotime($threshold))->one();
     }
     if (empty($Alert)) {
         $Alert = static::find()->byMessage($message)->withoutDeleted()->one();
     }
     if (empty($Alert)) {
         $Alert = new static();
         $Alert->setAttributes(['account_id' => $account_id, 'type' => $type, 'message' => $message, 'detail' => $detail]);
         $Alert->validate() && $Alert->save(false);
     }
     return $Alert;
 }
Example #17
0
 /**
  * 收藏问题
  * @param User $user
  * @param Question $question
  */
 public static function question(User $user, Question $model)
 {
     $data = ['target_id' => $model->id, 'target_type' => $model::TYPE, 'author_id' => $user->id, 'status' => static::STATUS_ACTIVE];
     if (!static::deleteAll($data + ['type' => static::TYPE])) {
         // 删除数据有行数则代表有数据,无行数则添加数据
         $favorite = new static();
         $favorite->setAttributes($data);
         $result = $favorite->save();
         if ($result) {
             $model->updateCounters(['favorite_count' => 1]);
         }
         return [$result, $favorite];
     }
     $model->updateCounters(['favorite_count' => -1]);
     return [true, null];
 }
 /**
  * Find all CurrencyRateProvider and placing them into cache
  *
  * @return array Currency
  */
 public static function findAll()
 {
     if (0 == count(static::$models)) {
         $models = Yii::$app->cache->get(static::$cacheKey);
         if (false === $models) {
             $models = [];
             $items = CurrenciesModule::module()->getData(static::className(), true, false);
             foreach ($items as $item) {
                 $model = new static();
                 $model->setDefaults();
                 $model->setAttributes($item);
                 $models[$model->name] = $model;
             }
             if (false === empty($models)) {
                 Yii::$app->cache->set(static::$cacheKey, $models, 86400);
             }
         }
         static::$models = $models;
     }
     return static::$models;
 }
Example #19
0
 /**
  * 喜欢数据切换
  * @param User $user
  * @param ActiveRecord $model
  * @return array
  */
 protected static function toggleType(User $user, Post $model)
 {
     $data = ['target_id' => $model->id, 'target_type' => $model::TYPE, 'author_id' => $user->id, 'status' => static::STATUS_ACTIVE];
     if (!static::deleteAll($data + ['type' => static::TYPE])) {
         // 删除数据有行数则代表有数据,无行数则添加数据
         $like = new static();
         $like->setAttributes($data);
         $result = $like->save();
         if ($result) {
             // 如果是新增数据, 删除掉Hate的同类型数据
             $attributes = ['like_count' => 1];
             if (Hate::deleteAll($data + ['type' => Hate::TYPE])) {
                 // 如果有删除hate数据, hate_count也要-1
                 $attributes['hate_count'] = -1;
             }
             $model->updateCounters($attributes);
         }
         return [$result, $like];
     }
     $model->updateCounters(['like_count' => -1]);
     return [true, null];
 }
Example #20
0
 /**
  * @param $objectKey
  * @param $objectId
  * @param $path
  * @return bool
  */
 public static function add($objectKey, $objectId, $path)
 {
     /** @var static $model */
     $model = static::find()->andWhere(['object_key' => $objectKey, 'object_id' => $objectId])->one();
     $path = ltrim($path, '/');
     if ($model) {
         if ($model->path == $path) {
             return true;
         }
         $model->setAttribute('path', $path);
     } else {
         $model = new static();
         $model->setAttributes(['path' => $path, 'action_key' => static::ACTION_VIEW, 'object_key' => $objectKey, 'object_id' => $objectId]);
     }
     return $model->save();
 }
Example #21
0
 /**
  * 添加用户
  *
  * @param string $username 用户名
  * @param string $nickname 昵称
  * @param string $password 密码
  * @param string $note 备注
  * @param int $status 状态
  * @return mixed
  * @throws \yii\base\Exception
  * @throws \yii\base\InvalidConfigException
  */
 public static function addUser($username, $nickname, $password, $note = '', $status = 1)
 {
     $obj = new static();
     $obj->setAttributes(['username' => $username, 'nickname' => $nickname, 'password' => \Yii::$app->getSecurity()->generatePasswordHash($password, 4), 'note' => $note, 'status' => $status]);
     return $obj->save();
 }
Example #22
0
 /**
  * @param $key
  *
  * @return RedisItem
  * @throws NotFoundHttpException
  */
 public static function find($key)
 {
     $ex = Redisman::getInstance()->executeCommand('EXISTS', [$key]);
     if (!$ex) {
         throw new NotFoundHttpException(Redisman::t('redisman', 'key not found'));
     } else {
         list($type, $size, $ttl, $refcount, $idletype, $encoding) = Redisman::getInstance()->executeCommand('EVAL', [self::infoScript($key), 0]);
         $model = new static();
         $model->setAttributes(ArrayHelper::merge(['class' => 'insolita\\redisman\\models\\RedisItem', 'key' => $key, 'db' => Redisman::getInstance()->getCurrentDb(), 'storage' => Redisman::getInstance()->getCurrentConn()], compact('type', 'size', 'ttl', 'refcount', 'idletime', 'encoding')), false);
         $event = new Event();
         $event->data = $model->getAttributes();
         $model->onAfterFind($event);
         return $model;
     }
 }
Example #23
0
 /**
  * @param integer $try
  * @param string $period strtotime string format
  * @return boolean
  * @throws \yii\base\InvalidConfigException
  */
 public function repeatAfter($try, $period)
 {
     $try_message_id = !empty($this->try_message_id) ? $this->try_message_id : $this->id;
     $count = static::find()->byTryMessageId($try_message_id)->count();
     if ($count >= $try) {
         $this->addError('try_message_id', \Yii::t('cookyii.postman', 'Limit exceeded retries'));
     } else {
         $Message = new static();
         $Message->setAttributes(['subject' => $this->subject, 'content_text' => $this->content_text, 'content_html' => $this->content_html, 'address' => $this->address, 'try_message_id' => !empty($this->try_message_id) ? $this->try_message_id : $this->id, 'scheduled_at' => strtotime($period)]);
         $Message->validate() && $Message->save();
         if (!$Message->hasErrors()) {
             /** @var SendMailJob $Job */
             $Job = \Yii::createObject(SendMailJob::class, ['postmanMessageId' => $Message->id]);
             if (\Yii::$app->has($Job->queue)) {
                 $Job->push();
             }
         } else {
             $this->populateErrors($Message, 'id');
         }
     }
     return !$this->hasErrors();
 }
Example #24
0
 /**
  * Создание объекта для текущей ситуации
  *
  * @return static
  */
 public static function createCurrent()
 {
     $object = new static(['user_id' => \Yii::$app->user->id]);
     $object->setAttributes((array) \Yii::$app->session->get("sx-v3toys-order"));
     if ($object->user) {
         if (!$object->email) {
             $object->email = $object->user->email;
         }
         if (!$object->name) {
             $object->name = $object->user->name;
         }
         if (!$object->phone) {
             $object->phone = $object->user->phone;
         }
     }
     $products = [];
     if (\Yii::$app->shop->shopFuser->shopBaskets) {
         foreach (\Yii::$app->shop->shopFuser->shopBaskets as $shopBasket) {
             if ($shopBasket->product) {
                 $products[] = ['v3toys_product_id' => (int) $shopBasket->product->cmsContentElement->relatedPropertiesModel->getAttribute(\Yii::$app->v3toysSettings->v3toysIdPropertyName), 'price' => $shopBasket->price, 'quantity' => $shopBasket->quantity, 'name' => (string) $shopBasket->product->cmsContentElement->name, 'product_id' => (int) $shopBasket->product->cmsContentElement->id];
             }
         }
     }
     //Наполнение заказа текущими продуктами из корзины
     $object->products = $products;
     if (\Yii::$app->dadataSuggest->address) {
         $object->dadata_address = \Yii::$app->dadataSuggest->address->toArray();
     }
     $object->discount = \Yii::$app->shop->shopFuser->moneyDiscount->getValue();
     if ($object->dadataAddress) {
         //print_r(\Yii::$app->dadataSuggest->address->toArray());die;
         //$object->post_region = ArrayHelper::getValue(\Yii::$app->dadataSuggest->address->data, 'region');
         if (ArrayHelper::getValue($object->dadataAddress->data, 'city')) {
             //$object->post_city = ArrayHelper::getValue(\Yii::$app->dadataSuggest->address->data, 'city');
         } else {
             if (ArrayHelper::getValue($object->dadataAddress->data, 'settlement')) {
                 //$object->post_city = ArrayHelper::getValue(\Yii::$app->dadataSuggest->address->data, 'settlement');
             }
         }
         //$object->post_area = ArrayHelper::getValue(\Yii::$app->dadataSuggest->address->data, 'area');
         $object->post_index = ArrayHelper::getValue($object->dadataAddress->data, 'postal_code');
         $object->post_address = $object->dadataAddress->shortAddressString;
         $object->courier_address = $object->dadataAddress->shortAddressString;
     }
     return $object;
 }
Example #25
0
 /**
  * Creates a new user.
  *
  * @param  array $attributes the attributes given by field => value.
  * @param int    $defaultStatus
  * @param bool   $generateToken
  * @return static the newly created model, or null on failure.
  */
 public static function create($attributes, $defaultStatus = self::STATUS_NOT_ACTIVE, $generateToken = true)
 {
     /** @var Users $user */
     $user = new static();
     $user->setScenario(self::S_REGISTRATION);
     $user->setAttributes($attributes);
     $user->setPassword($attributes['password']);
     $user->setHash(['username', 'email']);
     if ($generateToken === true) {
         $user->generateToken();
     }
     $user->setStatus($defaultStatus);
     if ($user->save()) {
         return $user;
     }
     return null;
 }
Example #26
0
 /**
  * @param string[] $attributes
  *
  * @return static
  */
 public static function make($attributes = [])
 {
     $tag = new static();
     $tag->setAttributes($attributes);
     return $tag;
 }
Example #27
0
 /**
  * Static factory method to create a child object
  *
  * @param  array $c
  * @throws Exception
  * @return Child
  */
 public static function factory(array $c)
 {
     if (!isset($c['nodeName'])) {
         throw new Exception('Error: At least the \'nodeName\' must be set within the child configuration array.');
     }
     $nodeName = $c['nodeName'];
     $nodeValue = isset($c['nodeValue']) ? $c['nodeValue'] : null;
     $childFirst = isset($c['childrenFirst']) ? $c['childrenFirst'] : false;
     $indent = isset($c['indent']) ? $c['indent'] : null;
     $child = new static($nodeName, $nodeValue, null, $childFirst, $indent);
     if (isset($c['attributes'])) {
         $child->setAttributes($c['attributes']);
     }
     if (isset($c['childNodes'])) {
         $child->addChildren($c['childNodes']);
     }
     return $child;
 }
Example #28
0
 /**
  * Duplicates the current activerecord to a new one.
  *
  * Instead of cloning it will create a new instance of the called class
  * Copy all the attributes from the original and overwrite the one in the $attibutes parameter
  * Unset the primary key if it's not multicolumn and assumably auto_increment
  *
  * @param array $attributes Array of attributes that need to be set in
  * the newly created activerecord as KEY => VALUE.
  * Like: $params = array('attribute1'=>1,'attribute2'=>'Hello');
  * @param boolean $save if the copy should be save when calling this function
  * @param boolean $ignoreAclPermissions
  * @return mixed The newly created object or false if before or after duplicate fails
  *
  */
 public function duplicate($attributes = array(), $save = true, $ignoreAclPermissions = false)
 {
     $copy = new static();
     $copiedAttrs = $this->getAttributes('raw');
     unset($copiedAttrs['ctime'], $copiedAttrs['files_folder_id']);
     $pkField = $this->primaryKey();
     if (!is_array($pkField)) {
         unset($copiedAttrs[$pkField]);
     }
     $copiedAttrs = array_merge($copiedAttrs, $attributes);
     $copy->setAttributes($copiedAttrs, false);
     if (!$this->beforeDuplicate($copy)) {
         return false;
     }
     foreach ($attributes as $key => $value) {
         $copy->{$key} = $value;
     }
     //Generate new acl for this model
     if ($this->aclField() && !$this->isJoinedAclField) {
         $user_id = isset($this->user_id) ? $this->user_id : GO::user()->id;
         $copy->setNewAcl($user_id);
     }
     if ($this->customFieldsRecord) {
         $cfAtt = $this->customFieldsRecord->getAttributes('raw');
         unset($cfAtt['model_id']);
         $copy->customFieldsRecord->setAttributes($cfAtt, false);
     }
     $this->_duplicateFileColumns($copy);
     if ($save) {
         if (!$copy->save($ignoreAclPermissions)) {
             throw new \Exception("Could not save duplicate: " . implode("\n", $copy->getValidationErrors()));
         }
     }
     if (!$this->afterDuplicate($copy)) {
         $copy->delete(true);
         return false;
     }
     return $copy;
 }
Example #29
0
 /**
  * Convert XML Elements to IodefElement Objects
  * @param  SabreReader $reader [description]
  * @return IodefElement
  */
 public static function xmlDeserialize(SabreReader $reader)
 {
     $IodefElement = new static();
     $IodefElement->setAttributes($reader->parseAttributes());
     $innerData = $reader->parseInnerTree();
     // parseInnerTree will return an array or string.
     if (is_array($innerData)) {
         foreach ($innerData as $child) {
             // This should always and only by of type 'object'.
             if (gettype($child['value']) == 'object') {
                 $className = $child['value']->getShortName();
                 if (substr($IodefElement->elements[$className], -6) == '_MULTI') {
                     $IodefElement->{$className}[] = $child['value'];
                 } else {
                     $IodefElement->{$className} = $child['value'];
                 }
             }
         }
     } else {
         if (!empty($innerData)) {
             $IodefElement->value = $innerData;
         }
     }
     return $IodefElement;
 }
Example #30
0
 /**
  * Builds model from array.
  * @param array $attributes Model attributes.
  * @param array &$aliases Aliases.
  * @internal
  * @return static
  */
 public static function buildFromArray(array $attributes, array &$aliases = null)
 {
     /** @var Model $model */
     $model = new static();
     return $model->setAttributes($attributes, $aliases);
 }