forked from ushahidi/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DRY out JSON encoding code in repositories
Summary: - Create JsonTranscodeRepository trait and use it - Fix up layers test: don't expect an array unless JSON value is populated Test Plan: bin/behat Reviewers: lkamau, will, jasonmule Reviewed By: jasonmule Subscribers: jasonmule, will, rjmackay, lkamau Differential Revision: https://phabricator.ushahidi.com/D927
- Loading branch information
Showing
8 changed files
with
118 additions
and
108 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
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,56 @@ | ||
<?php defined('SYSPATH') OR die('No direct script access.'); | ||
|
||
/** | ||
* Ushahidi Json Transcoding Repo Trait | ||
* | ||
* JSON encodes properties defined `json_properties` | ||
* during `executeUpdate` and `executeInsert` | ||
* | ||
* @author Ushahidi Team <[email protected]> | ||
* @package Ushahidi\Application\Controllers | ||
* @copyright 2013 Ushahidi | ||
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3) | ||
*/ | ||
|
||
use Ushahidi\Core\Tool\JsonTranscode; | ||
|
||
trait Ushahidi_JsonTranscodeRepository { | ||
|
||
protected $json_transcoder; | ||
|
||
/** | ||
* Return an array of properties to be json encoded | ||
* @return Array | ||
*/ | ||
abstract protected function getJsonProperties(); | ||
|
||
public function setTranscoder(JsonTranscode $transcoder) | ||
{ | ||
$this->json_transcoder = $transcoder; | ||
} | ||
|
||
// Ushahidi_Repository | ||
public function executeInsert(Array $input) | ||
{ | ||
// JSON Encode defined properties | ||
$input = array_filter($this->json_transcoder->encode( | ||
$input, | ||
$this->getJsonProperties() | ||
)); | ||
|
||
return parent::executeInsert($input); | ||
} | ||
|
||
// Ushahidi_Repository | ||
public function executeUpdate(Array $where, Array $input) | ||
{ | ||
// JSON Encode defined properties | ||
$input = $this->json_transcoder->encode( | ||
$input, | ||
$this->getJsonProperties() | ||
); | ||
|
||
return parent::executeUpdate($where, $input); | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.