Skip to content

Commit

Permalink
Add new files.
Browse files Browse the repository at this point in the history
  • Loading branch information
JASchilz committed Oct 3, 2016
1 parent 1966b7f commit e610c20
Show file tree
Hide file tree
Showing 3 changed files with 804 additions and 0 deletions.
155 changes: 155 additions & 0 deletions src/orm-wrapper/PropelCollectionWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace Athens\Propel\ORMWrapper;

use Propel\Runtime\Collection\ObjectCollection;

use Propel\Runtime\Collection\CollectionIterator;

use Athens\Core\ORMWrapper\CollectionWrapperInterface;

/**
* Class PropelCollectionWrapper
*
* @package Athens\Propel\ORMWrapper
*/
class PropelCollectionWrapper implements CollectionWrapperInterface
{
/** @var ObjectCollection */
protected $collection;

/** @var CollectionIterator */
protected $iterator;

/**
* PropelCollectionWrapper constructor.
* @param ObjectCollection $collection
*/
public function __construct(ObjectCollection $collection)
{
$this->collection = $collection;
$this->iterator = new CollectionIterator($collection);
}

/**
* Move the iterator index to the next item.
* @return void
*/
public function next()
{
$this->iterator->next();
}

/**
* Find whether the current iterator index is valid.
*
* @return boolean
*/
public function valid()
{
return $this->iterator->valid();
}

/**
* Move the iterator index back to its initial position.
*
* @return void
*/
public function rewind()
{
$this->iterator->rewind();
}

/**
* Return the current array key.
*
* @return mixed
*/
public function key()
{
return $this->iterator->key();
}

/**
* Get the item at the current iterator index.
*
* @return PropelObjectWrapper
*/
public function current()
{
return new PropelObjectWrapper($this->iterator->current());
}

/**
* Set the element at the given offset to the given value.
*
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet($offset, $value)
{
$this->collection->offsetSet($offset, $value);
}

/**
* Retrieve the element at the given array offset.
*
* @param mixed $offset
* @return mixed
*/
public function offsetGet($offset)
{
return $this->collection->offsetGet($offset);
}

/**
* Unset the element at the given array offset.
*
* @param mixed $offset
* @return void
*/
public function offsetUnset($offset)
{
$this->collection->offsetUnset($offset);
}

/**
* Find whether the array has an element set at the given offset.
*
* @param mixed $offset
* @return boolean
*/
public function offsetExists($offset)
{
return $this->collection->offsetExists($offset);
}

/**
* Count the number of elements.
*
* @return integer
*/
public function count()
{
return $this->collection->count();
}

/**
* Save all of the elements in the collection to the database.
* @return void
*/
public function save()
{
$this->collection->save();
}

/**
* Delete all of the elements in the collection from the database.
* @return void
*/
public function delete()
{
$this->collection->delete();
}
}
189 changes: 189 additions & 0 deletions src/orm-wrapper/PropelORMWrapperTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php

namespace Athens\Propel\ORMWrapper;

use Propel\Runtime\Map\ColumnMap;
use Propel\Runtime\Map\TableMap;

trait PropelORMWrapperTrait
{

/** @var string[] */
protected $keys;

/** @var ColumnMap[] */
protected $columns;

/** @var TableMap */
protected $tableMap;

/**
* @param TableMap $tableMap
* @return void
*/
protected function setTableMap(TableMap $tableMap)
{
$this->tableMap = $tableMap;
}

/**
* @return TableMap
*/
protected function getTableMap()
{
return $this->tableMap;
}

/**
* @return string
*/
public function getTitleCasedObjectName()
{
$tableMap = $this->getTableMap();

return ucwords(str_replace('_', ' ', $tableMap::TABLE_NAME));
}

/**
* @return string[]
*/
public function getUnqualifiedTitleCasedColumnNames()
{
$labels = [];
foreach ($this->getColumns() as $column) {
$label = $column->getName();
$labels[] = ucwords(str_replace('_', ' ', $label));
}
return array_combine(
$this->getKeys(),
$labels
);
}

/**
* @return ColumnMap[]
*/
protected function getColumns()
{
if ($this->columns === null) {
$columns = [];

foreach ($this->findParentTables() as $parentTable) {
$columns = array_merge($columns, $parentTable->getColumns());
}

$columns = array_merge($columns, $this->getTableMap()->getColumns());

$objectName = $this->getPascalCasedObjectName();
$keys = [];
foreach ($columns as $column) {
$keys[] = $objectName . '.' . $column->getPhpName();
}

$this->columns = array_combine($keys, $columns);
}

return $this->columns;
}

/**
* @return string[]
*/
protected function getKeys()
{
if ($this->keys === null) {
$objectName = $this->getPascalCasedObjectName();

$this->keys = [];
foreach ($this->getColumnPhpNames() as $columnPhpName) {
$this->keys[] = $objectName . '.' . $columnPhpName;
}
}

return $this->keys;
}

/**
* @return string[]
*/
protected function getColumnPhpNames()
{
$columnPhpNames = [];
foreach ($this->getColumns() as $column) {
$columnPhpNames[] = $column->getPhpName();
}

return $columnPhpNames;
}

/**
* @return string[]
*/
public function getQualifiedPascalCasedColumnNames()
{
return array_combine(
$this->getKeys(),
$this->getKeys()
);
}

/**
* Return a Propel TableMap corresponding to a table within the same schema as
* $fullyQualifiedTableMapName.
*
* In some cases, a foreign table map within the same database as $this may not be initialized
* by Propel. If we try to access a foreign table map using runtime introspection and it has
* not yet been initialized, then Propel will throw a TableNotFoundException. This method
* accesses the table map by access to its fully qualified class name, which it determines by
* modifying $this->_classTableMapName.
*
* @param string $tableName The SQL name of the related table for which you
* would like to retrieve a table map.
*
* @return \Propel\Runtime\Map\TableMap
*/
protected function getRelatedTableMap($tableName)
{
$fullyQualifiedTableMapName = get_class($this->getTableMap());
$upperCamelCaseTableName = str_replace('_', '', ucwords($tableName, '_')) . "TableMap";

// We build he fully qualified name of the related table map class
// by doing some complicated search and replace on the fully qualified
// table map name of the child.
$fullyQualifiedRelatedTableName = substr_replace(
$fullyQualifiedTableMapName,
$upperCamelCaseTableName,
strrpos(
$fullyQualifiedTableMapName,
"\\",
-1
) + 1
) . "\n";
$fullyQualifiedParentTableName = trim($fullyQualifiedRelatedTableName);

return $fullyQualifiedParentTableName::getTableMap();
}

/**
* @return \Propel\Runtime\Map\TableMap[]
*/
protected function findParentTables()
{
// Make recursive
$behaviors = $this->getTableMap()->getBehaviors();

$parentTables = [];
if (array_key_exists("delegate", $behaviors) === true) {
$parentTables[] = $this->getRelatedTableMap($behaviors["delegate"]["to"], $this->getTableMap());
}
return $parentTables;
}

/**
* @return string
*/
public function getPascalCasedObjectName()
{
return $this->getTableMap()->getPhpName();
}
}
Loading

0 comments on commit e610c20

Please sign in to comment.