Enforce writer parts parent to be concrete writer

We used to type hint against IWriter for writer part parent, but this
was not respected in writer part who often coded against the implementation
instead of the interface. With this change we make it a requirement for
writer parts to use their respective writer implementation instead of an
interface. This give legitimate access to all `BaseWriter` methods amongst
other things.
This commit is contained in:
Adrien Crivelli 2017-04-21 14:46:17 +09:00
parent 3ee9cc5ce6
commit 870d8640f5
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
2 changed files with 37 additions and 26 deletions

View File

@ -24,6 +24,34 @@ namespace PhpOffice\PhpSpreadsheet\Writer\Ods;
* @copyright Copyright (c) 2006 - 2015 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet) * @copyright Copyright (c) 2006 - 2015 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/ */
abstract class WriterPart extends \PhpOffice\PhpSpreadsheet\Writer\Xlsx\WriterPart abstract class WriterPart
{ {
/**
* Parent Ods object.
*
* @var \PhpOffice\PhpSpreadsheet\Writer\Ods
*/
private $parentWriter;
/**
* Get Ods writer.
*
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*
* @return \PhpOffice\PhpSpreadsheet\Writer\Ods
*/
public function getParentWriter()
{
return $this->parentWriter;
}
/**
* Set parent Ods writer.
*
* @param \PhpOffice\PhpSpreadsheet\Writer\Ods $writer
*/
public function __construct(\PhpOffice\PhpSpreadsheet\Writer\Ods $writer)
{
$this->parentWriter = $writer;
}
} }

View File

@ -27,47 +27,30 @@ namespace PhpOffice\PhpSpreadsheet\Writer\Xlsx;
abstract class WriterPart abstract class WriterPart
{ {
/** /**
* Parent IWriter object. * Parent Xlsx object.
* *
* @var \PhpOffice\PhpSpreadsheet\Writer\IWriter * @var \PhpOffice\PhpSpreadsheet\Writer\Xlsx
*/ */
private $parentWriter; private $parentWriter;
/** /**
* Set parent IWriter object. * Get parent Xlsx object.
*
* @param \PhpOffice\PhpSpreadsheet\Writer\IWriter $pWriter
*
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
public function setParentWriter(\PhpOffice\PhpSpreadsheet\Writer\IWriter $pWriter)
{
$this->parentWriter = $pWriter;
}
/**
* Get parent IWriter object.
* *
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
* *
* @return \PhpOffice\PhpSpreadsheet\Writer\IWriter * @return \PhpOffice\PhpSpreadsheet\Writer\Xlsx
*/ */
public function getParentWriter() public function getParentWriter()
{ {
if (!is_null($this->parentWriter)) { return $this->parentWriter;
return $this->parentWriter;
}
throw new \PhpOffice\PhpSpreadsheet\Writer\Exception('No parent \\PhpOffice\\PhpSpreadsheet\\Writer\\IWriter assigned.');
} }
/** /**
* Set parent IWriter object. * Set parent Xlsx object.
* *
* @param \PhpOffice\PhpSpreadsheet\Writer\IWriter $pWriter * @param \PhpOffice\PhpSpreadsheet\Writer\Xlsx $pWriter
*
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/ */
public function __construct(\PhpOffice\PhpSpreadsheet\Writer\IWriter $pWriter) public function __construct(\PhpOffice\PhpSpreadsheet\Writer\Xlsx $pWriter)
{ {
$this->parentWriter = $pWriter; $this->parentWriter = $pWriter;
} }