Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved main objects hierarchy, fixed #6: Separate models and attrib…
…utes for export
- Loading branch information
Showing
18 changed files
with
694 additions
and
375 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace arogachev\excel\components; | ||
|
||
use yii\base\Component; | ||
|
||
/** | ||
* @property StandardAttribute $standardAttribute | ||
* @property mixed $value | ||
*/ | ||
class Attribute extends Component | ||
{ | ||
/** | ||
* @var Model | ||
*/ | ||
protected $_model; | ||
|
||
/** | ||
* @var StandardAttribute | ||
*/ | ||
protected $_standardAttribute; | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
protected $_value; | ||
|
||
|
||
/** | ||
* @param Model $value | ||
*/ | ||
public function setModel($value) | ||
{ | ||
$this->_model = $value; | ||
} | ||
|
||
/** | ||
* @return StandardAttribute | ||
*/ | ||
public function getStandardAttribute() | ||
{ | ||
return $this->_standardAttribute; | ||
} | ||
|
||
/** | ||
* @param StandardAttribute $value | ||
*/ | ||
public function setStandardAttribute($value) | ||
{ | ||
$this->_standardAttribute = $value; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getValue() | ||
{ | ||
return $this->_value; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
*/ | ||
public function setValue($value) | ||
{ | ||
$this->_value = $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace arogachev\excel\components; | ||
|
||
use yii\base\Component; | ||
|
||
/** | ||
* @property StandardModel $standardModel | ||
* @property \yii\db\ActiveRecord $instance | ||
*/ | ||
abstract class Model extends Component | ||
{ | ||
/** | ||
* @var StandardModel | ||
*/ | ||
protected $_standardModel; | ||
|
||
/** | ||
* @var Attribute[] | ||
*/ | ||
protected $_attributes = []; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected static $attributeClassName = 'arogachev\excel\components\Attribute'; | ||
|
||
/** | ||
* @var \yii\db\ActiveRecord | ||
*/ | ||
protected $_instance; | ||
|
||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function init() | ||
{ | ||
$this->initAttributes(); | ||
} | ||
|
||
abstract protected function initAttributes(); | ||
|
||
/** | ||
* @param array $config | ||
*/ | ||
protected function initAttribute($config) | ||
{ | ||
$className = static::$attributeClassName; | ||
$extendedConfig = array_merge($config, ['model' => $this]); | ||
$this->_attributes[] = new $className($extendedConfig); | ||
} | ||
|
||
/** | ||
* @return StandardModel | ||
*/ | ||
public function getStandardModel() | ||
{ | ||
return $this->_standardModel; | ||
} | ||
|
||
/** | ||
* @param StandardModel $value | ||
*/ | ||
public function setStandardModel($value) | ||
{ | ||
$this->_standardModel = $value; | ||
} | ||
|
||
/** | ||
* @return \yii\db\ActiveRecord | ||
*/ | ||
public function getInstance() | ||
{ | ||
return $this->_instance; | ||
} | ||
|
||
/** | ||
* @param \yii\db\ActiveRecord $value | ||
*/ | ||
public function setInstance($value) | ||
{ | ||
$this->_instance = $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
namespace arogachev\excel\components; | ||
|
||
use arogachev\excel\exceptions\StandardAttributeException; | ||
use yii\base\Object; | ||
|
||
/** | ||
* @property StandardModel $standardModel | ||
* @property string $column | ||
*/ | ||
class StandardAttribute extends Object | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $name; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $label; | ||
|
||
/** | ||
* @var array|callable | ||
*/ | ||
public $valueReplacement; | ||
|
||
/** | ||
* @var StandardModel | ||
*/ | ||
protected $_standardModel; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $_column; | ||
|
||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function init() | ||
{ | ||
$this->validateName(); | ||
|
||
if ($this->_standardModel->extendStandardAttributes && !$this->label) { | ||
$this->label = $this->_standardModel->instance->getAttributeLabel($this->name); | ||
} | ||
|
||
$this->validateLabel(); | ||
$this->validateValueReplacement(); | ||
} | ||
|
||
/** | ||
* @throws StandardAttributeException | ||
*/ | ||
protected function validateName() | ||
{ | ||
if (!$this->name) { | ||
throw new StandardAttributeException($this, 'Name is required.'); | ||
} | ||
} | ||
|
||
/** | ||
* @throws StandardAttributeException | ||
*/ | ||
protected function validateLabel() | ||
{ | ||
if ($this->standardModel->useAttributeLabels && !$this->label) { | ||
throw new StandardAttributeException($this, 'Label not specified.'); | ||
} | ||
} | ||
|
||
/** | ||
* @throws StandardAttributeException | ||
*/ | ||
protected function validateValueReplacement() | ||
{ | ||
if (!$this->valueReplacement || !is_array($this->valueReplacement)) { | ||
return; | ||
} | ||
|
||
if ($this->valueReplacement != array_unique($this->valueReplacement)) { | ||
throw new StandardAttributeException($this, 'Value replacement list contains duplicate labels / values.'); | ||
} | ||
} | ||
|
||
/** | ||
* @return StandardModel | ||
*/ | ||
public function getStandardModel() | ||
{ | ||
return $this->_standardModel; | ||
} | ||
|
||
/** | ||
* @param StandardModel $value | ||
*/ | ||
public function setStandardModel($value) | ||
{ | ||
$this->_standardModel = $value; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getColumn() | ||
{ | ||
return $this->_column; | ||
} | ||
|
||
/** | ||
* @param string $value | ||
*/ | ||
public function setColumn($value) | ||
{ | ||
$this->_column = $value; | ||
} | ||
} |
Oops, something went wrong.