Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created ContextInterface #112

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 6 additions & 24 deletions src/Finite/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
namespace Finite;

use Finite\Factory\FactoryInterface;
use Finite\StateMachine\StateMachine;

/**
* The Finite context.
* It provides easy ways to deal with Stateful objects, and factory.
*
* @author Yohan Giarelli <[email protected]>
*/
class Context
class Context implements ContextInterface
{
/**
* @var FactoryInterface
Expand All @@ -27,22 +26,15 @@ public function __construct(FactoryInterface $factory)
}

/**
* @param object $object
* @param string $graph
*
* @return string
* {@inheritdoc}
*/
public function getState($object, $graph = 'default')
{
return $this->getStateMachine($object, $graph)->getCurrentState()->getName();
}

/**
* @param object $object
* @param string $graph
* @param bool $asObject
*
* @return array<string>
* {@inheritdoc}
*/
public function getTransitions($object, $graph = 'default', $asObject = false)
{
Expand All @@ -61,33 +53,23 @@ function ($transition) use ($stateMachine) {
}

/**
* @param object $object
* @param string $graph
*
* @return array<string>
* {@inheritdoc}
*/
public function getProperties($object, $graph = 'default')
{
return $this->getStateMachine($object, $graph)->getCurrentState()->getProperties();
}

/**
* @param object $object
* @param string $property
* @param string $graph
*
* @return bool
* {@inheritdoc}
*/
public function hasProperty($object, $property, $graph = 'default')
{
return $this->getStateMachine($object, $graph)->getCurrentState()->has($property);
}

/**
* @param object $object
* @param string $graph
*
* @return StateMachine
* {@inheritdoc}
*/
public function getStateMachine($object, $graph = 'default')
{
Expand Down
56 changes: 56 additions & 0 deletions src/Finite/ContextInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Finite;

use Finite\StateMachine\StateMachineInterface;

/**
* Interface that represents the contract between context
*
* @author Luis Henrique Mulinari <[email protected]>
*/
interface ContextInterface
{
/**
* @param object $object
* @param string $graph
*
* @return string
*/
public function getState($object, $graph = 'default');

/**
* @param object $object
* @param string $graph
* @param bool $asObject
*
* @return array<string>
*/
public function getTransitions($object, $graph = 'default', $asObject = false);

/**
* @param object $object
* @param string $graph
*
* @return array<string>
*/
public function getProperties($object, $graph = 'default');

/**
* @param object $object
* @param string $property
* @param string $graph
*
* @return bool
*/
public function hasProperty($object, $property, $graph = 'default');

/**
* @param object $object
* @param string $graph
*
* @return StateMachineInterface
*/
public function getStateMachine($object, $graph = 'default');
}

8 changes: 4 additions & 4 deletions src/Finite/Extension/Twig/FiniteExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Finite\Extension\Twig;

use Finite\Context;
use Finite\ContextInterface;

/**
* The Finite Twig extension.
Expand All @@ -12,14 +12,14 @@
class FiniteExtension extends \Twig_Extension
{
/**
* @var Context
* @var ContextInterface
*/
protected $context;

/**
* @param Context $context
* @param ContextInterface $context
*/
public function __construct(Context $context)
public function __construct(ContextInterface $context)
{
$this->context = $context;
}
Expand Down