-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract association formatting to a more general formatting stack
- Loading branch information
1 parent
bc4e096
commit 7788091
Showing
6 changed files
with
189 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
concrete/src/Express/Entry/Formatter/EntryFormatterInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Concrete\Core\Express\Entry\Formatter; | ||
|
||
use Concrete\Core\Entity\Express\Entry; | ||
|
||
interface EntryFormatterInterface | ||
{ | ||
/** | ||
* Format a mask given an entry | ||
* | ||
* Mask format will typically be something like `Example %attribute_key% %association_handle` | ||
* | ||
* @param string $mask | ||
* @param \Concrete\Core\Entity\Express\Entry $entry | ||
* @return string|null | ||
*/ | ||
public function format($mask, Entry $entry); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Concrete\Core\Express\Entry\Formatter; | ||
|
||
use Concrete\Core\Entity\Express\Entry; | ||
use Concrete\Core\Express\Formatter\FormatterInterface; | ||
|
||
class LabelFormatter implements EntryFormatterInterface | ||
{ | ||
|
||
/** @var \Concrete\Core\Express\Formatter\FormatterInterface */ | ||
protected $formatter; | ||
|
||
public function __construct(FormatterInterface $formatter) | ||
{ | ||
$this->formatter = $formatter; | ||
} | ||
|
||
/** | ||
* Format a mask given an entry | ||
* @param string $mask | ||
* @param \Concrete\Core\Entity\Express\Entry $entry | ||
* @return string|null | ||
*/ | ||
public function format($mask, Entry $entry) | ||
{ | ||
return $this->formatter->format($mask, function($key) use ($entry) { | ||
$attribute = $entry->getAttribute($key); | ||
if ($attribute) { | ||
return $attribute; | ||
} | ||
|
||
$association = $entry->getAssociation($key); | ||
if (is_object($association)) { | ||
return $association->getSelectedEntry()->getLabel(); | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Concrete\Core\Express\Formatter; | ||
|
||
interface FormatterInterface | ||
{ | ||
/** | ||
* Format a mask using the standard format given a callable | ||
* | ||
* Ex: if you have attributes with handles `student_first_name` and `student_last_name` | ||
* `%student_last_name%, %student_first_name%` | ||
* | ||
* @param $mask | ||
* @param callable $matchHandler | ||
* @return mixed | ||
*/ | ||
public function format($mask, callable $matchHandler); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Concrete\Core\Express\Formatter; | ||
|
||
use Psr\Log\LoggerInterface; | ||
|
||
class LabelFormatter implements FormatterInterface | ||
{ | ||
|
||
/** @var \Psr\Log\LoggerInterface */ | ||
protected $logger; | ||
|
||
public function __construct(LoggerInterface $logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Format a mask using the standard format given a callable | ||
* | ||
* Ex: if you have attributes with handles `student_first_name` and `student_last_name` | ||
* `%student_last_name%, %student_first_name%` | ||
* | ||
* @param $mask | ||
* @param callable $matchHandler | ||
* @return mixed | ||
*/ | ||
public function format($mask, callable $matchHandler) | ||
{ | ||
try { | ||
// Run a regular expression to match the mask | ||
return preg_replace_callback('/%(.*?)%/i', function ($matches) use ($matchHandler) { | ||
// Return the result returned from the matchHandler | ||
return $this->getResult($matches[1], $matchHandler) ?: ''; | ||
}, $mask); | ||
} catch (\Exception $e) { | ||
// Log any failures | ||
$this->logger->debug( | ||
'Failed to format express mask "{mask}": {message}', | ||
['mask' => $mask, 'message' => $e->getMessage(), 'exception' => $e] | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Get a result given a key | ||
* @param $key | ||
* @param callable $matchHandler | ||
* @return string | ||
*/ | ||
private function getResult($key, callable $matchHandler) | ||
{ | ||
if ($key = trim($key)) { | ||
return $matchHandler($key) ?: ''; | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
} |