MongoCode.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. */
  15. if (class_exists('MongoCode', false)) {
  16. return;
  17. }
  18. class MongoCode implements \Alcaeus\MongoDbAdapter\TypeInterface
  19. {
  20. /**
  21. * @var string
  22. */
  23. private $code;
  24. /**
  25. * @var array
  26. */
  27. private $scope;
  28. /**
  29. * @link http://php.net/manual/en/mongocode.construct.php
  30. * @param string $code A string of code
  31. * @param array $scope The scope to use for the code
  32. */
  33. public function __construct($code, array $scope = [])
  34. {
  35. if ($code instanceof \MongoDB\BSON\Javascript) {
  36. // @todo Use properties from object once they are accessible
  37. $code = '';
  38. }
  39. $this->code = $code;
  40. $this->scope = $scope;
  41. }
  42. /**
  43. * Returns this code as a string
  44. * @return string
  45. */
  46. public function __toString()
  47. {
  48. return $this->code;
  49. }
  50. /**
  51. * Converts this MongoCode to the new BSON JavaScript type
  52. *
  53. * @return \MongoDB\BSON\Javascript
  54. * @internal This method is not part of the ext-mongo API
  55. */
  56. public function toBSONType()
  57. {
  58. return new \MongoDB\BSON\Javascript($this->code, $this->scope);
  59. }
  60. }