-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from nathansalter/bugfix/59-rpde-body-serialize
Add RpdeBody::deserialize() functionality
- Loading branch information
Showing
8 changed files
with
150 additions
and
34 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace OpenActive\Validators; | ||
|
||
use OpenActive\BaseModel; | ||
|
||
class RpdeItemDataValidator implements ValidatorInterface | ||
{ | ||
public function run($value) | ||
{ | ||
if ($value instanceof BaseModel) { | ||
return true; | ||
} | ||
if (null === $value) { | ||
return true; | ||
} | ||
if (is_object($value)) { | ||
$value = (array) $value; | ||
} | ||
return isset($value['@type']) | ||
&& null !== $this->getValidClass($value['@type']); | ||
} | ||
|
||
public function coerce($value) | ||
{ | ||
if ($value instanceof BaseModel) { | ||
return $value; | ||
} | ||
if (null === $value) { | ||
return $value; | ||
} | ||
|
||
$class = $this->getValidClass($value['@type']); | ||
if (null === $class) { | ||
throw new \InvalidArgumentException(sprintf('Invalid type %s given, no instantiable class', $value['@type'])); | ||
} | ||
return new $class($value); | ||
} | ||
|
||
private function getValidClass($type) | ||
{ | ||
$classOa = sprintf('\\OpenActive\\Models\\OA\\%s', $type); | ||
$classSchema = sprintf('\\OpenActive\\Models\\SchemaOrg\\%s', $type); | ||
|
||
if (class_exists($classOa)) { | ||
return $classOa; | ||
} | ||
if (class_exists($classSchema)) { | ||
return $classSchema; | ||
} | ||
return null; | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
namespace OpenActive\Validators; | ||
|
||
use OpenActive\Concerns\TypeChecker; | ||
use OpenActive\Rpde\RpdeItem; | ||
|
||
class RpdeItemValidator implements ValidatorInterface | ||
{ | ||
use TypeChecker; | ||
|
||
public function run($value) | ||
{ | ||
if ($value instanceof RpdeItem) { | ||
return true; | ||
} | ||
if (is_object($value)) { | ||
$value = (array) $value; | ||
} | ||
|
||
if (!isset($value['state'], $value['kind'], $value['id'], $value['modified'])) { | ||
return false; | ||
} | ||
|
||
if ('deleted' === $value['state']) { | ||
return true; | ||
} | ||
|
||
return isset($value['data']); | ||
} | ||
|
||
public function coerce($value) | ||
{ | ||
if ($value instanceof RpdeItem) { | ||
return $value; | ||
} | ||
return new RpdeItem($value); | ||
} | ||
} |
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