protected static function initAttributeSet() { $className = get_called_class(); if (!Attributes::attributesSetFor($className)) { Attributes::setClassAttributes($className, static::attributeSet()); } }
protected function typeCastForSet($attrName, $value) { switch ($this->getAttribute($attrName)->type()) { case 'datetime': if ($value instanceof MongoDate) { list($u, $s) = explode(" ", (string) $value); $u = (double) $u; $s = (int) $s; return Carbon::createFromFormat('U.u', sprintf('%.f', $s + $u)); } break; case 'mongoId': return $value; } return parent::typeCastForSet($attrName, $value); }
/** * @see initAttrsDirtyModel() * @see getAttributesClass() */ protected function initializeAttributes(array $attributes) { $className = get_called_class(); /** * Check if attributes are set for this class. Set them if not. */ if (!Attributes::attributesSetFor($className)) { $className::initAttributeSet(); } $attrsClass = $this->getAttributesClass(); $this->attributes = new $attrsClass($className, $this->defaultAttributes()); if ($attributes) { $this->setAndFilterProperties($attributes); $this->getAttributes()->set($attributes, null, $this->initAttrsDirtyModel()); } }
protected function fillTimeAttribute($attrName) { $attributes = Attributes::getAttributesFor(get_called_class()); if (!($attribute = $attributes->getAttribute($attrName))) { return false; } switch ($attribute->type()) { case 'datetime': case 'timestamp': $value = date('Y-m-d H:i:s'); break; case 'date': $value = date('Y-m-d'); break; case 'time': $value = date('H:i:s'); break; case 'year': $value = date('Y'); break; default: return false; } $this->setAttribute($attrName, $value); return true; }
protected function getPropertyGetter($modelClass, $propName) { if (Attributes::isClassAttribute($modelClass, $propName)) { return function ($model) use($propName) { return $model->getAttribute($propName); }; } else { $property = AccessibleProperties::getProperty($modelClass, $propName); if ($property === true) { return function ($model) use($propName) { $model->{$propName}; }; } elseif ($property[0]) { return function ($model) use($propName) { $model->{$propName}(); }; } } throw new Exception\RuntimeException(sprintf("Unknown attribute or property %s::%s", $modelClass, $propName)); }