commit 9762f9f74344134ea7ddd74fc26eeacae5a0a747
parent aa6b8e680e6c5995608537040fe4b29b1b8326f1
Author: Felicitus <felicitus@felicitus.org>
Date: Mon, 22 Jun 2015 20:22:12 +0200
Added validators for the Unit Entity, removed obsolete code
Diffstat:
6 files changed, 152 insertions(+), 250 deletions(-)
diff --git a/src/PartKeepr/UnitBundle/Controller/DefaultController.php b/src/PartKeepr/UnitBundle/Controller/DefaultController.php
@@ -1,85 +0,0 @@
-<?php
-
-namespace PartKeepr\UnitBundle\Controller;
-
-use FOS\RestBundle\Request\ParamFetcher;
-use PartKeepr\DoctrineReflectionBundle\Controller\DoctrineRESTQueryController;
-use Sensio\Bundle\FrameworkExtraBundle\Configuration as Routing;
-use JMS\Serializer\Annotation as JMS;
-use Nelmio\ApiDocBundle\Annotation\ApiDoc;
-use FOS\RestBundle\Controller\Annotations\View;
-use Symfony\Component\HttpFoundation\Request;
-
-class DefaultController extends DoctrineRESTQueryController
-{
- protected $targetEntity = "PartKeepr\\UnitBundle\\Entity\\Unit";
-
- /**
- * Retrieves units
- *
- * @Routing\Route("/unit", defaults={"method" = "get","_format" = "json"})
- * @Routing\Method({"GET"})
- * @ApiDoc(section="unit",output="array<PartKeepr\UnitBundle\Entity\Unit>")
- *
- * @View()
- *
- * {@inheritdoc}
- */
- public function listAction(ParamFetcher $paramFetcher)
- {
- return parent::listAction($paramFetcher);
- }
-
- /**
- * Retrieves a single unit
- *
- * @Routing\Route("/unit/{id}", defaults={"method" = "get","_format" = "json"})
- * @Routing\Method({"GET"})
- * @ApiDoc(section="unit",output="PartKeepr\UnitBundle\Entity\Unit")
- * @View()
- */
- public function getAction ($id) {
- return parent::getAction($id);
- }
-
- /**
- * Saves a single unit
- *
- * @Routing\Route("/unit/{id}", defaults={"_format" = "json"})
- * @Routing\Method({"PUT"})
- * @ApiDoc(section="unit",input="PartKeepr\UnitBundle\Entity\Unit",output="PartKeepr\UnitBundle\Entity\Unit")
- *
- * @View()
- */
- public function putAction(Request $request, $id)
- {
- return parent::putAction($request, $id);
- }
-
- /**
- * Creates a single unit
- *
- * @Routing\Route("/unit", defaults={"_format" = "json"})
- * @Routing\Method({"POST"})
- * @ApiDoc(section="unit",input="PartKeepr\UnitBundle\Entity\Unit",output="PartKeepr\UnitBundle\Entity\Unit")
- *
- * @View()
- */
- public function postAction(Request $request)
- {
- return parent::postAction($request);
- }
-
- /**
- * Deletes a single unit
- *
- * @Routing\Route("/unit/{id}", defaults={"_format" = "json"})
- * @Routing\Method({"DELETE"})
- * @ApiDoc(section="unit")
- *
- * @View()
- */
- public function deleteAction ($id) {
- return parent::deleteAction($id);
- }
-}-
\ No newline at end of file
diff --git a/src/PartKeepr/UnitBundle/DependencyInjection/Configuration.php b/src/PartKeepr/UnitBundle/DependencyInjection/Configuration.php
@@ -1,29 +0,0 @@
-<?php
-
-namespace PartKeepr\UnitBundle\DependencyInjection;
-
-use Symfony\Component\Config\Definition\Builder\TreeBuilder;
-use Symfony\Component\Config\Definition\ConfigurationInterface;
-
-/**
- * This is the class that validates and merges configuration from your app/config files
- *
- * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
- */
-class Configuration implements ConfigurationInterface
-{
- /**
- * {@inheritdoc}
- */
- public function getConfigTreeBuilder()
- {
- $treeBuilder = new TreeBuilder();
- $rootNode = $treeBuilder->root('part_keepr_unit');
-
- // Here you should define the parameters that are allowed to
- // configure your bundle. See the documentation linked above for
- // more information on that topic.
-
- return $treeBuilder;
- }
-}
diff --git a/src/PartKeepr/UnitBundle/DependencyInjection/PartKeeprUnitExtension.php b/src/PartKeepr/UnitBundle/DependencyInjection/PartKeeprUnitExtension.php
@@ -1,28 +0,0 @@
-<?php
-
-namespace PartKeepr\UnitBundle\DependencyInjection;
-
-use Symfony\Component\DependencyInjection\ContainerBuilder;
-use Symfony\Component\Config\FileLocator;
-use Symfony\Component\HttpKernel\DependencyInjection\Extension;
-use Symfony\Component\DependencyInjection\Loader;
-
-/**
- * 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 PartKeeprUnitExtension extends Extension
-{
- /**
- * {@inheritdoc}
- */
- public function load(array $configs, ContainerBuilder $container)
- {
- $configuration = new Configuration();
- $config = $this->processConfiguration($configuration, $configs);
-
- $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
- $loader->load('services.xml');
- }
-}
diff --git a/src/PartKeepr/UnitBundle/Entity/Unit.php b/src/PartKeepr/UnitBundle/Entity/Unit.php
@@ -2,121 +2,123 @@
namespace PartKeepr\UnitBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
-use PartKeepr\Util\BaseEntity,
- PartKeepr\SiPrefixBundle\Entity\SiPrefix,
- Doctrine\ORM\Mapping as ORM,
- PartKeepr\DoctrineReflectionBundle\Annotation\TargetService;
+use Doctrine\ORM\Mapping as ORM;
+use PartKeepr\DoctrineReflectionBundle\Annotation\TargetService;
+use PartKeepr\SiPrefixBundle\Entity\SiPrefix;
+use PartKeepr\Util\BaseEntity;
use Symfony\Component\Serializer\Annotation\Groups;
+use Symfony\Component\Validator\Constraints as Assert;
/**
- * This object represents an unit. Units can be: Volt, Hertz etc.
- *
+ * This object represents an unit. Units can be: Volt, Hertz etc.
+ *
* @ORM\Entity
* @TargetService(uri="/api/units")
**/
-class Unit {
- /**
- * @ORM\Id @ORM\Column(type="integer")
- * @ORM\GeneratedValue(strategy="AUTO")
- * @Groups({"default"})
- * @var integer
- */
- private $id;
+class Unit extends BaseEntity
+{
+ /**
+ * The name of the unit (e.g. Volts, Ampere, Farad, Metres)
+ * @ORM\Column(type="string")
+ * @Groups({"default"})
+ * @Assert\Type(type="string")
+ * @Assert\NotBlank(message="unit.name.not_blank")
+ *
+ * @var string
+ */
+ private $name;
- /**
- * Returns the ID of this object.
- *
- * @param none
- *
- * @return int The ID of this object
- */
- public function getId()
- {
- return $this->id;
- }
+ /**
+ * The symbol of the unit (e.g. V, A, F, m)
+ * @ORM\Column(type="string")
+ * @Groups({"default"})
+ * @Assert\Type(type="string")
+ * @Assert\NotBlank(message="unit.symbol.not_blank")
+ *
+ * @var string
+ */
+ private $symbol;
- public function setId ($id) {
- $this->id = $id;
- }
- /**
- * The name of the unit (e.g. Volts, Ampere, Farad, Metres)
- * @ORM\Column(type="string")
- *
- * @var string
- * @Groups({"default"})
- */
- private $name;
-
- /**
- * The symbol of the unit (e.g. V, A, F, m)
- * @ORM\Column(type="string")
- * @var string
- * @Groups({"default"})
- */
- private $symbol;
-
- /**
- * Defines the allowed SiPrefixes for this parameter unit
- * @ORM\ManyToMany(targetEntity="PartKeepr\SiPrefixBundle\Entity\SiPrefix")
- * @ORM\JoinTable(name="UnitSiPrefixes",
- * joinColumns={@ORM\JoinColumn(name="unit_id", referencedColumnName="id")},
- * inverseJoinColumns={@ORM\JoinColumn(name="siprefix_id", referencedColumnName="id")}
- * )
- * @Groups({"default"})
- *
- * @var ArrayCollection
- */
- public $prefixes;
-
- /**
- * Creates a new Unit.
- */
- public function __construct () {
- $this->prefixes = new \Doctrine\Common\Collections\ArrayCollection();
- }
-
- /**
- * Sets the name for this unit
- * @param string $name the name for this unit
- */
- public function setName ($name) {
- $this->name = $name;
- }
-
- /**
- * Returns the name for this unit
- * @return string The unit name
- */
- public function getName () {
- return $this->name;
- }
-
- /**
- * Sets the symbol for this unit
- * @param string $symbol The symbol
- */
- public function setSymbol ($symbol) {
- $this->symbol = $symbol;
- }
-
- /**
- * Returns the symbol for this unit
- * @return string The symbol
- */
- public function getSymbol () {
- return $this->symbol;
- }
+ /**
+ * Defines the allowed SiPrefixes for this parameter unit
+ * @ORM\ManyToMany(targetEntity="PartKeepr\SiPrefixBundle\Entity\SiPrefix")
+ * @ORM\JoinTable(name="UnitSiPrefixes",
+ * joinColumns={@ORM\JoinColumn(name="unit_id", referencedColumnName="id")},
+ * inverseJoinColumns={@ORM\JoinColumn(name="siprefix_id", referencedColumnName="id")}
+ * )
+ * @Groups({"default"})
+ * @Assert\All({
+ * @Assert\Type(type="PartKeepr\SiPrefixBundle\Entity\SiPrefix")
+ * })
+ * @var ArrayCollection
+ */
+ private $prefixes;
- /**
- * Returns the si-prefix list for this unit
- * @return array An array of SiPrefix objects
- */
- public function getPrefixes () {
- return $this->prefixes;
- }
+ /**
+ * Creates a new Unit.
+ */
+ public function __construct()
+ {
+ $this->prefixes = new ArrayCollection();
+ }
- public function setPrefixes ($array) {
- var_dump($array);
- $this->prefixes = $array;
- }
+ /**
+ * Sets the name for this unit
+ *
+ * @param string $name the name for this unit
+ */
+ public function setName($name)
+ {
+ $this->name = $name;
+ }
+
+ /**
+ * Returns the name for this unit
+ *
+ * @return string The unit name
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ /**
+ * Sets the symbol for this unit
+ *
+ * @param string $symbol The symbol
+ */
+ public function setSymbol($symbol)
+ {
+ $this->symbol = $symbol;
+ }
+
+ /**
+ * Returns the symbol for this unit
+ *
+ * @return string The symbol
+ */
+ public function getSymbol()
+ {
+ return $this->symbol;
+ }
+
+ /**
+ * Returns the si-prefix list for this unit
+ *
+ * @return array An array of SiPrefix objects
+ */
+ public function getPrefixes()
+ {
+ return $this->prefixes;
+ }
+
+ /**
+ * Sets the SiPrefixes
+ *
+ * @param $array
+ */
+ public function setPrefixes($array)
+ {
+ $this->prefixes = $array;
+ }
}
\ No newline at end of file
diff --git a/src/PartKeepr/UnitBundle/Resources/translations/validators.en.yml b/src/PartKeepr/UnitBundle/Resources/translations/validators.en.yml
@@ -0,0 +1,2 @@
+unit.name.not_blank: The unit name may not be blank.
+unit.symbol.not_blank: The unit symbol may not be blank.+
\ No newline at end of file
diff --git a/src/PartKeepr/UnitBundle/Tests/Model/UnitTest.php b/src/PartKeepr/UnitBundle/Tests/Model/UnitTest.php
@@ -0,0 +1,40 @@
+<?php
+namespace PartKeepr\UnitBundle\Tests\Model;
+
+use PartKeepr\SiPrefixBundle\Entity\SiPrefix;
+use PartKeepr\UnitBundle\Entity\Unit;
+
+class UnitTest extends \PHPUnit_Framework_TestCase
+{
+
+ public function testName()
+ {
+ $unit = $this->getUnit();
+
+ $unit->setName("Volt");
+ $this->assertEquals("Volt", $unit->getName());
+ }
+
+ public function testSymbol()
+ {
+ $unit = $this->getUnit();
+
+ $unit->setSymbol("V");
+
+ $this->assertEquals("V", $unit->getSymbol());
+ }
+
+ public function testPrefixes()
+ {
+ $unit = $this->getUnit();
+ $newSiPrefix = new SiPrefix();
+
+ $unit->setPrefixes(array($newSiPrefix));
+ $this->assertEquals(array($newSiPrefix), $unit->getPrefixes());
+ }
+
+ private function getUnit()
+ {
+ return new Unit();
+ }
+}