Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyuexrow committed Mar 6, 2017
0 parents commit 37b95c0
Show file tree
Hide file tree
Showing 14 changed files with 1,148 additions and 0 deletions.
44 changes: 44 additions & 0 deletions DependencyInjection/xrowMatrixExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* This file is part of the EzMatrixBundle package
*
* See README.md file distributed with this source code for further information.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @author For list of contributors see link in composer.json file distributed with this source code.
*/

namespace xrow\MatrixBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Yaml\Yaml;

/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class xrowMatrixExtension extends Extension implements PrependExtensionInterface
{
/**
* {@inheritDoc}
*/
public function load( array $configs, ContainerBuilder $container )
{
$loader = new Loader\YamlFileLoader( $container, new FileLocator( __DIR__ . '/../Resources/config' ) );
$loader->load( 'services.yml' );
}

public function prepend( ContainerBuilder $container )
{
$config = Yaml::parse( __DIR__ . '/../Resources/config/field_templates.yml' );

$container->prependExtensionConfig( 'ezpublish', $config );

}
}
59 changes: 59 additions & 0 deletions FieldType/Matrix/Column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* This file is part of the EzMatrixBundle package
*
* See README.md file distributed with this source code for further information.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @author For list of contributors see link in composer.json file distributed with this source code.
*/

namespace xrow\MatrixBundle\FieldType\Matrix;

use eZ\Publish\SPI\Persistence\ValueObject;

/**
* Class Column
* Column represents a single configured Column for a Matrix
*
* @package xrow\MatrixBundle\FieldType\Matrix
*/
class Column extends ValueObject
{
/**
* Identifier value of the column
* @var string
*/
public $id;

/**
* User-friendly name for the column
* @var string
*/
public $name;

/**
* Integer position of the column
* @var int
*/
public $num;

public function __toString()
{
return (string)$this->name;
}

/**
* Convert the column to an array compatible with the Matrix hash format
* @return array
*/
public function toArray()
{
return array(
'id' => $this->id,
'name' => $this->name,
'num' => $this->num
);
}
}
133 changes: 133 additions & 0 deletions FieldType/Matrix/ColumnCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
/**
* This file is part of the EzMatrixBundle package
*
* See README.md file distributed with this source code for further information.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @author For list of contributors see link in composer.json file distributed with this source code.
*/

namespace xrow\MatrixBundle\FieldType\Matrix;

use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;

/**
* Class ColumnCollection
* Collection of Column values for Matrix
*
* @package xrow\MatrixBundle\FieldType\Matrix
*/
class ColumnCollection extends \ArrayObject
{

/**
* @param \xrow\MatrixBundle\FieldType\Matrix\Column[] $elements
*/
public function __construct( array $elements = array() )
{
// Call parent constructor without $elements because all column elements
// must be given an id by $this->offsetSet()
parent::__construct();
foreach ( $elements as $i => $author )
{
if ( is_array( $author ) )
{
$author = new Column( $author );
}
$this->offsetSet( $i, $author );
}
}

/**
* Create a ColumnCollection from an array of field names.
* The ids will be auto-generated based on the names.
* @param array $names
*
* @return ColumnCollection
*/
public static function createFromNames( array $names = array() )
{
$columns = array();
$i = 1;

foreach ( $names as $name )
{
$id = strtolower(
preg_replace(
array( '/\s+/', '/[^a-z0-9_]/i' ),
array( '_', '' ),
$name
)
);

$columns[] = new Column( array( 'id' => $id, 'name' => $name, 'num' => $i ) );
$i++;
}

return new self( $columns );
}

/**
* Adds a new author to the collection
*
* @throws InvalidArgumentType When $value is not of type Column
* @param int $offset
* @param \xrow\MatrixBundle\FieldType\Matrix\Column $value
*/
public function offsetSet( $offset, $value )
{
if ( !$value instanceof Column )
{
throw new InvalidArgumentType(
'$value',
'xrow\\MatrixBundle\\FieldType\\Matrix\\Column',
$value
);
}

$aColumns = $this->getArrayCopy();
parent::offsetSet( $offset, $value );
if ( !isset( $value->id ) || $value->id == -1 )
{
if ( !empty( $aColumns ) )
{
$value->id = end( $aColumns )->id + 1;
}
else
{
$value->id = 1;
}
}
}

/*
* Returns a comma-separated list of the column names.
* @return string
*/
public function getColumnNames()
{
return implode( ', ', $this->getArrayCopy() );
}

public function __toString()
{
return implode( "\t", $this->getArrayCopy() );
}

/**
* Returns an array compatible with the 'columns' portion of the Matrix hash format.
* @return array
*/
public function toArray()
{
$columns = array();
foreach ( $this->getArrayCopy() as $column )
{
$columns[] = $column->toArray();
}

return $columns;
}
}
27 changes: 27 additions & 0 deletions FieldType/Matrix/Matrix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* This file is part of the EzMatrixBundle package
*
* See README.md file distributed with this source code for further information.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @author For list of contributors see link in composer.json file distributed with this source code.
*/

namespace xrow\MatrixBundle\FieldType\Matrix;

use eZ\Publish\SPI\Persistence\ValueObject;

/**
* Class Matrix
* Value object for the Matrix FieldType
*
* @package xrow\MatrixBundle\FieldType\Matrix
*/
class Matrix extends ValueObject
{
public $id;
public $columns;
public $rows;
}
59 changes: 59 additions & 0 deletions FieldType/Matrix/Row.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* This file is part of the EzMatrixBundle package
*
* See README.md file distributed with this source code for further information.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @author For list of contributors see link in composer.json file distributed with this source code.
*/

namespace xrow\MatrixBundle\FieldType\Matrix;

use eZ\Publish\SPI\Persistence\ValueObject;

/**
* Class Row
* Represents a Row in a Matrix
*
* @package xrow\MatrixBundle\FieldType\Matrix
*/
class Row extends ValueObject
{

/**
* Row's index within the row collection.
*
* @var int
*/
public $id;

/**
* Associative array of column values
*
* @var array
*/
public $columns;

public function __construct( array $columns )
{
$this->columns = $columns;
}

/**
* @return string
*/
public function __toString()
{
return implode( "\t", $this->columns );
}

/**
* @return array
*/
public function toArray()
{
return $this->columns;
}
}
Loading

0 comments on commit 37b95c0

Please sign in to comment.