Skip to content

Commit

Permalink
Improved main objects hierarchy, fixed #6: Separate models and attrib…
Browse files Browse the repository at this point in the history
…utes for export
  • Loading branch information
arogachev committed May 31, 2015
1 parent c0a525f commit b84f2eb
Show file tree
Hide file tree
Showing 18 changed files with 694 additions and 375 deletions.
68 changes: 68 additions & 0 deletions src/components/Attribute.php
@@ -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;
}
}
85 changes: 85 additions & 0 deletions src/components/Model.php
@@ -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;
}
}
120 changes: 120 additions & 0 deletions src/components/StandardAttribute.php
@@ -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;
}
}

0 comments on commit b84f2eb

Please sign in to comment.