Skip to content

Commit

Permalink
Merge pull request #20 from williamespindola/fix/extension-field-depe…
Browse files Browse the repository at this point in the history
…ndency-methods

Fix/extension field dependency methods
  • Loading branch information
williamespindola committed Sep 18, 2015
2 parents 4f65171 + 695f146 commit cf6e3f2
Show file tree
Hide file tree
Showing 26 changed files with 530 additions and 119 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
build
composer.lock
docs
vendor
vendor
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

<entity name="WilliamEspindola\Field\Entity\Collection">
<entity name="WilliamEspindola\Field\Entity\Collection" table="collection">
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

<entity name="WilliamEspindola\Field\Entity\CollectionField">
<entity name="WilliamEspindola\Field\Entity\CollectionField" table="collectionfield">
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>
Expand Down
2 changes: 1 addition & 1 deletion config/xml/WilliamEspindola.Field.Entity.Field.dcm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

<entity name="WilliamEspindola\Field\Entity\Field">
<entity name="WilliamEspindola\Field\Entity\Field" table="field">
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

<entity name="option">
<entity name="WilliamEspindola\Field\Entity\Options" table="options">
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>

<field name="option" type="string" />
<field name="value" type="string" />

<many-to-one target-entity="WilliamEspindola\Field\Entity\Field" field="field" />
</entity>
Expand Down
47 changes: 0 additions & 47 deletions data/mysql-schema.sql

This file was deleted.

Binary file removed data/schema-0.3.mwb
Binary file not shown.
Binary file removed data/schema-0.3.mwb.bak
Binary file not shown.
4 changes: 2 additions & 2 deletions data/schema-0.3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`collection_field`
-- Table `mydb`.`collectionfield`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`collection_field` (
CREATE TABLE IF NOT EXISTS `mydb`.`collectionfield` (
`collection_id` INT NOT NULL,
`field_id` INT NOT NULL,
`id` INT NOT NULL AUTO_INCREMENT,
Expand Down
26 changes: 13 additions & 13 deletions src/Entity/Option.php → src/Entity/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace WilliamEspindola\Field\Entity;

class Option implements EntityInterface
class Options implements EntityInterface
{
/**
* @var integer
Expand All @@ -12,27 +12,27 @@ class Option implements EntityInterface
/**
* @var text
*/
private $option;
private $value;

/**
* $var object WilliamEspindola\Field\Entity\Field
*/
private $field_id;
private $field;

/**
* @param mixed $field_id
* @param mixed $field
*/
public function setFieldId($field_id)
public function setField($field)
{
$this->field_id = $field_id;
$this->field = $field;
}

/**
* @return mixed
*/
public function getFieldId()
public function getField()
{
return $this->field_id;
return $this->field;
}

/**
Expand All @@ -52,18 +52,18 @@ public function getId()
}

/**
* @param text $option
* @param text $value
*/
public function setOption($option)
public function setValue($value)
{
$this->option = $option;
$this->value = $value;
}

/**
* @return text
*/
public function getOption()
public function getValue()
{
return $this->option;
return $this->value;
}
}
48 changes: 21 additions & 27 deletions src/Extension/FieldTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,37 @@
use Twig_Extension;
use Twig_Function_Method;
use WilliamEspindola\Field\Repository\FieldRepository;
use WilliamEspindola\Field\Repository\OptionRepository;
use WilliamEspindola\Field\Service\DoctrineFieldService;
use WilliamEspindola\Field\Service\RespectFieldService;
use Respect\Relational\Mapper;

/**
* Class FieldTwigExtension
* @package WilliamEspindola\Field\Extension
*/
class FieldTwigExtension extends Twig_Extension
{
/**
* @var \WilliamEspindola\Field\Repository\FieldRepository
* @var
*/
protected $fieldRepository;
protected $fieldService;

/**
* @var \WilliamEspindola\Field\Repository\OptionRepository
* @var
*/
protected $optionService;

/**
* @param FieldRepository $fieldRepository
*/
public function __construct(
FieldRepository $fieldRepository,
OptionRepository $optionRepository
FieldRepository $fieldRepository
) {
$this->fieldRepository = $fieldRepository;
$this->optionRepository = $optionRepository;
if ($fieldRepository->getStorage()->getMapper() instanceof Mapper) {
$this->fieldService = new RespectFieldService($fieldRepository);
} else {
$this->fieldService = new DoctrineFieldService($fieldRepository);
}
}

/**
Expand All @@ -45,22 +56,7 @@ public function getFunctions()
*/
public function getField($name)
{
return $this->fieldRepository->findOne(['name' => $name]);
}

/**
* @param $name Name of Field
* @return array Field Object with your options
*/
public function getOptionsOfField($name)
{
$field = $this->fieldRepository->findOne(['name' => $name]);
$field->options = $this->optionRepository->findBy(
['field_id' => $field->getId()],
Sql::orderBy('id')
);

return $field;
return $this->fieldService->findOneByName($name);
}

/**
Expand All @@ -69,9 +65,7 @@ public function getOptionsOfField($name)
*/
public function getFieldValue($name)
{
$field = $this->fieldRepository->findOne(['name' => $name]);

return $field->getValue();
return $this->fieldService->findOneByNameAndGetValue($name);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Repository/OptionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class OptionRepository
{
public function __construct(StorageORMInterface $storage)
{
$this->setStorage($storage, 'WilliamEspindola\Field\Entity\Option');
$this->setStorage($storage, 'WilliamEspindola\Field\Entity\Options');
}
}
56 changes: 56 additions & 0 deletions src/Service/DoctrineFieldService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace WilliamEspindola\Field\Service;

use WilliamEspindola\Field\Repository\RepositoryInterface;

/**
* Class DoctrineFieldService
* @package WilliamEspindola\Field\Service
*/
class DoctrineFieldService
{
/**
* @var \WilliamEspindola\Field\Repository\RepositoryInterface
*/
protected $repository;

/**
* @param RepositoryInterface $repository
*/
public function __construct(RepositoryInterface $repository)
{
$this->repository = $repository;
}

/**
* @param String $name
* @return Object WilliamEspindola\Field\Entity\Field
*/
public function findOneByName($name)
{
$field = $this->repository->findBy(
['name' => $name],
[['name' => 'ASC'], null, null]
);

return $field ? $field[0] : false;
}

/**
* @param String $name
* @return String Value of field
*/
public function findOneByNameAndGetValue($name)
{
$fields = $this->repository->findBy(
['name' => $name],
[['name' => 'ASC'], null, null]
);

if (!$fields)
return;

return $fields[0]->getValue();
}
}
38 changes: 38 additions & 0 deletions src/Service/DoctrineOptionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace WilliamEspindola\Field\Service;

use WilliamEspindola\Field\Repository\RepositoryInterface;

/**
* Class DoctrineOptionService
* @package WilliamEspindola\Field\Service
*/
class DoctrineOptionService
{
/**
* @var RepositoryInterface
*/
protected $repository;

/**
* @param RepositoryInterface $repository
*/
public function __costruct(RepositoryInterface $repository)
{
$this->repository = $repository;
}

/**
* @param Object $field
* @param String $order
* @return ArrayObject Options of field
*/
public function getOptionsOfField($field, Array $order)
{
return $this->repository->findBy(
['field_id' => $field->getId()],
[$order, null, null]
);
}
}
Loading

0 comments on commit cf6e3f2

Please sign in to comment.